> 技术文档 > Unity读写.xmls文件

Unity读写.xmls文件


背景

通过EPPlus插件,实现Unity读写.xlsx。

Unity2021.3.15f1

EPPlus8.0.7

安装NuGet的Unity插件

通过NuGet for Unity安装EPPlus,会把EPPlus插件下载到Assets\\Packages,可以直接使用,不再需要再复制相关.dll到Plugins中

  1. 从GitHub下载

    选择最新版,下载.unitypackage文件。

    NuGet for Unity下载地址为:https://github.com/GlitchEnzo/NuGetForUnity/releases

NuGet for Unity

  1. 导入

    拖到Unity中即可。

    或者从Unity的菜单栏Assets > Import Package > Custom Package导入。

    导入

  2. 导入成功后Unity菜单栏会多一个NuGet的菜单项。
    NuGet菜单项

安装EPPlus

  1. 打开NuGet的包管理界面
    Unity菜单 NuGet > Manage NuGet Packages。
    Manage NuGet Packages

  2. 搜索

    在NuGet for Unity弹窗中,搜索EPPLus。

    搜索EPPlus

  3. 安装

    选择第一个安装即可,相关的插件会一起安装。

    安装完成后,在Installed切页中可以看见安装的内容,Install按钮会变为Uninstall按钮,可以卸载相关插件

    安装EPPLus

操作.xlsx

  1. 引用命名空间

    using OfficeOpenXml;
  2. 设置License

    在操作.xlsx文件之前,需要先设置License,设置非商业的License可以不用购买。

    ExcelPackage.License.SetNonCommercialPersonal(\"GoDotTools\");

    更多的设置方法及License区别参考:https://epplussoftware.com/en/Home/GettingStartedCommunityLicense

  3. 打开、保存

    通过new ExcelPackage对象打开.xlsx文件,参数是文件的路径。

    通过ExcelPackage对象Save()方法保存.xlsx文件

    ExcelPackage对象的Workbook.

    using (ExcelPackage excel = new ExcelPackage(excelFile)){ ExcelWorksheet worksheet = excel.Workbook.Worksheets[0]; if (worksheet == null || worksheet.Dimension == null) { EditorUtility.DisplayDialog(\"Error\", $\"Failed to open file‌\", \"OK\"); return; } excel.Save();}
  4. 操作表单sheet

    通过ExcelPackage对象的Workbook.Worksheets属性可以访问表单。

    1. 通过索引获取

      ExcelWorksheet worksheet = excel.Workbook.Worksheets[0];
    2. 通过名字获取

      ExcelWorksheet worksheet = excel.Workbook.Worksheets[\"Sheet1\"];
    3. 新增

      ExcelWorksheet worksheet = excel.Workbook.Worksheets.Add(\"Sheet2\");
    4. 删除

      excel.Workbook.Worksheets.Delete(\"Sheet2\");
  5. 操作单元格

    通过ExcelWorksheet对象的Cells属性可以访问单元格。

    需要注意的是,Cells的索引是从1开始的。

    1. 修改

      worksheet.Cells[1, 1].Value = \"NewValue\";
    2. 追加

      追加先找到最后一行的,对该值加1作为索引,直接对Cells写入即可。

      int row = worksheet.Dimension.End.Row + 1;worksheet.Cells[row, 1].Value = \"NewValue\";
    3. 删除

      删除的参数为删除的方式

      worksheet.Cells[1, 1].Delete(eShiftTypeDelete.EntireRow);

      删除方式包括右侧单元格左移、下方单元格上移、删除整行、删除整列

      public enum eShiftTypeDelete{ // // 摘要: // Cells in the range are shifted to the left Left, // // 摘要: // Cells in the range are shifted upwards Up, // // 摘要: // The range for the entire row is used in the shift operation EntireRow, // // 摘要: // The range for the entire column is used in the shift operation EntireColumn}

遇到的问题

LicenseNotSetException

问题:

调用using (ExcelPackage excel = new ExcelPackage(excelFile))时,报错:

LicenseNotSetException: Please set the license using one of the methods on the static property ExcelPackage.License. See https://epplussoftware.com/developers/licensenotsetexception for more informationOfficeOpenXml.ExcelPackage.get_Workbook () (at :0)

原因:

没有设置License。

解决:

在操作.xlsx之前,调用ExcelPackage.License.SetNonCommercialPersonal设置许可证。

访问Cells时提示越界

问题:

执行如下代码:

int row = worksheet.Dimension.End.Row + 1; for (int i = 0; i < data.Count; i++){ worksheet.Cells[row, i].Value = data[i];}

报错:

ArgumentException: Column out of rangeOfficeOpenXml.ExcelRange.ValidateRowCol (System.Int32 Row, System.Int32 Col) (at :0)OfficeOpenXml.ExcelRange.get_Item (System.Int32 Row, System.Int32 Col) (at :0)

原因

Cells的索引是从1开始的,但for循环中i的初始值为0,所以越界。

解决:

int row = worksheet.Dimension.End.Row + 1; for (int i = 0; i < data.Count; i++){ worksheet.Cells[row, i+1].Value = data[i];}

参考资料

https://epplussoftware.com/en/Home/GettingStartedCommunityLicense

https://blog.csdn.net/cherry_f_f/article/details/142976605

痞子鱼互联网观察平台