EPPlus number format

被刻印的时光 ゝ 提交于 2019-11-26 09:22:28

问题


I have an excel sheet generated with Epplus, I am experiencing some pain points and i wish to be directed by someone who have solved a similar challenge. I need to apply number formatting to a double value and i want to present it in excel like this.

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

Here is my code

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

The final excel file always append E+0 to the end of this format and therefore presents the final values like this instead.

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

When i check in the format cells of the generated excel sheet, i see that my format appears as ##0.0E+2 instead of ##0.0 that i applied. What may be wrong?


回答1:


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";


来源:https://stackoverflow.com/questions/40209636/epplus-number-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!