Two programs trying to read excel one succeeds one fails with same code

83 Views Asked by At

I have two programs unrelated to each other but they have an identical method where they try to read lines from an excel file.

Program 1

    public DataTable GetExcelInfo(string filepath)
    {
        DataTable datatab = new DataTable();
        try
        {
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\";
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("SELECT Format([F1], \"#\"), Format([F2], \"#\"), Format([F3], \"#\") FROM [Sheet1$]", conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string[] values = new string[3];                        
                    values[0] = reader.GetString(0);
                    values[1] = reader.GetString(1);
                    values[2] = reader.GetString(2);

                    DataRow dr = datatab.NewRow();
                    dr.ItemArray = values;
                    datatab.Rows.InsertAt(dr, i);
                    i++;
                }
            }
        }
    }

Program 2

private static DataTable GetInvoiceItems(string filepath)
{
        DataTable dt = new DataTable();
        string excelConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Excel=8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\";
        using (OleDbConnection conn = new OleDbConnection(excelConString))
        {
            OleDbDataAdapter ada = new OleDbDataAdapter("SELECT [F1], [F2], [F3], [F4], [F5] FROM [Sheet1$]", conn);
            conn.Open();
            ada.Fill(dt);
        }
        return dt;
}

Now the weird thing is the first program works perfectly run on the same pc, while the second program gets an error saying there is no installable ISAM on the pc. Any suggestions?

1

There are 1 best solutions below

1
On BEST ANSWER

Despite your assertions they're both the same, if you look closely they're not. Take a look at your connection strings...

Program 1

"... Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\"

Program 2

"... Excel=8.0;IMEX=1;HDR=NO;TypeGuessRows=1;ImportMixedTypes=Text\\"

There should be no = in Excel 8.0 in Program 2.