C#调用WPS的etapi.dll读写Excel

开发环境要求

  1. 安装WPS Office专业版,2016或者2019都可以

  2. 推荐下载地址: https://www.jb51.net/softs/681559.html

  3. 在项目中引用etapi.dll

  4. DLL在WPS的安装目录中,如C:\Program Files (x86)\Kingsoft\WPS Office\10.8.2.6666\office6

代码开发

代码的写法与使用Microsoft.Office.Interop.Excel.dll的方法完全一致

  • 注:客户端安装Office/WPS都可以正常使用程序,WPS安装个人版即可

示例:C#

Excel.Application app;
Excel.Workbook book;
Excel.Worksheet sheet;
Excel.Worksheet sheet2;
Excel.Range range;

app = new Excel.Application();
book = app.Workbooks.Add();
range = sheet.Range("A1");
range.Value2 = "Hello:";
sheet.SheetName = "我是Sheet1";
book.SaveAs("my file path");
book.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
GC.Collect();    // 强制进行垃圾回收

示例:VB.net

Dim app As Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim sheet2 As Excel.Worksheet
Dim range As Excel.Range

app = New Excel.Application
book = app.Workbooks.Add()
range = sheet.Range("A1")
range.Value2 = "Hello:"
sheet.SheetName = "我是Sheet1";
book.SaveAs("my file path")
book.Close()
app.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
GC.Collect()    '强制进行垃圾回收

以上为WPS使用方法,不过我建议大家使用Spire.Xls来创建/读写Excel

可参照我的另一篇文章:C#使用Free Spire.XLS创建Excel

你可能感兴趣的:(C#,.net,etapi,wps,C#,etapi.dll,excel)