开发者

VB.NET - Reading a blank string from Excel when trying to read the header of a data column?

开发者 https://www.devze.com 2023-03-30 22:34 出处:网络
A simple though very odd problem. I use an OLEDB connection to read from an excel database, and this loop to read in all of the data from each of the columns

A simple though very odd problem. I use an OLEDB connection to read from an excel database, and this loop to read in all of the data from each of the columns

While reader.Read()

            For i As Integer = 0 To reader.FieldCount - 1
           开发者_如何学C     temp = reader(i).ToString + ControlChars.Tab
                output_file.Write(temp)
                'output_file.Write(reader(i).ToString() + ControlChars.Tab)
            Next

            output_file.WriteLine()
        End While

Some of the columns contain date information, which are read in fine (usually as a string "2/20/2011" or so), but the headers of those columns are read in as a blank "". The headers for all the other columns read in fine, but not for the date containing columns. Any idea how I can fix this?


Is it because OLEDB is inferring type for the date columns (DateTime or whatever) and the headers to do not conform to this type? I've had similar issues with ODBC ignoring the odd alpha string in a column that is otherwise numeric.


Well here's the solution, which I stumble across accidentally. Your connection string needs "IMEX=1;" in it, which tells the reader that all data types should be read as strings.

Dim jet_string As String = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + input_file_path + ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1;"""
0

精彩评论

暂无评论...
验证码 换一张
取 消