0
我用這個功能來讀取Excel文件數據表:C#:讀取Excel文件單元格的格式未能像數
public static DataTable exceldata(string filePath)
{
try
{
DataTable dtexcel = new DataTable();
bool hasHeaders = false;
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
DataRow schemaRow = schemaTable.Rows[0];
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
string query = "SELECT * FROM [" + sheet + "]";
OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn);
dtexcel.Locale = CultureInfo.CurrentCulture;
daexcel.Fill(dtexcel);
}
conn.Close();
return dtexcel;
}
catch (Exception ex)
{
return null;
}
}
我的問題是:當Excel文件中有一些細胞格式看起來像號碼(例如:301/1,181/3 ....),數據表在該單元格中返回空值。
如何將excel文件讀取到數據表格中作爲字符串的單元格,而不是檢測到數字?
另一個問題嘗試格式化底層Excel作爲文本 –
你可以附加電子表格(透過Dropbox或G-驅動器或東西)?我相信你,但我從來沒有見過也不能複製這個問題。 – Hambone