sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1792 随笔 :: 22 文章 :: 24 评论 :: 223万 阅读
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

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)">\n</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)">\t</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>
复制代码
复制代码

 

标签: c#
分类: asp.net/c#
<div id="blog_post_info">
0
0
<div class="clear"></div>
<div id="post_next_prev">

<a href="https://www.cnblogs.com/sizhizhiyue/p/4759502.html" class="p_n_p_prefix">« </a> 上一篇:    <a href="https://www.cnblogs.com/sizhizhiyue/p/4759502.html" title="发布于 2015-08-26 10:05">infragistics--web网站升级注意点</a>
<br>
<a href="https://www.cnblogs.com/sizhizhiyue/p/4825575.html" class="p_n_p_prefix">» </a> 下一篇:    <a href="https://www.cnblogs.com/sizhizhiyue/p/4825575.html" title="发布于 2015-09-21 11:12">asp.net后台导出excel的方法:使用response导出excel</a>

posted @   丝竹之约  阅读(285)  评论(0编辑  收藏
posted on   sunny123456  阅读(285)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· .NET 8.0 + Linux 香橙派,实现高效的 IoT 数据采集与控制解决方案
· .NET中 泛型 + 依赖注入 的实现与应用
点击右上角即可分享
微信分享提示