paul mc gavigan

paul mc gavigan

  • NA
  • 5
  • 3.2k

Read excel file and write to a jagged array?

Jan 11 2012 8:52 AM
Hi, I have been asked to write a small piece of code which will open an excel file in c# and then write said excel file to a jagged array. I had never heard of a jagged array until monday so I am pretty much at a loss here as to what I am doing. So far I have managed to open the excel file in visual studio and write the contents to a datagridview but would like to bypass this and write it directly to jagged array any help much appreciated. Here is my working code so far.

private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\\myworkbook.xlsx ';Extended Properties=Excel 12.0;";
            {
                OleDbConnection connection = new OleDbConnection(connectionString);
                connection.Open();

                OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection);
                DataSet ds = new DataSet();
                System.Data.DataTable dt = new System.Data.DataTable();
                adapter.Fill(ds);//now you have your dataset ds now filled with the data and ready for manipulation
               
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
        }

any help much appreciated.

Answers (4)