asp.net后台导出excel的方法一:使用response导出excel

方法一:带StringBuilder 方法的导出

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

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

System.Web.HttpContext.Current.Response.Clear();
      System.Web.HttpContext.Current.Response.Charset </span>= <span >"</span><span >GB2312</span><span >"</span><span >;
      </span><span >//</span><span > System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
      </span><span >//</span><span > 添加头信息,为"文件下载/另存为"对话框指定默认文件名</span>
      System.Web.HttpContext.Current.Response.AddHeader(<span >"</span><span >Content-Disposition</span><span >"</span>, <span >"</span><span >attachment; filename=myU.xls</span><span >"</span><span >);
      </span><span >//</span><span > 添加头信息,指定文件大小,让浏览器能够显示下载进度
      </span><span >//</span><span > System.Web.HttpContext.Current.Response.AddHeader("Content-Length",sb.ToString());

      </span><span >//</span><span > 指定返回的是一个不能被客户端读取的流,必须被下载</span>
      System.Web.HttpContext.Current.Response.ContentType = <span >"</span><span >application/ms-excel</span><span >"</span><span >;

      </span><span >//</span><span > 把文件流发送到客户端</span>

方法二:带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的事例)
1 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
4 Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
   Response.ContentType = "application/vnd.ms-excel"
   Response.ContentEncoding = System.Text.Encoding.Unicode
5 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一行一行导出的,你可以根据自己的需求添加相应的行。

</span><span >string</span> colHeaders = <span >string</span><span >.Empty;
    </span><span >string</span> ls_item = <span >string</span><span >.Empty;
    DataRow[] myRow </span>=<span > dt.Select();
    </span><span >int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span><span >;
    </span><span >int</span> cl =<span > dt.Columns.Count;
    </span><span >foreach</span> (DataRow row <span >in</span><span > myRow)
    {
        </span><span >for</span> (i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i < cl; i++<span >)
        {
            </span><span >if</span> (i == (cl - <span style="color: rgba(128, 0, 128, 1)">1</span><span >))
            {<br>//到头换行
                ls_item </span>+= row[i].ToString() + <span >"</span><span >\n</span><span >"</span><span >;
            }
            </span><span >else</span><span >
            {<br>//换格
                ls_item </span>+= row[i].ToString() + <span >"</span><span >\t</span><span >"</span><span >;
            }
        }
        </span><span >//</span><span >一行一行的写入</span>

    Response.Output.Flush();
    Response.End();
}</span></pre>