尝试读取OleDb的excel文件的异常?

时间:2016-09-18 04:49:31

标签: c#

我正在尝试使用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.

1 个答案:

答案 0 :(得分:1)

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

的开头报价