我有一个用EPPLUS生成的Excel表格,我正在经历一些痛点,我希望由解决了类似挑战的人来指导.

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

  • 8.→ 8
  • 12→ 12
  • 14.54→14.5
  • 0→ 0

这是我的密码

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

最终的Excel文件始终将E+0附加到此格式的末尾,因此会以如下方式显示最终值.

  • 8.→ 8E+0
  • 12→ 12E+0
  • 14.54→14.5E+0
  • 0→ 000.0E+0

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

怎么了?

推荐答案

以下是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";

Asp.net相关问答推荐

是否可以发布 ASP.NET 5 应用程序以使目标机器不需要安装 DNX?

如何使多行的 TextBox 控件无法调整大小?

为 Web 请求实现速率限制算法的最佳方法是什么?

VS2013发布Web部署任务失败文件正在使用中

如果站点在 localhost 或 127.0.0.1 上运行,如何判断 ASP.NET MVC 视图

在 Asp.net 中通过 Button 的 CommandArgument 传递多个参数

使用 gridview asp.net 进行排序和分页

没有回发的按钮?

使用 JObject 所需的库名称是什么?

确定哪个 w3wp.exe 进程属于 Windows 7 / IIS7.5 中的哪个 App Pool?

如何从 NuGet 安装 EntityFramework 5.0(和其他旧版本)?

如何使div按钮提交其所在的表单?

如何在 GridView 中隐藏 TemplateField 列

MVC 模型布尔显示是或否

主机与 DnsSafeHost

我可以在 .NET Framework 4.0 上运行 MVC 5 应用程序吗?

回发后运行javascript函数

将 HTML 字符串转换为图像

ASP.net 判断页面是 http 还是 https

强制 IIS Express 进入classic 管道模式