尝试通过OleDb读取excel文件时出现异常

本文关键字:文件 异常 excel 读取 OleDb | 更新日期: 2023-09-27 18:07:30

我正在尝试使用OleDb读取excel文件,但总是抛出异常。我怎么能这么做呢?

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);
            }
        }

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

尝试通过OleDb读取excel文件时出现异常

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

您缺少Extended Properties值的开引号