2013-03-14 27 views
0

什麼是從Excel文件中讀取而不丟失數據的最佳方法?當使用ADO讀取時間格式更改爲日期和失去實際價值SS:從Excel從excel文件讀取而不丟失數據的最佳方法是什麼? Vb.net

樣品: Excel工作表已列格式化HH:MM enter image description here

進口到網格視圖後: enter image description here

代碼:

 Try 
     ' Clear the DataGridView and the connection string TextBox 
     Dim Datatable As New DataTable("Sometable") 
     ' Fill the DataGridView and connection string TextBox 
     Using connection As New OleDb.OleDbConnection(Super.Excel_GetConnectionString(txtZDCalc_txtPerPath.Text, txtZDCalc_txtPerPath.Tag)) 
      connection.Open() 
      Using adapter As New OleDb.OleDbDataAdapter("SELECT * FROM [" & cboxZDCalc_Sheet.SelectedItem & "]", connection) 
       adapter.Fill(Datatable) 
      End Using 
     End Using 

     Super.ZD_GetEmployees(Datatable) 
    Catch ex As Exception 
     ' Display any errors 
     MessageBox.Show("[" & ex.[GetType]().Name & "] " & ex.Message & ex.StackTrace) 
    End Try 

爲什麼導入時間後失去了價值?

回答

0

聽起來好像您要顯示TimeSpan而不是日期。 Excel將單元格的值存儲爲日期時間,並使用格式來控制其顯示方式。

你想要做的是改變列的數據類型在你的數據表,以時間跨度

看一看這裏瞭解如何做到這一點:How To Change DataType of a DataColumn in a DataTable?

相關問題