【问题标题】:Incorrect error data from excel来自excel的错误数据不正确
【发布时间】:2017-01-12 19:08:40
【问题描述】:

我正在使用 OLEDB 提供程序将数据从 excel 导入 mySql DB。当有一个带有 % 的字段时,它会自动除以 100。假设我有 2 列 Qty 和 Disc_Percentage,值为 4 和 25%。当我从 excel 表中获取数据到数据表时,它将 Disc_Percentage 值转换为 0.25。我有以下代码用于阅读 excel

 public static DataTable ConvertExcelFileDataToDataTable(string file, string extension)
            {
                var dtImportedData = new DataTable();

                // -- Start of Constructing OLEDB connection string to Excel file
                var props = new Dictionary<string, string>();

                // For Excel 2007/2010
                if (file.ToLower().EndsWith(".xlsx"))
                {
                    props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
                    props["Extended Properties"] = "\"Excel 12.0\"";
                }
                // For Excel 2003 and older
                else if (file.ToLower().EndsWith(".xls"))
                {
                    props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
                    props["Extended Properties"] = "\"Excel 8.0;IMEX=1;HDR=Yes\"";
                }
                else
                    return null;

                props["Data Source"] = file;

                var sb = new StringBuilder();

                foreach (KeyValuePair<string, string> prop in props)
                {
                    sb.Append(prop.Key);
                    sb.Append('=');
                    sb.Append(prop.Value);
                    sb.Append(';');
                }

                //You must use the $ after the object you reference in the spreadsheet
                var conn = new OleDbConnection(sb.ToString());
                conn.Open();
                var myCommand = new OleDbDataAdapter();
                DataTable dtSerialNumbers = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                if (dtSerialNumbers == null)
                {
                    return dtImportedData;
                }

                var excelSheets = new String[dtSerialNumbers.Rows.Count];

                // Add the sheet name to the string array.
                if (dtSerialNumbers.Rows.Count > 0)
                {
                    //we need the first sheet so save the first sheet name from the first row of the table
                    excelSheets[0] = dtSerialNumbers.Rows[0]["TABLE_NAME"].ToString();
                    myCommand = new OleDbDataAdapter("SELECT * FROM [" + excelSheets[0] + "]", conn);
                }

                myCommand.Fill(dtImportedData);
                dtImportedData.TableName = excelSheets[0].Replace("$", String.Empty);
                return dtImportedData;
            }

【问题讨论】:

  • 您的问题是什么?恕我直言,从数学 POV 来看这是绝对正确的:20 的 25% = 20 * (25/100) = 5
  • 是的,但是值应该是 25% 而不是 0.25。

标签: c# excel oledb


【解决方案1】:

Excel(我的机器上的 2016)自动检测输入的“%”并将单元格格式设置为百分比。我建议在导入之前重新格式化这些单元格。

【讨论】:

  • 当然不是,但是您可以强制程序执行此操作;)将此作为示例,了解如何识别格式link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 2020-11-07
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
相关资源
最近更新 更多