0

从excel文件中读取而不丢失数据的最佳方法是什么?Excel 工作表的列格式为 HH:MM:SS,当使用 ADO 读取时,时间格式更改为日期并丢失实际值:

来自excel的样本: 在此处输入图像描述

导入网格视图后: 在此处输入图像描述

代码:

        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

为什么进口时间后失去价值?

4

1 回答 1

0

听起来好像您想显示 TimeSpan 而不是日期。Excel 将单元格的值存储为日期时间,并使用格式来控制其显示方式。

您要做的是将 DataTable 中列的数据类型更改为 TimeSpan

查看此处了解如何执行此操作:如何更改 DataTable 中 DataColumn 的 DataType?

于 2013-03-14T22:11:19.830 回答