Re: How to connect to .CSV file ?



fniles wrote:
I have a .CSV file (comma delimited) that I want to open using OLEDB, but I get the error "External table is not in the expected format."
If I save the .CSV file to an .XLS file, I can open the connection with no problem.
What is the correct way to open a .CSV file ?
If I can not open the CSV file, how can I programmatically save the CSV file to an XLS file ?
Thanks a lot.

dim myCon OleDb.OleDbConnection
myCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\file.csv; Extended Properties=""Excel 8.0; HDR=NO; IMEX=1""")
--> error "External table is not in the expected format."



Hello,

A very simple way to open and read the contents of a *.CSV file is with System.IO.StreamReader.

Let me show you an example in C#:

// You first create a StreamReader to read the file
System.IO.StreamReader myReader = new System.IO.StreamReader("c:\\myFile.csv");

//Let say you want to store all the read contents into a string variable
string myString;

//test if the reader had reached the end of the stream or not
while (myReader.Peek != -1)
{
/*
Here you will have each line of string read from the file put int-o the myString variable. You can start parse your results into an array, or anything else.
*/
myString = myReader.ReadLine();
}

Hope it will help you.

Have a nice day.
.



Relevant Pages

  • Re: How to open a .CSV file ?
    ... string within it, so using string methods on this string is hardly "stupid". ... I assume when you said "parse at the comma" you meant string.split. ... it would be stupid to use it on a CSV file. ... If I save the .CSV file to an .XLS file, ...
    (microsoft.public.dotnet.languages.vb)
  • Hidden rows still visuale under CSV
    ... The file Extension .CSV stands for Comma Separated Values. ... When you save an Excel .xls file as a .CSV file only the ...
    (microsoft.public.excel.misc)
  • Re: Read from XLS and substitute values in Ruby (Watir)
    ... Also,Is there a way to read from csv file or xls file because currently ... tokens as separate parameters for the text fields. ... Let's assume that tabs are used instead of spaces between the fields. ...
    (comp.lang.ruby)
  • Re: How to open a .CSV file ?
    ... but a string within it, so using string methods on this string is ... I assume when you said "parse at the comma" you meant string.split. ... If I save the .CSV file to an .XLS file, ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Convert .CSV format to .XLS format in C#
    ... But how it is usfull in converting .csv file to .xls file. ... I used delimited and set a special delimiter character | to match your data. ... .TextFilePromptOnRefresh = False ...
    (microsoft.public.excel.programming)