Accessing an excel file throws OleDbException but keeps handle on file
Posted
by Jonn
on Stack Overflow
See other posts from Stack Overflow
or by Jonn
Published on 2010-05-19T00:02:54Z
Indexed on
2010/05/19
0:10 UTC
Read the original article
Hit count: 356
Really odd that I'd get an oledbexception but turns out that the file's handle is still with the original file. I've been searching through google and keep finding the same problem but no solutions.
Connection String:
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filePath + ";" +
"Extended Properties=Excel 8.0;";
Note that it works on every other file except a particular excel file.
Exception:
System.Data.OleDb.OleDbException: No error information available: E_UNEXPECTED(0x8000FFFF).
And then I have exception handling like this:
try {
IEnumerable<string> worksheetNames = GetWorkbookWorksheetNames(connString);
DataSet ds;
foreach (string worksheetName in worksheetNames)
{
OleDbDataAdapter dataAdapter =
new OleDbDataAdapter("SELECT * FROM [" + worksheetName + "]", connString);
ds = new DataSet();
dataAdapter.Fill(ds, "ExcelInfo");
DataTable dt = ds.Tables["ExcelInfo"];
entityList.AddRange(GetDataFromDataTable(dt, worksheetName));
}
}
catch (OleDbException ex)
{
File.Move(filePath, filePath + ".invalidFormat.xls");
}
Has anyone else encountered this behavior? And I'm not sure how to handle an error that keeps the handle on the file I'm supposed to process. It sort of freezes everything in place.
© Stack Overflow or respective owner