There is a program that reads an Excel file using OLEDB and converts it to a tab delimited text file. This loop handles the conversion.
While reader.Read
Dim count As Integer = 0
Try
While (True)
temp = reader(count).ToString + ControlChars.Tab
output_file.Write(temp)
count += 1
End While
Catch ex As Exception
output_file.WriteLine()
End Try
End While
Not the most elegant code for sure, but it reads in each cell one by one, then throws an exception at the end of the row to write a newline. The only problem is that it skips the first line of the Excel document. Any idea why?
For a more elegant solution, try this:
Read each line from the reader, and process the fields in the row in a FOR loop, using the number of fields in the row (.FieldCount).
After each FOR loop is complete, end the line in the file.
Repeat for each row in the reader.
It’s good practice to not use exceptions to handle events in code that can otherwise be handled (e.g., throwing a handling a FileNotFoundException – or throwing one – rather than simply doing a File.Exits check).\
UPDATE
Did a little googling, and it appears that you can include/exclude the header via the connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";Note the HDR=Yes part. According to Connection strings for Excel this is used to indicate whether or not the first row is the header.
Another thing you might try is seeing if the reader’s fields have values in their Name property, using reader.GetName(int index), where index is the zero-based ordinal of the column (field).