Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to export data alone in the datatable to an exisiting excel template?
I have 2 static headers set into the template. I Want to export data in the datatable to the exisiting template in the folder of my project. I Don't want to create another excel.

Below is my code
C#
string path = Server.MapPath("~/Files/WeeklyIMS.xls");

                    //if (!Directory.Exists(path))   // CHECK IF THE FOLDER EXISTS. IF NOT, CREATE A NEW FOLDER.
                    //{
                    //    Directory.CreateDirectory(path);
                    //}


                    //File.Delete(path + "WeeklyIMS.xls");
                    //path = path + "WeeklyIMS.xls";
                     // ADD A WORKBOOK USING THE EXCEL APPLICATION.
                    Excel.Application xlAppToExport = new Excel.Application();
                     //xlAppToExport.Workbooks.Add("");

                     // ADD A WORKSHEET.
                     Excel.Worksheet xlWorkSheetToExport = default(Excel.Worksheet);
                     xlWorkSheetToExport = (Excel.Worksheet)xlAppToExport.Sheets["Sheet1"];

                     // ROW ID FROM WHERE THE DATA STARTS SHOWING.
                     int iRowCnt = 4;

                     // SHOW THE HEADER.
                     //xlWorkSheetToExport.Cells[1, 1] = "Employee Details";

                     //Excel.Range range = xlWorkSheetToExport.Cells[1, 1] as Excel.Range;
                     //range.EntireRow.Font.Name = "Calibri";
                     //range.EntireRow.Font.Bold = true;
                     //range.EntireRow.Font.Size = 20;

                     //xlWorkSheetToExport.Range["A1:D1"].MergeCells = true;       // MERGE CELLS OF THE HEADER.

                     // SHOW COLUMNS ON THE TOP.
                    // xlWorkSheetToExport.Cells[iRowCnt - 1, 1] = "Employee Name";
                     //xlWorkSheetToExport.Cells[iRowCnt - 1, 2] = "Mobile No.";
                     //xlWorkSheetToExport.Cells[iRowCnt - 1, 3] = "PresentAddress";
                     //xlWorkSheetToExport.Cells[iRowCnt - 1, 4] = "Email Address";


                     int i;
                     for (i = 0; i <dt.rows.count;>                        {
                         string s = dt.Rows[i]["CSDReceivedDate"].ToString();
                         xlWorkSheetToExport.Cells[iRowCnt, 1] = dt.Rows[i]["CSDReceivedDate"].ToString();
                         //xlWorkSheetToExport.Cells[iRowCnt, 2] = dt.Rows[i].Field("Mobile");
                         //xlWorkSheetToExport.Cells[iRowCnt, 3] = dt.Rows[i].Field("PresentAddress");
                         //xlWorkSheetToExport.Cells[iRowCnt, 4] = dt.Rows[i].Field("Email");

                         iRowCnt = iRowCnt + 1;
                     }

                     // FINALLY, FORMAT THE EXCEL SHEET USING EXCEL'S AUTOFORMAT FUNCTION.
                     //Excel.Range range1 = xlAppToExport.ActiveCell.Worksheet.Cells[4, 1] as Excel.Range;
                     //range1.AutoFormat(ExcelAutoFormat.xlRangeAutoFormatList3,null,null,null,null,null,null);

                     // SAVE THE FILE IN A FOLDER.
                     //path = path + "~/WeeklyIMS.xls";
                     //xlWorkSheetToExport.SaveAs(path,null,null,null,null,null,null,null,null,null);

                     // CLEAR.
                     xlAppToExport.Workbooks.Close();
                     xlAppToExport.Quit();
                     xlAppToExport = null;
                     xlWorkSheetToExport = null;

                     //lblConfirm.Text = "Data Exported.";
                     //lblConfirm.Attributes.Add("style", "color:green; font: bold 14px/16px Sans-Serif,Arial");
                     //btView.Attributes.Add("style", "display:block");
                 }

             catch
             {
                 //lblConfirm.Text = "There was an error.";
                 //lblConfirm.Attributes.Add("style", "color:red; font: bold 14px/16px Sans-Serif,Arial");
             }
             finally
             {
                 //sda.Dispose();
                 //sda = null;
             }
Posted
Updated 26-Dec-14 1:36am
v2
Comments
ZurdoDev 26-Dec-14 7:36am    
Where are you stuck?
deepankarbhatnagar 26-Dec-14 7:44am    
PLease elaborate your requirement?

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