Unity3d 使用NPOI读写Excel 遇到的问题
开发环境: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文件夹下,搞定!!!!