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

时间:2023-03-08 22:20:37

微软的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引用如下图:C#利用最新版的WPS实现导入导出

本程序要引用etapi.all

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

导入效果:

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