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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using Excel;
- namespace WebApplication1
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Excel.Application appli = new Application();
- /*wsp excel导出*/
- /*Excel._Workbook wk = appli.Workbooks.Add(Type.Missing);
- Excel.Worksheet sheet = wk.ActiveSheet;
- sheet.Cells[1, 1] = "WPS表格测试";
- 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);
- wk.Close(Type.Missing, Type.Missing, Type.Missing);
- System.Runtime.InteropServices.Marshal.ReleaseComObject(wk);
- System.Runtime.InteropServices.Marshal.ReleaseComObject(appli);*/
- /*wps excel导入*/
- Excel._Workbook wk = appli.Workbooks.Open(Server.MapPath("/2.xls"));
- Excel.Worksheet sheet = wk.Worksheets.get_Item(1);
- Excel.Range range = sheet.UsedRange;
- string data = ((Excel.Range)range.get_Item(1, 1)).Text;
- int rowCount = range.Rows.Count;
- int columCount = range.Columns.Count;
- for (int j = 1; j <= rowCount; j++)
- {
- string info = "";
- for (int i = 1; i <= columCount; i++)
- {
- info += " " + ((Excel.Range)range.get_Item(j)).Cells[j, i].Text;
- }
- Response.Write(info + "
- ");
- }
- }
- }
- }
导入效果: