本文介绍了如何在C#上创建薪水单以精益求精的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尊敬的先生,
如何在Excel上创建薪水单.我有数据集(ds)中来自数据库的数据.
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.
推荐答案
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;
}
}
这篇关于如何在C#上创建薪水单以精益求精的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!