Search code examples
asp.netdynamic-data

read an excel file


In my asp.net dynamic data project I want to read an excel file using suitable Microsoft technology (Not Excel). Someone said their was a server side tool for this? I want to read the first few columns of data from a table?

Any suggestions or directions on where to go?


Solution

  • In reply to your comments, take a look here:

    http://www.codeproject.com/Articles/37055/Working-with-MS-Excel-xls-xlsx-Using-MDAC-and-Oled

    Here is some (untested) code to get you started.

    string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;
                              Data Source=c:\\testexcel.xls;
                              Extended Properties\"Excel 8.0;HDR=YES\"";
    string cmdText = "SELECT * FROM [Sheet1$]";
    using(conObj = new OleDbConnection(connectionstring))
    {
       using (OleDbCommand cmd = new OleDbCommand(createTableScript, conObj)
       {
          OleDbDataAdapter adpt = new OleDbDataAdapter(cmd);
          DataSet ds = new DataSet();
          adpt.Fill(ds,"w1");
       }
    }
    

    Good luck.