John
John

Reputation: 3945

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?

Upvotes: 0

Views: 3034

Answers (3)

ketan italiya
ketan italiya

Reputation: 256

Either you can take use of OLEDB object or you can take use of Interop object to read each cell from excel file and insert it in database see below code snippet in following snippet i have used OLEDB and fetch the values from excel

string objCON = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=EXcel_file_path;Extended Properties='Excel 8.0;HDR=Yes;'"
using(OleDbConnection objCon = new OleDbConnection(objCON))
{
    objCon.Open();
    OleDbCommand objCMD = new OleDbCommand("select * from [Sheet1$]", objCon) 
    using(OleDbDataReader objDR = objCMD.ExecuteReader())
    {
         while(objDR.Read())
         {
             messageBox.Show("value of row1 and column 0" + dr[0]);
         }
        if(objDR != null)
        {
    objDR.Close();
    objDR = null;
         }
    }
}

Upvotes: 0

Hernan Guzman
Hernan Guzman

Reputation: 1235

I suggest you this Codeplex Project called Excel Data Reader:

http://exceldatareader.codeplex.com/

Hope it helps!

Upvotes: 1

sgeddes
sgeddes

Reputation: 62851

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.

Upvotes: 2

Related Questions