1. 程式人生 > >C# MVC 自學筆記—11 在 ASP.NET MVC 中使用EXCEL匯出

C# MVC 自學筆記—11 在 ASP.NET MVC 中使用EXCEL匯出

====================此部分為轉載內容====================

因為是轉載文章 在此標明出處,以前有文章是轉的沒標明的請諒解,因為有些已經無法找到出處,或者與其它原因。

如有冒犯請聯絡本人,或刪除,或標明出處。

因為好的文章,以前只想收藏,但連線有時候會失效,所以現在碰到好的直接轉到自己這裡。

先把程式碼貼出來:

複製程式碼
public void Print()
  {
    //接收需要匯出的資料
    List<實體類名稱> list = db.findAll<實體類名稱>();
 
    //命名匯出表格的StringBuilder變數
StringBuilder sHtml = new StringBuilder(string.Empty); //打印表頭 sHtml.Append("<table border=\"1\" width=\"100%\">"); sHtml.Append("<tr height=\"40\"><td colspan=\"5\" align=\"center\" style='font-size:24px'><b>XXXXXXX報價表" + "</b></td></tr>");
//列印列名 sHtml.Append("<tr height=\"20\" align=\"center\" style='background-color:#CD0000'><td>編號</td><td>商品名稱</td><td>市場價</td><td>VIP價格</td><td>說明</td></tr>"); //迴圈讀取List集合 for (int i = 0; i < list.Count; i++) { sHtml.Append(
"<tr height=\"20\" align=\"left\"><td style='background-color:#8DEEEE'>" + list[i].Id + "</td><td>" + list[i].Title + "</td><td style='background-color:#8DEEEE'>¥" + list[i].SalePrice + "</td><td style='color:#F00;background-color:#8DEEEE;'>¥" + list[i].VipPrice + "</td><td>" + list[i].Summary + "</td></tr>"); } //打印表尾 sHtml.Append("<tr height=\"40\"><td align=\"center\" colspan=\"5\" style='background-color:#CD0000;font-size:24px'><b>XXXXXXXX</a> </b></td></tr>"); sHtml.Append("</table>"); //呼叫輸出Excel表的方法 ExportToExcel("application/ms-excel", "XXXXXX報價表.xls", sHtml.ToString()); }
複製程式碼 打印表格的方法 複製程式碼
         //輸入HTTP頭,然後把指定的流輸出到指定的檔名,然後指定檔案型別
      public void ExportToExcel(string FileType, string FileName, string ExcelContent)
         {
             System.Web.HttpContext.Current.Response.Charset = "UTF-8";
             System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
             System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
             System.Web.HttpContext.Current.Response.ContentType = FileType;
             System.IO.StringWriter tw = new System.IO.StringWriter();
             System.Web.HttpContext.Current.Response.Output.Write(ExcelContent.ToString());
             System.Web.HttpContext.Current.Response.Flush();
             System.Web.HttpContext.Current.Response.End();
         }
複製程式碼

====================此部分為轉載內容====================

以下是我改進後的案例:

public void toExcel()
        {
            //接收需要匯出的資料
            List<Contacts_BClient> list = db.Contacts_BClient.ToList();

            //命名匯出表格的StringBuilder變數
            StringBuilder sHtml = new StringBuilder(string.Empty);

            //打印表頭
            sHtml.Append("<table border=\"1\" width=\"100%\">");
            sHtml.Append("<tr height=\"40\"><td colspan=\"8\" align=\"center\" style='font-size:24px'><b>大客戶部通訊錄" + "</b></td></tr>");

            //列印列名
            sHtml.Append("<tr height=\"20\" align=\"center\" ><td>員工號</td><td>大客戶經理</td><td>聯絡方式</td><td>職務</td><td>行業</td>"
                +"<td>省份</td><td>負責區域</td><td>直屬經理</td></tr>");

            //迴圈讀取List集合
            for (int i = 0; i < list.Count; i++)
            {
                sHtml.Append("<tr height=\"20\" align=\"left\"><td>" + list[i].employeeNumber_C
                    + "</td><td>" + list[i].accountManager_C + "</td><td>" + list[i].phone_C + "</td><td>" + list[i].position_C
                    + "</td><td>" + list[i].industry_C + "</td><td>" + list[i].province_C + "</td><td>" + list[i].responsibleRegional_C
                    + "</td><td>" + list[i].lineManager_C
                    + "</td></tr>");
            }

            //打印表尾
            sHtml.Append("</table>");

            //呼叫輸出Excel表的方法
            ExportToExcel("application/ms-excel", "大客戶部通訊錄.xls", sHtml.ToString());
        }

        public void ExportToExcel(string FileType, string FileName, string ExcelContent)
        {
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            System.Web.HttpContext.Current.Response.ContentType = FileType;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.HttpContext.Current.Response.Output.Write(ExcelContent.ToString());
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }

介面程式碼下次補齊 下班了