Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
dear sir,
how to create a salary slip on excel. I have data from database in dataset(ds).
how can design the salary slip based on user-defined format.
Posted
Comments
Manoj Kumar Choubey 7-Feb-12 6:23am    
Is this web application

 
Share this answer
 
Crate all salary slip data in data table and export it in excel format as follow (if you using web application otherwise use another code for export )

C#
public static void exportDataTableToExcel(DataTable table, string name)
   {

       try
       {
           HttpContext context = HttpContext.Current;

           context.Response.Clear();
           context.Response.Buffer = true;
           string style = @"<style> .textmode { mso-number-format:\@; } </style>";
           context.Response.Write(style);

           context.Response.Write("<div> ");
           context.Response.Write(Environment.NewLine);
           context.Response.Write("<table>");
           context.Response.Write(Environment.NewLine);
           context.Response.Write("<tr> ");
           context.Response.Write(Environment.NewLine);

           foreach (DataColumn column in table.Columns)
           {
               context.Response.Write("<th style = 'font-weight:bold'>");
               context.Response.Write(column.ColumnName);
               context.Response.Write("</th>");
           }

           context.Response.Write("</tr>");




           foreach (DataRow row in table.Rows)
           {

               context.Response.Write(Environment.NewLine);
               context.Response.Write("<tr> ");
               context.Response.Write(Environment.NewLine);

               for (int i = 0; i < table.Columns.Count; i++)
               {
                   context.Response.Write("<th  style = 'font-weight:Normal' >");
                   context.Response.Write(row[i].ToString());
                   context.Response.Write("</th>");

               }

               context.Response.Write(Environment.NewLine);
               context.Response.Write("</tr> ");
               context.Response.Write(Environment.NewLine);


           }

           context.Response.Write("</table>");
           context.Response.Write(Environment.NewLine);
           context.Response.Write("</div>");
           context.Response.Write(Environment.NewLine);





           context.Response.AddHeader("content-disposition", "attachment;filename=" + name + ".xls");
           context.Response.Charset = "";
           context.Response.ContentType = "application/vnd.ms-excel";

           context.Response.Flush();


           context.Response.End();
       }
       catch (Exception ex)
       {
           throw ex;
       }

   }
 
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