我正在使用NPOI库创建excel文件,我在格式化价格方面有问题。
ISheet excelSheet = workbook.CreateSheet(sheetName);
ICellStyle codeCellStyle = workbook.CreateCellStyle();
ICellStyle priceCellStyle = workbook.CreateCellStyle();
ICellStyle availabilityStyle = workbook.CreateCellStyle();
excelSheet.SetColumnWidth(0, 10 * 256);
excelSheet.SetColumnWidth(1, 12 * 256);
excelSheet.SetColumnWidth(2, 15 * 256);
List<string> columns = new List<string>() { "Code", "Price", "Availability" };
IRow row = excelSheet.CreateRow(0);
foreach (var columnData in columns.Select((v, i) => new { Column = v, Index = i }).ToList())
{
row.CreateCell(columnData.Index).SetCellValue(columnData.Column);
}
codeCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@");
priceCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("#,##0.00");
availabilityStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@");
int rowIndex = 1;
foreach (var item in items)
{
row = excelSheet.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue(item.Code);
row.Cells[0].CellStyle = codeCellStyle;
if (item.HasData)
{
if (item.Price == "ON DEMAND")
{
row.CreateCell(1).SetCellValue(item.Price);
}
else
{
ICell priceCell = row.CreateCell(1);
row.Cells[1].SetCellType(CellType.Numeric);
row.Cells[1].CellStyle = priceCellStyle;
if (percentage != 0)
priceCell.SetCellValue(double.Parse(item.Price) + ((double)percentage / (double)100) * double.Parse(item.Price));
else
priceCell.SetCellValue(double.Parse(item.Price));
}
row.CreateCell(2).SetCellValue(item.Availability ? "True" : "False");
}
else
{
row.CreateCell(1).SetCellValue("");
row.CreateCell(2).SetCellValue("");
}
rowIndex++;
}当我有标价为"422,26“的项目时,它会产生价格值为42226,00的excel文件。当我有价格"380,00","4730,00“时,它可以正常工作,但是当我在字符串数中有小数部分时,就会出现问题。
我尝试了来自堆栈溢出的其他建议,但我没有运气,没有什么对我的例子有效。
发布于 2022-08-17 17:01:15
经过努力,这种格式解决了我的问题
"# #0.00"
https://stackoverflow.com/questions/68898693
复制相似问题