Now it works only when the file is open. Here is the connection string:

const string oleConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=No\";Data Source={0}"; 

like this I open it as standard

 var oleConnStr = String.Format(oleConn, fileName); OleDbConnection cn = new OleDbConnection(oleConnStr); cn.Open(); 

Mistake:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

The external table has no intended format.

2 answers 2

Do not suffer and install ( Install-Package ExcelDataReader ) library . Then everything is very simple:

 // Создаем поток для чтения. var stream = File.Open("путь к файлу", FileMode.Open, FileAccess.Read); // В зависимости от расширения файла Excel, создаем тот или иной читатель. // Читатель для файлов с расширением *.xlsx. var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); // Читатель для файлов с расширением *.xls. var excelReader = ExcelReaderFactory.CreateBinaryReader(stream); // Читаем, получаем DataSet и работаем с ним как обычно. var result = excelReader.AsDataSet(); // После завершения чтения освобождаем ресурсы. excelReader.Close(); 

PS If you need to explicitly use Microsoft.Office.Interop.Excel , let me know.

    It works for me. Excel even open is not necessary. But you need to connect the library Microsoft.Office.Interop.Excel

    Here is the code:

     public void Excel(DataGridView datagrid) { var ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { var path = System.IO.Path.GetFullPath(ofd.FileName); const string querry = "SELECT * FROM [Лист1$]"; var conn = new OleDbConnection { ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = '" + path + "'" + @";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""" }; var fag = new OleDbDataAdapter(querry, conn) {AcceptChangesDuringFill = false}; fag.Fill((DataTable) _dt1); datagrid.DataSource = _dt1; } else { ofd.Dispose(); } }