C# MVC 自学笔记—11 在 ASP.NET MVC 中使用EXCEL导出

来源:互联网 发布:布尔教育java 编辑:程序博客网 时间:2024/05/02 01:51

====================此部分为转载内容====================

因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因。

如有冒犯请联系本人,或删除,或标明出处。

因为好的文章,以前只想收藏,但连接有时候会失效,所以现在碰到好的直接转到自己这里。

http://www.cnblogs.com/liyumei/archive/2013/01/25/2876108.html

 

 

C#  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();
        }

 

界面代码下次补齐 下班了

 

原创粉丝点击