C # 3.5 Reading Excel Files

I have strange behavior when I try to read an XML worksheet using this code:

string CONNEC_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=No;IMEX=2;\"";
            string fullFilePath = @"C:\Tmp\TestFile.xls";
            using (OleDbConnection objCon = new OleDbConnection(string.Format(CONNEC_STRING, fullFilePath)))
            {
                using (OleDbCommand cm = new OleDbCommand("Select * From [MYCELLSRANGE]", objCon))
                using (OleDbDataAdapter da = new OleDbDataAdapter(cm))
                {
                    DataTable dt = new DataTable();
                    objCon.Open();
                    da.Fill(dt);
                    objCon.Close();
                }
            }

If the Excel file is closed, I get the error message "The external table is not in the expected format." When I open the file, if I execute the above code, it works fine and I can read the data contained in MYCELLSRANGE. So, does anyone have any ideas about this issue? Thanks for answers.

+3
source share
1 answer

, Jet Engine Excel . . Excel.dll Excel, . Jet , . .

, . VB.NET .

+2

Source: https://habr.com/ru/post/1761335/


All Articles