Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Could anyone help me to solve this issue....

The below code is for creating a new excel file with data from generic list...I dont want to create a new excel file ..i just needed to add data to an existing excel file.


C#
protected void Button1_Click(object sender, EventArgs e)
       {
            List<Employee> empList = new List<Employee>();

          empList.Add(new Employee(20, "Ranjith"));

          empList.Add(new Employee(23, "Rakesh"));

          empList.Add(new Employee(30, "Ram"));

   // take excel FileName and the Exported List

          Export("Ranjith", empList);
       }

       public void Export(string fileName, List<Employee> empList)

       {

           

           HttpContext.Current.Response.Clear();

          

           HttpContext.Current.Response.AddHeader(

              "content-disposition", string.Format("attachment; filename={0}", fileName + ".xls"));

          

       


          HttpContext.Current.Response.ContentType = "application/ms-excel";

         

         using (StringWriter sw = new StringWriter())

          {

      

             using (HtmlTextWriter htw = new HtmlTextWriter(sw))

              {

                 //  Create a form to contain the List

                 Table table = new Table();

                  TableRow row = new TableRow();

                  foreach (PropertyInfo proinfo in new Employee(1,"Name").GetType().GetProperties())

                 {

                      TableHeaderCell hcell = new TableHeaderCell();

                     hcell.Text = proinfo.Name;

                      row.Cells.Add(hcell);

                  }

                 table.Rows.Add(row);

                  //  add each of the data item to the table

                  foreach (Employee emp in empList)

                  {

                      TableRow row1 = new TableRow();

                      TableCell cellAge = new TableCell();

                      cellAge.Text = "" + emp.Age;

                      TableCell cellName = new TableCell();

                      cellName.Text = "" + emp.Name;

                      row1.Cells.Add(cellAge);

                      row1.Cells.Add(cellName);

                      table.Rows.Add(row1);

                  }

                  //  render the table into the htmlwriter

                  table.RenderControl(htw);

                  //  render the htmlwriter into the response

                  HttpContext.Current.Response.Write(sw.ToString());

                  HttpContext.Current.Response.End();

              }
Posted
Updated 22-Oct-12 0:32am
v2
Comments
AshishChaudha 22-Oct-12 5:44am    
what issue???
Ranjithkumar54 22-Oct-12 5:47am    
To Export data from generic list to Existing Excel file
TheCoolCoder 22-Oct-12 6:24am    
Paste a relevant portion of your code using improve question.

1 solution

You are creating a HTML Excel File which very hard to add more rows directly.
You can use some third party tools to make an excel file / to add rows etc.

You can try this:
ClosedXML - The easy way to OpenXML
http://closedxml.codeplex.com/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900