
如圖,這個加油時間是有秒值的,但通過代碼獲取后,讀不到秒。只到分鐘。這樣導致后面的計算會出問題,要讀到秒才行。
讓用戶改文本格式不太現實。
有什么方法可以解決這個問題?
代碼:
public static DataSet ExcelToDS (string file)
{
//string strConn = "Provider=Microsoft.Jet.OLEDB.12.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;HDR=NO;IMEX=1';";
string connstring = "";
string filetype = file.Substring(file.LastIndexOf('.') + 1);
DataSet dt = null;
if (filetype.ToLower() == "xlsx")
{
// Office 07及以上版本 不能出現多余的空格 而且分號注意
connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
}
else //if (filetype.ToLower() == "xls")
{
//Office 07以下版本
connstring = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
}
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//string tableName = schemaTable.Rows[0][2].ToString().Trim();
string tableName = "sheet1$";
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [" + tableName + "]";
myCommand = new OleDbDataAdapter(strExcel, connstring);
dt = new DataSet();
myCommand.Fill(dt, "table1");
conn.Close();
return dt;
}
}
uj5u.com熱心網友回復:
換個方式,用npoi讀試試uj5u.com熱心網友回復:
用npoi會經常出現版本不匹配,所以才換成OLEDB。
uj5u.com熱心網友回復:
不行 在Excel里面 你就把他全部顯示出來呢,你在寫入Excel的時候 日期轉為string型別試試,如果是string 轉為DateTime型別試試轉載請註明出處,本文鏈接:https://www.uj5u.com/net/41587.html
標籤:C#
