Excel 已控制格式,如下所述
在我的 excel 样本数据(字母数字)中: -
在我的示例代码中: -
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 电子表格。
请指教,谢谢。