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 )
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;
}
}