Read Excel in C #

0

Dear that you have a good day, I am in a problem when I try to read an Excel xlsx format, I have done it before and it had never happened to me before. it turns out that it is a console application and when opening the Oledb connection it falls and says external table is not in the expected format but the weird is here. when I open my excel file it reads it without problems while the file is open. But when the file is closed it does not read it and it falls at the point I mentioned. Here is the extract of the code:

string path = Convert.ToString(dnom1);
string filename = Convert.ToString(Path.GetFileName(path));               
string filepath = @"C:\Users\joel.baez.silvestre\Desktop\Descargas editadas\Listos\TramoLineaLectura\Excel\" + filename;
string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=NO;""", filepath);            
Console.WriteLine("Archivo descargado y extraído exitosamente");
OleDbConnection Econ;
Econ = new OleDbConnection(constr);
string qTotal = string.Format("Select * from[Sheet1$]");
string qTotalvalores = string.Format("Select * from[Sheet1$B12:H755]");
OleDbCommand ETotal = new OleDbCommand(qTotal, Econ);
OleDbCommand Etotal2 = new OleDbCommand(qTotalvalores, Econ);

Econ.Open(); // en este punto se cae cuando el excel esta cerrado

// Creacion de Dataset para cada uno de los contructores
DataSet dsTotal = new DataSet();
DataSet dsTotal2 = new DataSet();
OleDbDataAdapter odaTotal = new OleDbDataAdapter(qTotal, Econ);
OleDbDataAdapter odaTotal2 = new OleDbDataAdapter(qTotalvalores, Econ);
Econ.Close();
odaTotal.Fill(dsTotal);
odaTotal2.Fill(dsTotal2);
    
asked by Joel Baez 29.08.2018 в 18:45
source

1 answer

0

I recommend using link

 XLWorkbook ArchivoExcel = new XLWorkbook(@"C:\Users\...\Desktop\calculo.xlsx");//ruta del libro excel
            var HojaExcel = ArchivoExcel.Worksheet(1);//hojas del libro


            foreach (IXLRow fila in HojaExcel.Rows())//recorrer las filas
            {

             var valor1 =  fila.Cell(1).GetValue<String>();//obtienes los valores //string o el tipo que desees

            }
    
answered by 29.08.2018 в 19:28