Asp.net匯出Excel報表(解決無網格問題)

來源:互聯網
上載者:User
  隨著單位活動的增多,各大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 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.