Unity用NPOI创建Exect表,保存数据,和修改删除数据。以及打包后的坑——无法打开新创建的Exect表

先说坑花了一下午才找到解决方法解决,

在Unity编辑模式下点击物体创建对应的表,获取物体名字与在InputText填写的注释数据。然后保存。创建Exect表可以打开,打包PC后,点击物体创建的表,打不开文件破损

解决方法:到unity编辑器所在路径中去找这个路径
\Editor\Data\MonoBleedingEdge\lib\mono\unityaot 或者 unityjit 文件夹或者unityaot-win32
找到里边以”I18N“开头的这四个dll文件

 

在这里感谢大佬:LAIALAIA

解决方法思路原版链接:unity 使用EPPlus对Excel的创建、写入、读取操作 - 哒哒哒~~~ - 博客园 (cnblogs.com)

创建读取删除Execel

 // 初始化 Excel 文件
 

 // 初始化 Excel 文件
        public void InitializeExcelFile(string ExcelFileName)
        {
        // 获取应用的数据文件夹路径
        string dataPath = Application.streamingAssetsPath + "/Data";
        // 合并路径,得到完整的 Excel 文件路径
        excelFilePath = Path.Combine(dataPath, ExcelFileName + ".xls");
        // 创建一个文件信息对象来检查 Excel 文件是否存在
        FileInfo excelFile = new FileInfo(excelFilePath);
        // 隐藏输入框
        inputField.gameObject.SetActive(false);
        // 如果 Excel 文件不存在,创建一个新的 Excel 工作簿,并添加一个工作表
        if (!excelFile.Exists)
        {
            // 创建一个新的 Excel 工作簿对象
            workbook = new XSSFWorkbook();
            // 在工作簿中创建一个名为 "Comments" 的工作表
            sheet = (XSSFSheet)workbook.CreateSheet(ExcelFileName);
            // 在工作表的第一行(行索引为0)上创建一个新的行对象,并在该行中创建一个新的单元格对象(列索引为0)
            // 然后将 "Object Name" 这个字符串设置为单元格的值
            sheet.CreateRow(0).CreateCell(0).SetCellValue("Object Name");
            // 获取工作表的第一行(行索引为0),然后在该行中创建一个新的单元格对象(列索引为1)
            // 然后将 "Comment" 这个字符串设置为单元格的值
            sheet.GetRow(0).CreateCell(1).SetCellValue("Comment");
        }
        else
        {
            using (FileStream fs = new FileStream(excelFilePath, FileMode.Open, FileAccess.Read))
            {
                workbook = new XSSFWorkbook(fs);
                sheet = (XSSFSheet)workbook.GetSheetAt(0);
            }
        }
    }

删除

   private void DeleteObjectFromExcel()
    {
        if (workbook == null || lastClickedObject == null)
        {
            Debug.LogError("Excel workbook is not initialized or no object clicked.");
            return;
        }

        string objectName = lastClickedObject.name;

        int rowIndex = FindRowIndexByObjectName(objectName);
        if (rowIndex >= 0)
        {
            // 删除选定的行
            sheet.RemoveRow(sheet.GetRow(rowIndex));

            // 清空 InputField 的文本内容
            inputField.text = "";

            for (int i = rowIndex + 1; i <= sheet.LastRowNum; i++)
            {
                IRow currentRow = sheet.GetRow(i);

                // 跳过已删除的行
                if (currentRow == null)
                {
                    continue;
                }

                IRow newRow = sheet.CreateRow(i - 1); // 创建一个新行
                for (int j = 0; j < currentRow.LastCellNum; j++)
                {
                    ICell currentCell = currentRow.GetCell(j);
                    ICell newCell = newRow.CreateCell(j); // 创建一个新单元格
                    if (currentCell != null)
                    {
                        newCell.SetCellValue(currentCell.ToString());
                    }
                }
            }

            // 清除最后一行
            sheet.RemoveRow(sheet.GetRow(sheet.LastRowNum));


            // 保存修改后的文件
            using (FileStream fs = new FileStream(excelFilePath, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(fs);
            }
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值