C# 读写 Excel 四种方案(OpenXml、NPOI、EPPlus、Spire.Office)

前言

在项目中需要使用C#读写Excel,每天定时将数据输出到Excel表格中。

在参考了很多的方案后,找到了4个常用的方案,并提供了一些小Demo。

更多功能和示例可以参考官方文档。.

1、Microsoft.Office.Interop.Excel:不推荐使用。

2、OpenXml:微软官方提供的SDK。

3、NPOI:第三方开源框架,口碑很好。

4、EPPlus:只能用于读写Excel,笔者目前使用最多。

5、Spire.Office for .NET:商业解决方案,功能完善强大。免费版功能也够用。

环境和工具

IDE:Visual Studio 2019 和 Visual Studio 2022

框架:.NET Framework 4.8 和 .NET 6.0

功能介绍

1、Microsoft.Office.Interop.Excel

最原始的操作库,兼容性一般,偶尔会出现内存泄漏和文件无法解除占用的问题(也可能是我太菜),不推荐使用。

2、OpenXml

GitHub:github.com/OfficeDev/Open-XML-SDK

NuGet:nuget.org/packages/DocumentFormat.OpenXml

微软官方提供的一个SDK用于读写Office。

官方文档:docs.microsoft.com/en-us/office/open-xml/open-xml-sdk

3、NPOI

在Java中,有Apache POI这样一个API用于操作Office。

Java POI:https://poi.apache.org/

在C#中,有开源免费的NPOI。虽然不知道二者有什么关系,但总感觉很相似。

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在使用过程中有些不习惯,特别是在设置单元格内容时,和传统的思维方式不太一样。

4、EPPlus

官方网站:https://epplussoftware.com/

GitHub:https://github.com/EPPlusSoftware

NuGet:https://www.nuget.org/packages/EPPlus/

EPPlus通过LicenseContext来区分商业应用(Commercial)和非商业应用(NonCommercial)。

使用方法

public void TestEPPlus()
{
    string sourceFile = @"D:\sourceFile.xlsx";
    string targetFile = @"D:\targetFile.xlsx";

    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;//指明非商业应用
    ExcelPackage package = new ExcelPackage(sourceFile);//加载Excel工作簿
    ExcelWorksheet sheet1 = package.Workbook.Worksheets["Sheet1"];//读取工作簿中名为"Sheet1"的工作表

    sheet1.Cells[1, 1].Value = "A";//设置单元格内容
    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);//将更改保存到新的文件,类似于另存为
}

笔者目前使用最多的框架,操作简单,符合使用习惯。但是EPPlus只能用于读写Excel,不能读写Word等其他文件。

5、Spire.Office for .NET

官方网站:https://www.e-iceblue.com/

GitHub:https://github.com/eiceblue

NuGet:https://www.nuget.org/packages/FreeSpire.Office/

Spire.Office提供了一整套的Office解决方案,可以读写、展示Word、Excel、PDF等。分为收费版和免费版。

使用方法

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");//设置单元格内容
    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的收费版可以在WinForm中直接展示Word、Excel、PDF文件内容,但是免费版不能展示Excel。

一种替代方式是先将Excel转换成PDF,再展示PDF,但这种方案只能展示前三页。

总结

以上介绍的5中方案中,笔者最常使用的是EPPlusSpire.Office。毕竟是商业软件,体验确实不一样。免费版功能够用即可。

如果只是需要读写Excel,那么EPPlus非常方便而且符合使用习惯。

如果需要在WinForm中展示,那么Spire.Office可能是唯一选择。

如果需要读写多种Word、Excel等Office文件,OpenXml和NPOI也是不错的选择。

写完文章发现了一个网站,也介绍了许多关于C#读写Office的各种方法,比我写的详细多了,推荐给大家。

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/