【问题标题】:C# get excel data in exponential formatC#以指数格式获取excel数据
【发布时间】:2011-09-23 05:19:24
【问题描述】:

Excel 已控制格式,如 here 中所述

在我的 excel 示例数据(字母数字)中:-

  • P000213590-A
  • 312700133751-->显示为3.127E+11

    在我的示例代码中:-

    DataTable dt = new DataTable("PartUpload");
    
    DataColumn column;
    
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.AllowDBNull = true;
    column.ColumnName = "Part Original";
    dt.Columns.Add(column);
    
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.AllowDBNull = true;
    column.ColumnName = "Part_ToString";
    dt.Columns.Add(column);
    
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.AllowDBNull = true;
    column.ColumnName = "Part_TryParse";
    dt.Columns.Add(column);
    
    string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\seahc1\Desktop\test1.xls;Extended Properties=ImportMixedTypes=Text;Excel 8.0;HDR=Yes;IMEX=1";
    
    OleDbConnection objConn = new OleDbConnection(connStr);
    objConn.Open();
    
    OleDbDataAdapter adap = new OleDbDataAdapter(@"SELECT Part as [Part Original]FROM [Sheet1$]", connStr);
    
    adap.Fill(dt);
    
    foreach (DataRow row in dt.Rows)
    {
        row["Part_ToString"] = row["Part Original"].ToString();
    
        double doubleConverResult;
        bool result = double.TryParse(row["Part Original"].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out doubleConverResult);
        if (result)
        {
            row["Part_TryParse"] = doubleConverResult.ToString();
        }
    }
    

    Part_ToString = 指数 = "3.1270013375e+011"
    Part_TryParse = 四舍五入=312700133750

    我调试并发现adap.Fill(dt) 用指数数据填充数据表。

    我如何通过 C# 程序获得确切的值,因为我不希望最终用户格式化他们的 excel 电子表格。

    请指教,谢谢。

  • 【问题讨论】:

    • 呃,什么?问题没有任何意义...
    • 显然存在语言障碍,我们无法理解您想要的内容。

    标签: c# excel tostring tryparse exponential


    【解决方案1】:

    我明白水晶是什么意思了。

    创建一个 Excel 文档并将 312700133751 数字放入单元格中。单元格的格式必须是“常规”。

    Excel 将显示 3.127E+11 而不是 312700133751。

    问题在于,当您使用 OleDbConnection 驱动程序读取文件时,DataTable 将填充“3.1270013505e+011”字符串值(即使 Excel 具有“常规”格式)。

    问题是 3.1270013505e+011 等于 312700133750,而不是 312700133751(注意最后一位,是零而不是一)。这就是问题! OleDbConnection 将数字的小数“截断”最多 10 位,这会在转换中留下一个额外的数字。

    我也没有找到解决办法……

    【讨论】:

      猜你喜欢
      • 2012-11-15
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多