● EPPlus export excel error:Please set the ExcelPackage.LicenseContext property.

今天测试.net core 2.2到3.1时,顺便更新了EPPlus版本。导出excel时报错:

2021-03-31 20:51:27,481 [5] ERRORStaffController - OfficeOpenXml.LicenseException: Please set the ExcelPackage.LicenseContext property. See https://epplussoftware.com/developers/licenseexception
   at OfficeOpenXml.ExcelPackage.get_Workbook()
   at OfficeOpenXml.ExcelPackage.CreateBlankWb()
   at OfficeOpenXml.ExcelPackage.ConstructNewFile(String password)
   at OfficeOpenXml.ExcelPackage..ctor(FileInfo newFile)
   at PartnerPlatform.Service.StaffExport.ExportExcelSingleWorksheets(IDbConnection dbConnection, String sWebRootFolder, String fileName, List`1 staff) in /Users/jinyazhou/Test/PartnerPlatform/Service/ExcelExport.cs:line 43
   at PartnerPlatform.Controllers.StaffController.Export() in /Users/jinyazhou/Test/Controllers/ExcelController.cs:line 19
Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/3.1.13/System.Runtime.Serialization.Primitives.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

原因: EPPlus 5.0 以后的版本需要指定 商业证书 或者非商业证书。你需要在代码里指定证书或者降低EPPlus版本。在代码里面指定非商业证书:

ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

参考资料: https://www.hierror.com/exception/article/139035