EPPlus数字格式

Ton*_*nto 49 .net c# asp.net excel epplus

我有一张用Epplus生成的excel表,我遇到了一些痛点,我希望由一个解决了类似挑战的人指导.我需要将数字格式应用于double值,我想在excel中将它呈现为这样.

  • 8 - > 8.0
  • 12 - > 12.0
  • 14.54 - > 14.5
  • 0 - > 0.0

这是我的代码

ws.Cells[row, col].Style.Numberformat.Format = "##0.0";
Run Code Online (Sandbox Code Playgroud)

最终的excel文件总是将E + 0附加到此格式的末尾,因此会显示最终值.

  • 8 - > 8.0E + 0
  • 12 - > 12.0E + 0
  • 14.54 - > 14.5E + 0
  • 0 - > 000.0E + 0

当我检查生成的Excel工作表的格式单元格时,我看到我的格式显示为## 0.0E + 2而不是我应用的## 0.0.可能有什么不对?

VDW*_*WWD 114

以下是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_-;_-$* \"-\"??_-;_-@_-";
Run Code Online (Sandbox Code Playgroud)

不要将小数和千位分隔符更改为您自己的本地化.Excel会为您做到这一点.

通过请求一些DateTime格式化选项.

//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";
Run Code Online (Sandbox Code Playgroud)

  • `不要将小数点和千位分隔符更改为您自己的本地化。Excel 会为您做到这一点。` 那!试图“修复”小数是错误和破坏公式的巨大来源! (3认同)
  • 如果要将单元格设置为文本格式,请设置“ Style.Numberformat.Format =“ @”;` (3认同)