開發環境:unity5.3 NPOI(.net 2.0版 http://npoi.codeplex.com/)
運行環境:PC版, 其他平台沒有測試
先上效果圖:
實現步驟:
1、新建一個Excel模板(CompareTemplate.xls, .xlsx格式的無法讀取),用來設置表頭、顏色、字體樣式等。
2、使用npoi讀取模板文件
using (FileStream file = new FileStream(Path.Combine(tempFolder, "CompareTemplate.xls"), FileMode.Open, FileAccess.ReadWrite)) { book = new HSSFWorkbook(file); sheet = book.GetSheetAt(0); //獲取工作表
file.Close(); }
3、保存數據到HSSFWorkbook中,部分代碼如下:
// 保存老師測距答案 for (int i = 0; i < teacherData.Count; i++) { IRow sRow = sheet.GetRow(meaStartRow) ?? sheet.CreateRow(meaStartRow); IRow eRow = sheet.GetRow(meaStartRow + 1) ?? sheet.CreateRow(meaStartRow + 1); var data = teacherData[i] as MeasureLineCompareData; sRow.CreateCell(1).SetCellValue(data.Describe); sRow.CreateCell(2).SetCellValue("起點"); sRow.CreateCell(3).SetCellValue(string.Format("{0} , {1} , {2}", Math.Round(data.StartPoint.x, 4).ToString(), Math.Round(data.StartPoint.y, 4).ToString(), Math.Round(data.StartPoint.z, 4).ToString())); eRow.CreateCell(2).SetCellValue("終點"); eRow.CreateCell(3).SetCellValue(string.Format("{0} , {1} , {2}", Math.Round(data.EndPoint.x, 4).ToString(), Math.Round(data.EndPoint.y, 4).ToString(), Math.Round(data.EndPoint.z, 4).ToString())); // 合並單元格 CellRangeAddress range = new CellRangeAddress(meaStartRow, meaStartRow + 1, 1, 1); sheet.AddMergedRegion(range); meaStartRow += 2; }
4、保存到Excel文件中
using (MemoryStream ms = new MemoryStream()) { book.Write(ms); // 保存HSSFWorkbook到數據流中 string file = GetNewPathForDupes(filePath); // 獲取excel文件路徑 // 寫入excel文件 using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } System.Diagnostics.Process.Start(file);// 打開excel文件 book = null; }
發布項目時設置:
1、api compatibility level : .net 2.0;
2、在unity安裝目錄:C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0中找到 System.Drawing.dll放到項目Plugins文件夾下;
==============================
上面兩個設置在網上都能方便的找到。But,發布成exe后,保存excel報錯
System.NotSupportedException: CodePage 936 not supported at System.Text.Encoding.GetEncoding (Int32 codepage) [0x00000] in <filename unknown>:0 at NPOI.HPSF.CodePageString.GetJavaValue (Int32 codepage) [0x00000] in <filename unknown>:0 at NPOI.HPSF.VariantSupport.Read (System.Byte[] src, Int32 offset, Int32 length, Int64 type, Int32 codepage) [0x00000] in <filename unknown>:0 at NPOI.HPSF.Property..ctor (Int64 id, System.Byte[] src, Int64 offset, Int32 Length, Int32 codepage) [0x00000] in <filename unknown>:0 at NPOI.HPSF.Section..ctor (System.Byte[] src, Int32 offset) [0x00000] in <filename unknown>:0 at NPOI.HPSF.PropertySet.init (System.Byte[] src, Int32 offset, Int32 Length) [0x00000] in <filename unknown>:0 at NPOI.HPSF.PropertySet..ctor (System.IO.Stream stream) [0x00000] in <filename unknown>:0 at NPOI.HPSF.PropertySetFactory.Create (System.IO.Stream stream) [0x00000] in <filename unknown>:0 at NPOI.POIDocument.GetPropertySet (System.String setName) [0x00000] in <filename unknown>:0 at NPOI.POIDocument.ReadProperties () [0x00000] in <filename unknown>:0 at NPOI.POIDocument.get_DocumentSummaryInformation () [0x00000] in <filename unknown>:0 at NPOI.HSSF.UserModel.HSSFWorkbook.Write (System.IO.Stream stream) [0x00000] in <filename unknown>:0 at CompareHelper.SaveWorkbook (System.String filePath, NPOI.HSSF.UserModel.HSSFWorkbook book) [0x00000] in <filename unknown>:0
解決辦法:
同樣在C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0中找到I18N*.dll 開頭的程序集放到Plugins文件夾下,搞定!!!!