EPPlus 号码格式
EPPlus number format
我有一个用 Epplus 生成的 Excel sheet,我遇到了一些痛点,我希望得到解决过类似挑战的人的指导。
我需要将数字格式应用到双精度值,我想像这样在 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 sheet 的格式单元格时,我看到我的格式显示为 ##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_-;_-$* \"-\"??_-;_-@_-";
Don't change the decimal and thousand separators to your own
localization. Excel will do that for you.
通过请求一些日期时间格式设置选项。
//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";
除了已接受的答案之外,因为值 Accept Object 如果您的输入是字符串,您必须将 Number 传递给 Value For Example :
var input = "5";
ws.Cells["A1:A25"].Value = double.Parse(input);
已接受答案的另一个补充:您可以使用可为 null 的值并且格式看起来都不错但它最终成为 Excel 中的字符串并且您不能 SUM、AVG 等
因此请确保您使用可为 null 的实际 Value
。
如果您想将“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
}
我按如下方式解决了它,所以我只是加载模型并根据我的模型进行更改,如果它是 int
或 datetime
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";
}
}
我有一个用 Epplus 生成的 Excel sheet,我遇到了一些痛点,我希望得到解决过类似挑战的人的指导。
我需要将数字格式应用到双精度值,我想像这样在 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 sheet 的格式单元格时,我看到我的格式显示为 ##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_-;_-$* \"-\"??_-;_-@_-";
Don't change the decimal and thousand separators to your own localization. Excel will do that for you.
通过请求一些日期时间格式设置选项。
//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";
除了已接受的答案之外,因为值 Accept Object 如果您的输入是字符串,您必须将 Number 传递给 Value For Example :
var input = "5";
ws.Cells["A1:A25"].Value = double.Parse(input);
已接受答案的另一个补充:您可以使用可为 null 的值并且格式看起来都不错但它最终成为 Excel 中的字符串并且您不能 SUM、AVG 等
因此请确保您使用可为 null 的实际 Value
。
如果您想将“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
}
我按如下方式解决了它,所以我只是加载模型并根据我的模型进行更改,如果它是 int
或 datetime
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";
}
}