C# 讀寫 Excel 四種方案(OpenXml、NPOI、EPPlus、Spire.Office)
前言
在項(xiàng)目中需要使用C#讀寫Excel,每天定時(shí)將數(shù)據(jù)輸出到Excel表格中。
在參考了很多的方案后,找到了4個(gè)常用的方案,并提供了一些小Demo。
更多功能和示例可以參考官方文檔。
1、Microsoft.Office.Interop.Excel:不推薦使用。
2、OpenXml:微軟官方提供的SDK。
3、NPOI:第三方開源框架,口碑很好。
4、EPPlus:只能用于讀寫Excel,筆者目前使用最多。
5、Spire.Office for .NET:商業(yè)解決方案,功能完善強(qiáng)大。免費(fèi)版功能也夠用。
環(huán)境和工具
IDE:Visual Studio 2019 和 Visual Studio 2022
框架:.NET Framework 4.8 和 .NET 6.0
功能介紹
1、Microsoft.Office.Interop.Excel
最原始的操作庫(kù),兼容性一般,偶爾會(huì)出現(xiàn)內(nèi)存泄漏和文件無(wú)法解除占用的問題(也可能是我太菜),不推薦使用。
2、OpenXml
GitHub:github.com/OfficeDev/Open-XML-SDK
NuGet:nuget.org/packages/DocumentFormat.OpenXml
微軟官方提供的一個(gè)SDK用于讀寫Office。
官方文檔:docs.microsoft.com/en-us/office/open-xml/open-xml-sdk
3、NPOI
在Java中,有Apache POI這樣一個(gè)API用于操作Office。
Java POI:https://poi.apache.org/
在C#中,有開源免費(fèi)的NPOI。雖然不知道二者有什么關(guān)系,但總感覺很相似。
GitHub:https://github.com/nissl-lab/npoi
NuGet:https://www.nuget.org/packages/NPOI/
使用方法
public void TestNPOI()
{
string sourceFile = @"D:\sourceFile.xlsx";
string targetFile = @"D:\targetFile.xlsx";
IWorkbook workbook = new XSSFWorkbook(sourceFile);
ISheet sheet1 = workbook.GetSheet("Sheet1");
sheet1.CreateRow(0).CreateCell(0).SetCellValue(1);
sheet1.CreateRow(1).CreateCell(0).SetCellValue(2);
sheet1.CreateRow(2).CreateCell(0).SetCellValue(3);
FileStream fs = new FileStream(targetFile, FileMode.Create);
workbook.Write(fs);
workbook.Close();
}
NPOI在使用過程中有些不習(xí)慣,特別是在設(shè)置單元格內(nèi)容時(shí),和傳統(tǒng)的思維方式不太一樣。
4、EPPlus
官方網(wǎng)站:https://epplussoftware.com/
GitHub:https://github.com/EPPlusSoftware
NuGet:https://www.nuget.org/packages/EPPlus/
EPPlus通過LicenseContext來(lái)區(qū)分商業(yè)應(yīng)用(Commercial)和非商業(yè)應(yīng)用(NonCommercial)。
使用方法
public void TestEPPlus()
{
string sourceFile = @"D:\sourceFile.xlsx";
string targetFile = @"D:\targetFile.xlsx";
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;//指明非商業(yè)應(yīng)用
ExcelPackage package = new ExcelPackage(sourceFile);//加載Excel工作簿
ExcelWorksheet sheet1 = package.Workbook.Worksheets["Sheet1"];//讀取工作簿中名為"Sheet1"的工作表
sheet1.Cells[1, 1].Value = "A";//設(shè)置單元格內(nèi)容
sheet1.Cells[2, 2].Value = "B";
sheet1.Cells[3, 3].Value = "C";
sheet1.Cells[1, 2].Value = "1";
sheet1.Cells[2, 2].Value = "2";
sheet1.Cells[3, 2].Value = "3";
//package.Save();//將更改保存到原文件
package.SaveAs(targetFile);//將更改保存到新的文件,類似于另存為
}
筆者目前使用最多的框架,操作簡(jiǎn)單,符合使用習(xí)慣。但是EPPlus只能用于讀寫Excel,不能讀寫Word等其他文件。
5、Spire.Office for .NET
官方網(wǎng)站:https://www.e-iceblue.com/
GitHub:https://github.com/eiceblue
NuGet:https://www.nuget.org/packages/FreeSpire.Office/
Spire.Office提供了一整套的Office解決方案,可以讀寫、展示W(wǎng)ord、Excel、PDF等。分為收費(fèi)版和免費(fèi)版。
使用方法
public void TestSpireOffice()
{
string sourceFile = @"D:\sourceFile.xlsx";
string targetFile = @"D:\targetFile.xlsx";
Workbook workbook = new Workbook();
workbook.LoadFromFile(sourceFile);//加載Excel工作簿
Worksheet sheet1 = workbook.Worksheets["Sheet1"];//讀取工作簿中名為"Sheet1"的工作表
sheet1.SetCellValue(1, 1, "A");//設(shè)置單元格內(nèi)容
sheet1.SetCellValue(2, 1, "B");
sheet1.SetCellValue(3, 1, "C");
sheet1.SetCellValue(1, 2, "1");
sheet1.SetCellValue(2, 2, "2");
sheet1.SetCellValue(3, 2, "3");
workbook.Save();//將更改保存到原文件
workbook.SaveToFile(targetFile);//將更改保存到新的文件,類似于另存為
}
Spire.Office的收費(fèi)版可以在WinForm中直接展示W(wǎng)ord、Excel、PDF文件內(nèi)容,但是免費(fèi)版不能展示Excel。
一種替代方式是先將Excel轉(zhuǎn)換成PDF,再展示PDF,但這種方案只能展示前三頁(yè)。
總結(jié)
以上介紹的5中方案中,筆者最常使用的是EPPlus和Spire.Office。畢竟是商業(yè)軟件,體驗(yàn)確實(shí)不一樣。免費(fèi)版功能夠用即可。
如果只是需要讀寫Excel,那么EPPlus非常方便而且符合使用習(xí)慣。
如果需要在WinForm中展示,那么Spire.Office可能是唯一選擇。
如果需要讀寫多種Word、Excel等Office文件,OpenXml和NPOI也是不錯(cuò)的選擇。
寫完文章發(fā)現(xiàn)了一個(gè)網(wǎng)站,也介紹了許多關(guān)于C#讀寫Office的各種方法,比我寫的詳細(xì)多了,推薦給大家。
OpenXml
https://www.thecodebuzz.com/read-excel-file-in-dotnet-core-2-1/
NPOI
https://www.thecodebuzz.com/read-and-write-excel-file-in-net-core-using-npoi/
EPPlus
https://www.thecodebuzz.com/read-write-excel-in-dotnet-core-epplus/
轉(zhuǎn)自:紙短情長(zhǎng)ZF
鏈接:blog.csdn.net/m0_49284219/article/details/121728799
