【发布时间】: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。