开发环境: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(); //获取工作表
file.Close();
}
3、保存数据到HSSFWorkbook中,部分代码如下:
// 保存老师测距答案
for (int i = ; i < teacherData.Count; i++)
{
IRow sRow = sheet.GetRow(meaStartRow) ?? sheet.CreateRow(meaStartRow);
IRow eRow = sheet.GetRow(meaStartRow + ) ?? sheet.CreateRow(meaStartRow + ); var data = teacherData[i] as MeasureLineCompareData;
sRow.CreateCell().SetCellValue(data.Describe);
sRow.CreateCell().SetCellValue("起点");
sRow.CreateCell().SetCellValue(string.Format("{0} , {1} , {2}", Math.Round(data.StartPoint.x, ).ToString(),
Math.Round(data.StartPoint.y, ).ToString(), Math.Round(data.StartPoint.z, ).ToString()));
eRow.CreateCell().SetCellValue("终点");
eRow.CreateCell().SetCellValue(string.Format("{0} , {1} , {2}", Math.Round(data.EndPoint.x, ).ToString(),
Math.Round(data.EndPoint.y, ).ToString(), Math.Round(data.EndPoint.z, ).ToString())); // 合并单元格
CellRangeAddress range = new CellRangeAddress(meaStartRow, meaStartRow + , , );
sheet.AddMergedRegion(range); meaStartRow += ;
}
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, , 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 not supported
at System.Text.Encoding.GetEncoding (Int32 codepage) [0x00000] in <filename unknown>:
at NPOI.HPSF.CodePageString.GetJavaValue (Int32 codepage) [0x00000] in <filename unknown>:
at NPOI.HPSF.VariantSupport.Read (System.Byte[] src, Int32 offset, Int32 length, Int64 type, Int32 codepage) [0x00000] in <filename unknown>:
at NPOI.HPSF.Property..ctor (Int64 id, System.Byte[] src, Int64 offset, Int32 Length, Int32 codepage) [0x00000] in <filename unknown>:
at NPOI.HPSF.Section..ctor (System.Byte[] src, Int32 offset) [0x00000] in <filename unknown>:
at NPOI.HPSF.PropertySet.init (System.Byte[] src, Int32 offset, Int32 Length) [0x00000] in <filename unknown>:
at NPOI.HPSF.PropertySet..ctor (System.IO.Stream stream) [0x00000] in <filename unknown>:
at NPOI.HPSF.PropertySetFactory.Create (System.IO.Stream stream) [0x00000] in <filename unknown>:
at NPOI.POIDocument.GetPropertySet (System.String setName) [0x00000] in <filename unknown>:
at NPOI.POIDocument.ReadProperties () [0x00000] in <filename unknown>:
at NPOI.POIDocument.get_DocumentSummaryInformation () [0x00000] in <filename unknown>:
at NPOI.HSSF.UserModel.HSSFWorkbook.Write (System.IO.Stream stream) [0x00000] in <filename unknown>:
at CompareHelper.SaveWorkbook (System.String filePath, NPOI.HSSF.UserModel.HSSFWorkbook book) [0x00000] in <filename unknown>:
解决办法:
同样在C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0中找到I18N*.dll 开头的程序集放到Plugins文件夹下,搞定!!!!