【问题标题】:EPPlus number formatEPPlus 编号格式
【发布时间】:2016-10-24 01:42:45
【问题描述】:

我有一个用 Epplus 生成的 Excel 表,我遇到了一些痛点,我希望得到解决过类似挑战的人的指导。

我需要将数字格式应用于双精度值,并且我想像这样在 Excel 中呈现它。

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

这是我的代码

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

最终的 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

可能出了什么问题?

【问题讨论】:

  • ##0.0 用于货币格式。 “0.00”是数字格式

标签: c# asp.net .net excel epplus


【解决方案1】:

以下是 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_-;_-$* \"-\"??_-;_-@_-";

请勿将小数和千位分隔符更改为您自己的 本土化。 Excel 会为您做到这一点。

通过请求一些日期时间格式选项。

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

【讨论】:

  • 日期时间格式是否有类似的参考。我正在尝试读取格式指定为 dd-mm-yy h:mm 的文件,但在调试中我看到的格式为:“[$-10409]m/d/yyyy\\ h:mm:ss\\上午/下午”
  • 添加了一些例子,但基本上和数字一样。
  • Nvm,找到了。不仅要使用符号,还必须转义多个字符。对于 CHF,它可能如下所示:_ \"CHF\" * # ##0.00_ ;_ \"CHF\" * -# ##0.00_ ;_ \"CHF\" * \"-\"??_ ;_ @_
  • Don't change the decimal and thousand separators to your own localization. Excel will do that for you. 那个!尝试“修复”小数是错误和破坏公式的巨大来源!
  • 如果您想将单元格格式化为文本,请设置Style.Numberformat.Format = "@";
【解决方案2】:

除了接受的答案,因为值接受对象,如果您的输入是字符串,您必须将数字传递给值:

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

【讨论】:

    【解决方案3】:

    对已接受答案的另一个补充:您可以使用可为空的值,并且格式看起来都不错,但它最终会成为 Excel 中的字符串,您不能 SUM、AVG 等。

    所以请确保您使用可空值的实际Value

    【讨论】:

    • 值可以为空是什么意思?
    • 说你有 Decimal? myDecimal = 0.4M; 然后要在 Excel 中得到一个正确的小数(不仅仅是一个字符串)你应该做 ws.Cells["A5"].Value = myDecimal.Value; 所以你通过添加“.Value”向 Excel 发送一个不可为空的小数"
    【解决方案4】:

    如果您想将特定列(如“B”列)格式化为数字格式,您可以这样做-

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      相关资源
      最近更新 更多