导出excel
public void CreateExcel(DataSet ds, string FileName)
{
StringBuilder builder = new StringBuilder();
//定义表对象与行对像,同时用DataSet对其值进行初始化
DataTable tb = ds.Tables[0];
DataRow[] myRow = tb.Select("");
builder.Append("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
//写出列名
builder.Append("<tr style=\"font-weight: bold; white-space: nowrap;\">");
foreach (DataColumn column in tb.Columns)
{
builder.Append("<td>" + column.ColumnName + "</td>");
}
builder.Append("</tr>");
//写出数据
foreach (DataRow row in tb.Rows)
{
builder.Append("<tr>");
foreach (DataColumn column in tb.Columns)
{
if (column.ColumnName.Equals("公司订单号") || column.ColumnName.Equals("总订单号"))
builder.Append("<td style=\"vnd.ms-excel.numberformat:@\">" + row[column].ToString() + "</td>");
else
builder.Append("<td>" + row[column].ToString() + "</td>");
}
builder.Append("</tr>");
}
builder.Append("</table>");
using (System.IO.StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath("/OrderFile/" + FileName + ".xls"), false, System.Text.Encoding.GetEncoding("GB2312")))
{
sw.Write(builder);
sw.Close();
sw.Dispose();
}
Response.AppendHeader("Content-Disposition", "attachment;filename=/OrderFile/" + FileName + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
Response.WriteFile("/OrderFile/" + FileName + ".xls");
Response.End();
}