EPPlus 数字格式

我有一个 Excel 表生成的 Epplus,我正在经历一些痛点,我希望由谁解决了类似的挑战,我希望有人指导。

我需要对一个双精度值应用数字格式,并且我想像这样在 Excel 中显示它。

  • 8→8.0
  • 12→12.0
  • 14.54→14.5
  • 0.0

这是我的密码

ws.Cells[row, col].Style.Numberformat.Format = "##0.0";

最终的 Excel 文件总是将 E + 0附加到这种格式的末尾,因此呈现的最终值如下所示。

  • 8→8.0 E + 0
  • 12→12.0 E + 0
  • 14.54→14.5 E + 0
  • 0.000.0 E + 0

当我检入生成的 Excel 表格的格式单元格时,我看到我的格式显示为 ##0.0E+2,而不是我应用的 ##0.0

可能出了什么问题?

109142 次浏览

Here are some number format options for EPPlus:

//integer (not really needed unless you need to round numbers, Excel will use default cell properties)
ws.Cells["A1:A25"].Style.Numberformat.Format = "0";


//integer without displaying the number 0 in the cell
ws.Cells["A1:A25"].Style.Numberformat.Format = "#";


//number with 1 decimal place
ws.Cells["A1:A25"].Style.Numberformat.Format = "0.0";


//number with 2 decimal places
ws.Cells["A1:A25"].Style.Numberformat.Format = "0.00";


//number with 2 decimal places and thousand separator
ws.Cells["A1:A25"].Style.Numberformat.Format = "#,##0.00";


//number with 2 decimal places and thousand separator and money symbol
ws.Cells["A1:A25"].Style.Numberformat.Format = "€#,##0.00";


//percentage (1 = 100%, 0.01 = 1%)
ws.Cells["A1:A25"].Style.Numberformat.Format = "0%";


//accounting number format
ws.Cells["A1:A25"].Style.Numberformat.Format = "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* \"-\"??_-;_-@_-";

Don't change the decimal and thousand separators to your own localization. Excel will do that for you.

By request some DateTime formatting options.

//default DateTime pattern
worksheet.Cells["A1:A25"].Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;


//custom DateTime pattern
worksheet.Cells["A1:A25"].Style.Numberformat.Format = "dd-MM-yyyy HH:mm";

Addition to Accepted Answer, because value Accept Object you must pass Number to Value For Example if your input is in string :

var input = "5";
ws.Cells["A1:A25"].Value = double.Parse(input);

Another addition to the accepted answer: you can use nullable values and the formatting all looks good BUT it ends up being a string in Excel and you can't SUM, AVG etc.

So make sure you use the actual Value of the nullable.

And if you want to format a specific column like column "B" to number format you can do it this way-

using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("SHEET1");
worksheet.Cells["A1"].LoadFromDataTable(dataTable, PrintHeaders: true);
for (var col = 1; col < dataTable.Columns.Count + 1; col++)
{
if (col == 2)//col number 2 is equivalent to column B
{
worksheet.Column(col).Style.Numberformat.Format = "#";//apply the number formatting you need
}
worksheet.Column(col).AutoFit();
}
return File(package.GetAsByteArray(), XlsxContentType, "report.xlsx");//downloads file
}

I solved it as follows, so I just load the model and change as per my model if it is int ordatetime

var li = typeof(Model)
.GetProperties()
.ToArray();




using (var package = new ExcelPackage(stream))
{
var workSheet = package.Workbook.Worksheets.Add("Sheet1");


var i = 0;
foreach (var c in li)
{
i++;
if(c.PropertyType.Name == typeof(DateTime).Name || c.PropertyType.Name == typeof(DateTime?).Name)
workSheet.Column(i).Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern; ;


if (c.PropertyType.Name == typeof(int).Name || c.PropertyType.Name == typeof(int?).Name)
workSheet.Column(i).Style.Numberformat.Format = "0";
}


}