0

I trying to read Excel file which is Html type with C# code. I'm getting an 'Unspecified error'.

This is my connection string:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='HTML Import;  // c:\1.xls

This is my code:

private string GetTableName(OleDbConnection conn)
{
   string tableName = null;
    try
    {
       conn.Open();

       var dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
       if (dt == null)
       {
          log.Error("Table schema is not available.");
          return tableName;
       }

       tableName = dt.Rows[0]["TABLE_NAME"].ToString();
    }
    catch (Exception e)
    {
       log.Warn(e);
       return null;
    }
    finally
    {
       conn.Close();
    }
    return tableName;
}

I looked all over the Internet and Google and nobody had exactly the same issue.

I would like to understand what is wrong with my code or what does it mean the 'Unspecified Error'?!

Thanks !!!

2
  • If i remember correctly one of the windows xp service packs changed security settings around the jet driver and reading html tables but i can't find the document on the msdn i'm thinking of. It basically restricted the ability to read html tables. If i find it i'll come back and post the link. good luck Commented Oct 9, 2011 at 15:42
  • check out support.microsoft.com/kb/240770 theres a section that talks about 'DisabledExtensions' Commented Oct 9, 2011 at 16:16

2 Answers 2

1

Try using

 Provider=Microsoft.ACE.OLEDB.12.0;

Edit:

Use this connection string

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";"

HDR=Yes means that the header is considered as a data row and not column names (Change it depending on your needs)

IMEX=1 specify that the table contains mixed data

Sign up to request clarification or add additional context in comments.

Comments

0

for Excel 2007 change connection string as

Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES';

ACE OLEDB 12.0 was released with Office 2007. It is possible to use the Microsoft.ACE.OLEDB.12.0 to connect to older .xls (Excel 97-2003) workbooks as well.

2 Comments

Hi, now i get "External table is not in the expected format."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.