【发布时间】:2016-10-31 05:25:04
【问题描述】:
请看下面的代码sn-p。我只需打开 excel 文件 myfile.xlsx 并从 List<Account> 类型的对象添加行(其中我的 Account 对象只有 Date、Account 和 Amount 属性),并将文件存储为名称myoutputfile.xlsx。我希望我写日期的单元格具有日期格式,而我有数量的单元格具有数字格式。但是,如果我尝试下面的代码,所有单元格都使用#.00 格式(日期也是如此)。我已经尝试了一切,有人可以告诉我发生了什么吗?我正在使用 NPOI。
XSSFWorkbook wb;
var fileName = "C:/tmp/myfile.xlsx";
var outputFileName = "C:/tmp/myoutputfile.xlsx";
using (var file = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
wb = new XSSFWorkbook(file);
}
XSSFSheet sheet = (XSSFSheet) wb.GetSheetAt(0);
for (int i = 0; i < accountRecs.Count(); ++i) {
var rec = accountRecs[i];
var row = sheet.CreateRow(i);
var dateCell = row.CreateCell(3);
dateCell.SetCellValue(rec.Date);
dateCell.CellStyle.DataFormat = wb.CreateDataFormat().GetFormat("dd/MM/yyyy");
var accountCell = row.CreateCell(4);
accountCell.SetCellValue(rec.Account);
var totalValueCell = row.CreateCell(16);
totalValueCell.SetCellValue(rec.Amount);
totalValueCell.CellStyle.DataFormat = wb.CreateDataFormat().GetFormat("#.00");
}
using (var file = new FileStream(outputFileName, FileMode.Create, FileAccess.Write))
{
wb.Write(file);
file.Close();
}
【问题讨论】:
-
可能与stackoverflow.com/a/3603750/619252有关。尝试使用
dd/MM/yyyy的内置格式。