M
Maury Markowitz
I'm trying to write some generic code to use ODBC to load data from
various Excel files. I have this working fine for XLS, but I'd like to
expand it so it could also use tab- or comma-delimited files as well.
This isn't working, and the debugger can't find the source so...
Here is the code that fails. All inputs are valid and point to real
files in the correct format.
string cnn = GetExcelConnectionString(filename,
hasHeaderRow);
OleDbConnection conn = new OleDbConnection(cnn);
conn.Open();
If this code is called with an XLS is works fine. When I call it with
a CSV it fails inside the Open, claiming that the path is incorrect.
It is not. The code to build the string follows below.
Can anyone spot the problem?
Maury
=======
private static string GetExcelConnectionString(string filename, bool
hasHeaderRow)
{
string useHeaders = (hasHeaderRow) ? "Yes" : "No";
string type = Path.GetExtension(filename);
switch (type)
{
case ".xlsx":
return @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filename + @";Extended Properties=""Excel 12.0 Xml;HDR" + useHeaders +
@";""";
case ".xls":
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
filename + @";Extended Properties=""Excel 8.0;IMEX=1;HDR" + useHeaders
+ @";""";
case ".csv":
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
filename + @";Extended Properties=""TEXT;IMEX=1;HDR" + useHeaders +
@";""";
case ".tab":
case ".txt":
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
filename + @";Extended Properties=""TEXT;FMT=TabDelimited;IMEX=1;HDR"
+ useHeaders + @";""";
default:
return null;
}
}
various Excel files. I have this working fine for XLS, but I'd like to
expand it so it could also use tab- or comma-delimited files as well.
This isn't working, and the debugger can't find the source so...
Here is the code that fails. All inputs are valid and point to real
files in the correct format.
string cnn = GetExcelConnectionString(filename,
hasHeaderRow);
OleDbConnection conn = new OleDbConnection(cnn);
conn.Open();
If this code is called with an XLS is works fine. When I call it with
a CSV it fails inside the Open, claiming that the path is incorrect.
It is not. The code to build the string follows below.
Can anyone spot the problem?
Maury
=======
private static string GetExcelConnectionString(string filename, bool
hasHeaderRow)
{
string useHeaders = (hasHeaderRow) ? "Yes" : "No";
string type = Path.GetExtension(filename);
switch (type)
{
case ".xlsx":
return @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filename + @";Extended Properties=""Excel 12.0 Xml;HDR" + useHeaders +
@";""";
case ".xls":
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
filename + @";Extended Properties=""Excel 8.0;IMEX=1;HDR" + useHeaders
+ @";""";
case ".csv":
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
filename + @";Extended Properties=""TEXT;IMEX=1;HDR" + useHeaders +
@";""";
case ".tab":
case ".txt":
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
filename + @";Extended Properties=""TEXT;FMT=TabDelimited;IMEX=1;HDR"
+ useHeaders + @";""";
default:
return null;
}
}