随着单位活动的增多,各大4S店要求查看活动报名的要求的出现,导出Excel或者是Word文档功能显得尤为重要。可以大量节省编辑的工作时间和工作强度。
        今天花了点时间编写了各大报名品牌的Excel导出功能。原理非常简单,其实就想往页面输出报名列表,不同的是在输出HTML文本时在HEAD标记 HTML标记增加点东西。
       代码如下:
              
 1  string strExportFileName = new SysCar.BLL.BrandCategory().GetModel(Convert.ToInt32(this.rblJoinBrand.SelectedValue)).Name;
 2                 int actID = Convert.ToInt32(Request.QueryString["actid"]);
 3                 SysCar.BLL.Apply apply = new BLL.Apply();
 4 
 5                 List<SysCar.Model.Apply> appExcel = apply.DataTableToList(apply.GetList("(ActivityID=" + actID + " and PATINDEX('%|" + this.rblJoinBrand.SelectedValue + "|%',BrandCollection)>0)").Tables[0]);
 6 
 7                 Response.ContentType = "application/vnd.ms-excel";
 8                 Response.AddHeader("Content-Disposition""inline;filename="
 9                     + HttpUtility.UrlEncode(strExportFileName + "_名单.xls", Encoding.UTF8));
10 
11 
12                 //如果输出为Word,修改为以下代码
13                 //Response.ContentType = "application/ms-word" 
14                 //Response.AddHeader("Content-Disposition", "inline;filename=test.doc") 
15 
16                 StringBuilder sb = new StringBuilder();
17                 System.IO.StringWriter sw = new System.IO.StringWriter(sb);
18                 System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
19                 sb.Append("<html xmlns:x=urn:schemas-microsoft-com:office:excel><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + strExportFileName + "</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo /></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
20                 sb.Append("<table><tr><td colspan=4>" + strExportFileName + "<font color='red'>" + appExcel.Count + "(人)</font></td></tr><tr><td>编号</td><td>姓名</td><td>性别</td><td>联系电话</td></tr>");
21                 for (int i = 0; i < appExcel.Count; i++)
22                 {
23                     string strSex = "";
24                     if (Convert.ToInt32(appExcel[i].Sex) == 2)
25                     {
26                         strSex = "";
27                     }
28                     sb.Append("<tr><td>" + appExcel[i].ApplyID + "</td><td>" + appExcel[i].Name + "</td><td>" + strSex + "</td><td>" + appExcel[i].Mobile + "</td></tr>");
29 
30                 }
31                 sb.Append("</table>");
32                 sb.Append("</body></html>");
33 
34 
35                 Response.Write(sb.ToString());
36                 Response.End();

关键点:

 1 
 2 <!--解决导出Excel无表格线问题-->
 3 <html xmlns:x=urn:schemas-microsoft-com:office:excel>
 4 <head>
 5 <!--[if gte mso 9]>
 6 <xml>
 7 <x:ExcelWorkbook>
 8 <x:ExcelWorksheets><x:ExcelWorksheet>
 9 <x:Name>表格名称</x:Name>
10 <x:WorksheetOptions>
11 <x:Print><x:ValidPrinterInfo /></x:Print>
12 </x:WorksheetOptions>
13 </x:ExcelWorksheet></x:ExcelWorksheets>
14 </x:ExcelWorkbook>
15 </xml>
16 <![endif]-->
17 
18 
19 
20 <!--解决导出Excel中文乱码问题-->
21 <meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">
22 

 

 

posted on 2010-08-26 14:56  AlexCheng  阅读(634)  评论(0编辑  收藏  举报