先說坑花了一下午才找到解決方法解決,
在Unity編輯模式下點擊物體創(chuàng)建對應(yīng)的表,獲取物體名字與在InputText填寫的注釋數(shù)據(jù)。然后保存。創(chuàng)建Exect表可以打開,打包PC后,點擊物體創(chuàng)建的表,打不開文件破損
解決方法:到unity編輯器所在路徑中去找這個路徑
\Editor\Data\MonoBleedingEdge\lib\mono\unityaot 或者 unityjit 文件夾或者unityaot-win32
找到里邊以”I18N“開頭的這四個dll文件
?
在這里感謝大佬:LAIALAIA
解決方法思路原版鏈接:unity 使用EPPlus對Excel的創(chuàng)建、寫入、讀取操作 - 噠噠噠~~~ - 博客園 (cnblogs.com)
創(chuàng)建讀取刪除Execel
?// 初始化 Excel 文件
?文章來源:http://www.zghlxwxcb.cn/news/detail-652243.html
// 初始化 Excel 文件
public void InitializeExcelFile(string ExcelFileName)
{
// 獲取應(yīng)用的數(shù)據(jù)文件夾路徑
string dataPath = Application.streamingAssetsPath + "/Data";
// 合并路徑,得到完整的 Excel 文件路徑
excelFilePath = Path.Combine(dataPath, ExcelFileName + ".xls");
// 創(chuàng)建一個文件信息對象來檢查 Excel 文件是否存在
FileInfo excelFile = new FileInfo(excelFilePath);
// 隱藏輸入框
inputField.gameObject.SetActive(false);
// 如果 Excel 文件不存在,創(chuàng)建一個新的 Excel 工作簿,并添加一個工作表
if (!excelFile.Exists)
{
// 創(chuàng)建一個新的 Excel 工作簿對象
workbook = new XSSFWorkbook();
// 在工作簿中創(chuàng)建一個名為 "Comments" 的工作表
sheet = (XSSFSheet)workbook.CreateSheet(ExcelFileName);
// 在工作表的第一行(行索引為0)上創(chuàng)建一個新的行對象,并在該行中創(chuàng)建一個新的單元格對象(列索引為0)
// 然后將 "Object Name" 這個字符串設(shè)置為單元格的值
sheet.CreateRow(0).CreateCell(0).SetCellValue("Object Name");
// 獲取工作表的第一行(行索引為0),然后在該行中創(chuàng)建一個新的單元格對象(列索引為1)
// 然后將 "Comment" 這個字符串設(shè)置為單元格的值
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);
}
}
}
刪除文章來源地址http://www.zghlxwxcb.cn/news/detail-652243.html
? ?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 的文本內(nèi)容
? ? ? ? ? ? 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); // 創(chuàng)建一個新行
? ? ? ? ? ? ? ? for (int j = 0; j < currentRow.LastCellNum; j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ICell currentCell = currentRow.GetCell(j);
? ? ? ? ? ? ? ? ? ? ICell newCell = newRow.CreateCell(j); // 創(chuàng)建一個新單元格
? ? ? ? ? ? ? ? ? ? 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);
? ? ? ? ? ? }
? ? ? ? }
? ? }
到了這里,關(guān)于Unity用NPOI創(chuàng)建Exect表,保存數(shù)據(jù),和修改刪除數(shù)據(jù)。以及打包后的坑——無法打開新創(chuàng)建的Exect表的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!