zoukankan      html  css  js  c++  java
  • MVC下用C#实现Excel导出

    Aspx页面脚本:

    function exportxls() {

                window.open("/Common/HomeExport?startdate=" + $("#hidStartTime").val(), "exportxls", null);

            }



    C#代码如下:

       public class CommonController

        {

            public void HomeExport()

            {

                StringBuilder sHtml = new StringBuilder(string.Empty);

                //下面这句解决中文乱码

                sHtml.Append("<meta http-equiv=content-type content=application/ms-excel; charset=utf-8/>");

                ……

                //打印表头

                sHtml.Append("<table border=1 width=100%>");

                //打印列名

                sHtml.Append("<tr height=20 align=centerstyle='background-color:yellow'><td>No. </td><td>Project</td><td>Content</td>tr>");

                //循环读取List集合

                ……

                //打印表尾

                sHtml.Append("</table>");

                string filename = "Report";

                //调用输出Excel表的方法

                ExportToExcel("application/ms-excel", filename + ".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();

            }

        }

     

  • 相关阅读:
    Codeforces Round #719 (Div. 3) 题解
    Codeforces Global Round 14 A~F题解
    AtCoder Beginner Contest 199 题解
    Codeforces Round #716 (Div. 2) A~D 题解
    Codeforces Round #713 (Div. 3) 题解
    Codeforces Round #712 (Div. 2) A~E 题解
    CodeCraft-21 and Codeforces Round #711 (Div. 2) A~E 题解
    CF839 D 莫比乌斯反演
    java存大数和高精度浮点数(BigInteger与BigDecimal)
    java科学计算常用方法(Math)
  • 原文地址:https://www.cnblogs.com/yumianhu/p/3707541.html
Copyright © 2011-2022 走看看