FernandoPaiva FernandoPaiva - 2 months ago 11
C# Question

Exception trying read an excel file by OleDb?

I'm trying read an excel file using

OleDb
but an exception is always throw. How could I do this ?

trying

private void btnRead_Click(object sender, EventArgs e) {
try {
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
FILE_PATH + ";Extended Properties=\\Excel 8.0;HDR=YES;IMEX=1\"");
String sql = "select * from [alunos.unid2.xls$]";
OleDbCommand command = new OleDbCommand(sql, conn);

conn.Open();
OleDbDataReader rs = command.ExecuteReader();
while (rs.NextResult()) {
//Console.WriteLine(rs["ALU_NOME"]);
strBuilder.Append(rs["ALU_NOME"]);
}
conn.Close();
}catch (Exception ex) {
Console.WriteLine(ex.Message);
}

}


Exception

Format of the initialization string does not conform to specification starting at index 130.

Answer
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FILE_PATH + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'"); 

You are missing opening quote for value of Extended Properties

Comments