c# - 使用 EPPlus 在没有科学记数法的情况下写入 Excel

标签 c# excel epplus

我有一个非常简单的问题。我有一个 C# 应用程序,它执行一些数据处理,然后使用 EPPlus 输出到 excel 文件。问题是我的一些数字很长,例如,数据值可能是20150602125320,在excel表中变成2.01506E+13。我正在努力避免这种情况。将值转换为字符串是可行的,但我需要它们作为数字,以便在 excel 中进行进一步的操作。任何人都知道我可以在没有科学记数法的情况下以原始形式保留数据的任何方法吗?谢谢!

最佳答案

您必须设置格式字符串以匹配您要查找的内容:

[TestMethod]
public void Big_Number_Format()
{
    //http://stackoverflow.com/questions/31124487/write-to-excel-without-scientifc-notation-using-epplus
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var package2 = new ExcelPackage(existingFile))
    {
        var ws = package2.Workbook.Worksheets.Add("Sheet1");
        ws.Cells[1, 1].Value = 20150602125320;
        ws.Cells[1, 2].Value = 20150602125320;

        ws.Cells[1, 1].Style.Numberformat.Format = "0";
        ws.Cells[1, 2].Style.Numberformat.Format = "0.00";
        package2.Save();
    }
}

如果您想查看在 Excel 功能区中看到的“内置”格式的一般列表,您可以在此处查看它们,但您无法直接在应用程序中访问它们,因为它们被标记为 internal:

https://github.com/JanKallman/EPPlus/blob/master/EPPlus/Style/XmlAccess/ExcelNumberFormatXml.cs#L62

/// <summary>
/// Id for number format
/// 
/// Build in ID's
/// 
/// 0   General 
/// 1   0 
/// 2   0.00 
/// 3   #,##0 
/// 4   #,##0.00 
/// 9   0% 
/// 10  0.00% 
/// 11  0.00E+00 
/// 12  # ?/? 
/// 13  # ??/?? 
/// 14  mm-dd-yy 
/// 15  d-mmm-yy 
/// 16  d-mmm 
/// 17  mmm-yy 
/// 18  h:mm AM/PM 
/// 19  h:mm:ss AM/PM 
/// 20  h:mm 
/// 21  h:mm:ss 
/// 22  m/d/yy h:mm 
/// 37  #,##0 ;(#,##0) 
/// 38  #,##0 ;[Red](#,##0) 
/// 39  #,##0.00;(#,##0.00) 
/// 40  #,##0.00;[Red](#,##0.00) 
/// 45  mm:ss 
/// 46  [h]:mm:ss 
/// 47  mmss.0 
/// 48  ##0.0E+0 
/// 49  @
/// </summary> 

关于c# - 使用 EPPlus 在没有科学记数法的情况下写入 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31124487/

相关文章:

c# - 如何使用 EPPlus 在同一张图表上绘制 2 种图表

c# - EPPlus 生成的 Excel 电子表格

c# - 我可以密码加密 SQLite 数据库吗?

c# - 如何在 C# 中执行#ifdef

excel - 将 Excel 电子表格导入 PostgreSQL

excel - 使用 Office.js 从 Excel 加载项创建 Word 文档

c# - 通过 EPPlus 调用 VBA 宏

c# - 搜索引擎。使用asp.net并从MySql检索数据

c# - 如何从签名的 C# exe 中读取公钥

excel - 无法使用 python 将 Excel 文件附加到电子邮件 - TypeError : expected bytes-like object, 不是工作簿