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.

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)






              "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;




                  //  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;





                  //  render the table into the htmlwriter


                  //  render the htmlwriter into the response



Updated 22-Oct-12 0:32am
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[^]
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