导出页面数据到Excel

  上个项目要用到导出数据到Excel,自己完全不知道,到网上大概的找寻了一下然后自己修改了一下做出了一个大概实现了功能模块,

  首先是在绑定列表数据源之后根据数据列表生成一串字符串

/// <summary>
/// 内部方法:报表导出操作
/// </summary>
/// <param name="applyList">数据列表</param>
/// <param name="time">导出时间时间</param>
private void GetExportDayStr(List<EFEntity.Apply> applyList, DateTime time)
{
StringBuilder sb = new StringBuilder();
int count = applyList.Count + 1;
string week = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(time.DayOfWeek);
sb.Append("<table><tr><td colspan=\"" + count + "\" style=\"text-align:center\"><h3>表头</h3></td></tr>");
sb.Append("<table><tr><td colspan=\"" + count + "\" style=\"text-align:center\">导出日期:" + time.ToString("yyyy年MM月dd日") + "" + week + "</td></tr>");

//编辑Excel表头
sb.Append("<tr><td style=\"text-align:center;80px;height:40px;\" >列名</td>");
sb.Append("<td style=\"text-align:center;80px;height:40px;\" >列名</td>");
sb.Append("<td style=\"text-align:center;80px;height:40px;\" >列名</td>");
sb.Append("<td style=\"text-align:center;80px;height:40px;\" >经办人</td>");
sb.Append("<td style=\"text-align:center;80px;height:40px;\" >列名</td>");
sb.Append("<td style=\"text-align:center;80px;height:40px;\" >列名</td>");
sb.Append("<td style=\"text-align:center;80px;height:40px;\" >列名</td>");
sb.Append("<td style=\"text-align:center;80px;height:40px;\" >列名</td>");
sb.Append("<td style=\"text-align:center;80px;height:40px;\" >列名</td></tr>");
foreach (var item in applyList)
{//循环输出数据
sb.Append("<tr><td style=\"text-align:center\">" + 数据字符串+ "</td>");
sb.Append("<td style=\"text-align:center\">" + 数据字符串+ "</td>");
sb.Append("<td style=\"text-align:center\">" + 数据字符串+ "</td>");
sb.Append("<td style=\"text-align:center\">" + 数据字符串+ "</td>");
sb.Append("<td style=\"text-align:center\">" + 数据字符串+ "</td>");
sb.Append("<td style=\"text-align:center\">" + 数据字符串+ "</td>");
sb.Append("<td style=\"text-align:center\">" + 数据字符串+ "</td>");
sb.Append("<td style=\"text-align:center\">" + 数据字符串 + "</td>");
sb.Append("<td style=\"text-align:center\">" + 数据字符串 + "</td></tr>");
}
sb.Append("</tr>");
ViewState["ExportStr"] = sb.ToString();
}


然后在点击导出数据时间中

  

protected void ToExcel_Click(object sender, EventArgs e)
{
if (ViewState["ExportStr"] != null)
{
ExportExcel("表名", ViewState["ExportStr"].ToString());
}
}
/// <summary>
/// 内部方法:导出数据报表xls
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="strTable">要导出的表数据</param>
public void ExportExcel(string fileName, string strTable)
{

string HEADER = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">" + "<meta http-equiv=Content-Type content=\"text/html; charset=\"gb2312\">" + "<head>" + "<!--[if gte mso 9]><xml>" + "<x:ExcelWorkbook>" + "<x:ExcelWorksheets>" + "<x:ExcelWorksheet>" + "<x:Name>Sheet1</x:Name>" + "<x:WorksheetOptions>" + "<x:Print>" + "<x:ValidPrinterInfo />" + "</x:Print>" + "</x:WorksheetOptions>" + "</x:ExcelWorksheet>" + "</x:ExcelWorksheets>" + "</x:ExcelWorkbook>" + "</xml>" + "<![endif]-->";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

System.Web.HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("utf-8")) + ".xls\"");
System.Web.HttpContext.Current.Response.ContentType = "ms-excel/application";
StringBuilder sbHtml = new StringBuilder();
sbHtml.AppendFormat(@"{0}</head> <body>{1}</body> </html>", HEADER, strTable);
System.Web.HttpContext.Current.Response.Write(sbHtml.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.End();
}


  这样基本就实现了。感觉不是很美观,还需要优化一下

※如果你觉得这篇文章不错,请点击推荐。如果你觉得我写的对你有用请关注我。
作者:Max蚊子
网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!
             
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/kim01/p/2394103.html