0

So I'm working on this thing for work, that converts an excel list of instructions into a better looking, formatted word document. I've been connecting to the excel document and then storing the file into a datatable for easier access.

I had just finally gotten the borders and stuff right for my word document when i started getting an error: External table is not in the expected format.

Here is the full connection algorithm:

 public static DataTable getWorkSheet(string excelFile =
            "C:\\Users\\Mitch\\Dropbox\\Work tools\\Excel for andrew\\Air Compressor PM's.xlsx") {
            string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile
                                + ";Extended Properties='Excel 8.0;HDR=YES;'";
            string sql = null;
            string worksheetName = null;
            string[] Headers = new string[4];
            DataTable schema = null;
            DataTable worksheet = null;
            DataSet workbook = new DataSet();
            //Preparing and opening connection
            OleDbConnection objconn = new OleDbConnection(connection);
            objconn.Open();
            //getting the schema data table
            schema = objconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            worksheetName = schema.Rows[0]["Table_Name"].ToString();
            //Each worksheet will have a varying name, so the name is just called from
            //the dataTable.rows array. Can be later modified to use multiple
            //worksheets within a workbook.
            sql = "SELECT * FROM[" + worksheetName + "]";

            //data adapter
            OleDbDataAdapter objAdapter = new OleDbDataAdapter();
            //pass the sql
            objAdapter.SelectCommand = new OleDbCommand(sql, objconn);
            //populate the dataset
            objAdapter.Fill(workbook);
            //Remove spaces from the headers.
            worksheet = workbook.Tables[0];
            for (int x = 0; x < Headers.Count(); x++) {
                Headers[x] = worksheet.Columns[x].ColumnName;
                worksheet.Columns[x].ColumnName = worksheet.Columns[x].ColumnName.Replace(" ", "");
            }
            return worksheet;            
        }//end of getWorksheet

EDIT: i pulled up my old code from dropbox previous versions that was definetly working as well as redownload a copy of the excel doc i know was working..... what gives? has something changed in my computer?

Liquid
  • 1,871
  • 1
  • 17
  • 32
MitchellK
  • 41
  • 1
  • 8
  • Are you sure you are still on 2003 Excel files (http://stackoverflow.com/questions/1139390/excel-external-table-is-not-in-the-expected-format)? – Fionnuala Jul 04 '12 at 10:11
  • EDIT: sorry, i hadn't read the link you provide. Looks like it might do just the trick. I'll try it now. It's in xlsx format if that is any indication. I'm not quite sure how to tell the difference. The confusing thing is that i'm using the same connection and the same excel as i have been since i started the project. it just has all of a sudden stopped accepting the file. – MitchellK Jul 04 '12 at 10:30
  • It worked! It worked fine on my computer, but i didn;t need to update any drivers (maybe because i have 2010 word, or because i have the ultimate version of visual studio). This program is likely to be run on a different computer, do I need the drivers only for compiling the code, or will i need to ensure that the client that runs the program has the ace drivers? – MitchellK Jul 04 '12 at 10:44
  • Yes, everyone will need ACE drivers, but like you, they may already have them. – Fionnuala Jul 04 '12 at 10:45

1 Answers1

1

You are connecting to a 2007/2010 Excel file (*.xlsx, *.xlsm). You need the updated 2010 drivers (Ace), which can be downloaded for free. The correct connection string can be obtained from http://connectionstrings.com/Excel and http://connectionstrings.com/Excel-2007

Fionnuala
  • 90,370
  • 7
  • 114
  • 152