如何在Revit 2022插件中使用EPPlus非商业许可证
关于EPPlus非商业许可证声明的问题
是的,你必须将ExcelPackage.LicenseContext = LicenseContext.NonCommercial;这段代码整合到你的脚本中。
从EPPlus 5.x版本开始,官方要求所有使用者必须明确声明许可证类型,不管是商业还是非商业用途。如果不设置这个属性,运行代码时会直接抛出许可证相关的异常,导致插件无法正常读取Excel文件。
修改后的完整代码
using Autodesk.Revit.DB; using Autodesk.Revit.UI; using OfficeOpenXml; using System; using System.IO; using System.Windows.Forms; namespace Jada { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)] public class ReadExcel : IExternalCommand { static AddInId appID = new AddInId(new Guid("D8822352-EC01-42AA-8165-714A26786540")); public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet) { string filename = ""; OpenFileDialog openDialog = new OpenFileDialog(); openDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); openDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"; if (openDialog.ShowDialog() == DialogResult.OK) filename = openDialog.FileName; else return Result.Cancelled; // 声明非商业许可证,必须在创建ExcelPackage实例前设置 ExcelPackage.LicenseContext = LicenseContext.NonCommercial; string data = ""; using (ExcelPackage package = new ExcelPackage(new FileInfo(filename))) { ExcelWorksheet sheet = package.Workbook.Worksheets[1]; for (int row = 1; row < 9999; row++) { var thisValue = sheet.Cells[row, 1].Value; if (thisValue == null || thisValue.ToString() == "") break; data += thisValue.ToString() + ","; data += Environment.NewLine; } } TaskDialog.Show("Excel", data); return Result.Succeeded; } } }
额外注意事项
- 确认你安装的EPPlus版本是5.x及以上,旧版本(4.x及更早)不需要设置许可证上下文
- 确保你的插件确实用于非商业用途,如果是商业项目,需要购买对应的EPPlus商业许可证
内容的提问来源于stack exchange,提问作者Victoria
相关产品推荐
相关产品推荐

