Looping through all rows of the Excel Data

Posted by Agopi.net under C# on 1/11/2011 | Points: 10 | Views : 4619 | Status : [Member] | Replies : 2
Hi Experts,

I found the below code from the article page.

http://www.dotnetfunda.com/articles/article12.aspx


private void LoadDataTable()

{

string str = string.Empty;

string path = Server.MapPath("~/GridData.xml");

int i = 0;

DataSet dSet = new DataSet();

dSet.ReadXml(path);

DataTable dTable = dSet.Tables[0];

foreach (DataRow dRow in dTable.Rows)

{

i++;

str += "ROW " + i.ToString() + " : <b>Name: </b>" + dRow["FirstName"].ToString() + " " + dRow["LastName"].ToString() + ", <b>Address: </b>"+ dRow["Address"].ToString() +", <b>Profession: </b>" + dRow["Profession"].ToString() + "<br />";

}

litText.Text = str;

}


I need the same result from the excel sheet as a back end. Could you please help me.

Regards,
Gopi A





Responses

Posted by: Karthikanbarasan on: 1/11/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Try this link...

http://support.microsoft.com/kb/299036

Thanks
Karthik
www.f5Debug.net

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suresh.mekkattil on: 1/11/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

you can consider the excel as a database and each sheets as the tables, then,
you can use the following;
// Create OleDbCommand object and select data from worksheet

using (OleDbCommand cmd = new OleDbCommand("Select * From [Sheet1$]", connection))
{
// Create new OleDbDataAdapter
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;

// Create a DataSet which will hold the extracted data from the worksheet.
DataSet ds = new DataSet();

// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(ds, tableName);

return ds;


Or
you can read the excel as you do for the csv files.

-----
Suresh M

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response