zoukankan      html  css  js  c++  java
  • https://www.cnblogs.com/sizhizhiyue/p/4820973.html asp.net后台导出excel的方法一:使用response导出excel

    asp.net后台导出excel的方法一:使用response导出excel
    	</h1>
    	<div class="clear"></div>
    	<div class="postBody">
    

    方法一:带StringBuilder 方法的导出

    该方法是将所有的数据通过html的形式写入到StringBuilder 中,然后通过response导出。

    熟悉html格式的人可以改变成各种格式。

    复制代码
    List<U> objList = new List<U>();
             objList = BLL.GetInfo();//读取数据                     
              
              StringBuilder sb = new StringBuilder();
              sb.Append("<style type="text/css">");
              sb.Append("<!--");
              sb.Append(".text");
              sb.Append("{mso-style-parent:style0;");
              sb.Append("font-size:10.0pt;");
              sb.Append("font-family:"Arial Unicode MS", sans-serif;");
              sb.Append("mso-font-charset:0;");
              sb.Append(@"mso-number-format:@;");
              sb.Append("text-align:center;");
              sb.Append("border:.5pt solid black;");
              sb.Append("white-space:normal;}");
              sb.Append("-->");
              sb.Append("</style>");
              sb.Append("<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">");
              sb.Append("<tr align="Center" style="font-weight:bold;">");
              sb.Append("<td>ID</td><td>用户名</td><td>真实姓名</td><td>省份</td><td>注册时间</td");
              sb.Append("</tr>");
              foreach (U item in objList)
              {
                  sb.Append("<tr align="Center"><td>" + item.id + "</td><td>" + item.uName + "</td><td>" + item.rName + "</td><td>" + item.proId + "</td><td>" + item.regDate + "</td></tr>");
              }
              sb.Append("</table>");
    
          System.Web.HttpContext.Current.Response.Clear();
          System.Web.HttpContext.Current.Response.Charset </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">GB2312</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加头信息,为"文件下载/另存为"对话框指定默认文件名</span>
          System.Web.HttpContext.Current.Response.AddHeader(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Content-Disposition</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">attachment; filename=myU.xls</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加头信息,指定文件大小,让浏览器能够显示下载进度
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.Web.HttpContext.Current.Response.AddHeader("Content-Length",sb.ToString());
    
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 指定返回的是一个不能被客户端读取的流,必须被下载</span>
          System.Web.HttpContext.Current.Response.ContentType = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">application/ms-excel</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 把文件流发送到客户端</span>
    

    System.Web.HttpContext.Current.Response.Write(sb.ToString());
    // 停止页面的执行

    System.Web.HttpContext.Current.Response.End();

    复制代码

    方法二:带StringWriter的导出方法

    该方法的格式不容易改变 方法与上面原理相同是将数据写入到StringWriter 中 让后到导出

     众所周知,Respone.Write()是输出Html流程序给用户的。考虑到一个标准的Web页面的是有多种呈现方式的,
    例如:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 这是以标准网页形式输出Html流

    <meta http-equiv="Content-Type" content="application/vnd.ms-excel">
    <meta http-equiv="Content-Disposition" content="attachment; filename=ex.xls">这是以附件形式输出Html流,而且是将“数据”存放在ex.xls这个表格中。^_^

    那么我们以编码的形式如何显示^_^(现在写VB了,给出的也是VB的事例)
    Dim _DataStringWriter As StringWriter = New StringWriter 定义一个StringWriter对象
    2 _DataStringWriter.WiteLine("FirstFieldName" + ControlChars.Tab + "SecondFieldName")给输出的Excel表格每  列加入名称
    3 从数据“容器”里面将数据取出。例如
       Dim dt as New DataTable
       For i as Integer = 0 To dt.Rows.Count - 1 Then
       _DataStringWriter.WiteLine(dt(i)(0) + ControlChars.Tab + dt(i)(1))
       Next
    Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
       Response.ContentType = "application/vnd.ms-excel"
       Response.ContentEncoding = System.Text.Encoding.Unicode
    Response.Write(_DataStringWriter) 输出Html流
       Response.End()
    以上已经可以实现将数据导入到Excel表格,如果需要导入Word则Response.ContentType = "application/vnd.ms-excel"中改为Response.ContentType = "application/vnd.ms-word"即可。但是注意将fileName也应随之改变,XX.xls或者XX.doc

    方法三:datatable导出

    该方法是response一行一行导出的,你可以根据自己的需求添加相应的行。

    复制代码
    /// <summary>
        /// DataTable中的数据导出到Excel并下载
        /// </summary>
        /// <param name="dt">要导出的DataTable</param>
        /// <param name="FileType">类型</param>
        /// <param name="FileName">Excel的文件名</param>
        public void CreateExcel(DataTable dt, string FileType, string FileName)
        {
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.Buffer = true;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename="" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"");
            Response.ContentType = FileType;
    
        </span><span style="color: rgba(0, 0, 255, 1)">string</span> colHeaders = <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;
        </span><span style="color: rgba(0, 0, 255, 1)">string</span> ls_item = <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;
        DataRow[] myRow </span>=<span style="color: rgba(0, 0, 0, 1)"> dt.Select();
        </span><span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
        </span><span style="color: rgba(0, 0, 255, 1)">int</span> cl =<span style="color: rgba(0, 0, 0, 1)"> dt.Columns.Count;
        </span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (DataRow row <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> myRow)
        {
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i &lt; cl; i++<span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (i == (cl - <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">))
                {<br>//到头换行
                    ls_item </span>+= row[i].ToString() + <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">
    </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                }
                </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
                {<br>//换格
                    ls_item </span>+= row[i].ToString() + <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">	</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                }
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一行一行的写入</span>
    

    Response.Output.Write(ls_item);
    ls_item
    = string.Empty;
    }

        Response.Output.Flush();
        Response.End();
    }</span></pre>
    
    复制代码

  • 相关阅读:
    window.open()弹出窗口防止被禁
    实用Javascript代码片段
    php字符串常用函数
    PHP基本使用
    php开发环境搭建
    what is php?
    Front-end Developer Interview Questions
    jQuery Questions:Front-end Developer Interview Questions
    JS Questions:Front-end Developer Interview Questions
    HTML Questions:Front-end Developer Interview Questions
  • 原文地址:https://www.cnblogs.com/sunny3158/p/14218543.html
Copyright © 2011-2022 走看看