C#利用最新版的WPS实现导入导出

微软的EXCEl操作相信大家也知道,不方便,安装包太大,而且表格的数据量也只有6000多(是6000多还是60000多我就忘记了),在导出导入大量数据的就没办法,而wsp表格则实现了百万数据的容量,而且安装包也小,操作更方便。下面利用最wps2015实现了一个简单的导入到出,参考http://blog.163.com/felex_cheng@126/blog/static/410470052013818325357/文章,也可也参考一下wsp二次开发文档http://www.wps.cn/wpsapi/funcapilist/page-1.htm,相应的dll引用如下图:

本程序要引用etapi.all

 

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using Excel;  
  8.   
  9. namespace WebApplication1  
  10. {  
  11.     public partial class WebForm1 : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             Excel.Application appli = new Application();  
  16.   
  17.             /*wsp excel导出*/  
  18.             /*Excel._Workbook wk = appli.Workbooks.Add(Type.Missing); 
  19.             Excel.Worksheet sheet = wk.ActiveSheet; 
  20.             sheet.Cells[1, 1] = "WPS表格测试"; 
  21.             wk.SaveAs(Server.MapPath("/2.xls"), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
  22.             wk.Close(Type.Missing, Type.Missing, Type.Missing); 
  23.             System.Runtime.InteropServices.Marshal.ReleaseComObject(wk); 
  24.             System.Runtime.InteropServices.Marshal.ReleaseComObject(appli);*/  
  25.   
  26.             /*wps excel导入*/  
  27.             Excel._Workbook wk = appli.Workbooks.Open(Server.MapPath("/2.xls"));  
  28.             Excel.Worksheet sheet = wk.Worksheets.get_Item(1);  
  29.             Excel.Range range = sheet.UsedRange;  
  30.   
  31.             string data = ((Excel.Range)range.get_Item(1, 1)).Text;  
  32.             int rowCount = range.Rows.Count;  
  33.             int columCount = range.Columns.Count;  
  34.             for (int j = 1; j <= rowCount; j++)  
  35.             {  
  36.                 string info = "";  
  37.                 for (int i = 1; i <= columCount; i++)  
  38.                 {  
  39.                     info += " " + ((Excel.Range)range.get_Item(j)).Cells[j, i].Text;  
  40.                 }  
  41.                 Response.Write(info + "  
  42. ");  
  43.             }  
  44.               
  45.         }  
  46.     }  
  47. }  

 

导入效果:

posted on 2016-11-17 22:15  大西瓜3721  阅读(1294)  评论(0编辑  收藏  举报

导航