none
Importing data from Excel RRS feed

  • Question

  • Hi!

    I found some code on the internet and it actually works, but not as I aspected. It does not return the first row of a sheet. What is wrong?

    Here is the code:
    Code Snippet

    try{
        string strConnectionString = string.Empty;
        strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";


        OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
        cnCSV.Open();
        OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [Sheet1$]", cnCSV);
        OleDbDataAdapter daCSV = new OleDbDataAdapter();
        daCSV.SelectCommand = cmdSelect;
        DataTable dtCSV = new DataTable();
        daCSV.Fill(dtCSV);
        cnCSV.Close();
        daCSV = null;
        textBox1.Text = (string)dtCSV.Rows[0][0];
        textBox2.Text = (string)dtCSV.Rows[1][1];

    }
    catch (Exception ex){
    }



    Tuesday, June 3, 2008 9:49 PM

Answers

All replies