ASP 百分网手机站

Asp.Net数据输出到EXCEL表格中的教程

时间:2018-04-30 12:23:18 ASP 我要投稿

Asp.Net数据输出到EXCEL表格中的教程

  一、定义文档类型、字符编码

  Response.Clear();

  Response.Buffer= true;

  Response.Charset="utf-8";

  //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开

  //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm

  Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");

  Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");

  //Response.ContentType指定文件类型 可以为application/ms-excel application/ms-word application/ms-txt application/ms-html 或其他浏览器可直接支持文档

  Response.ContentType = "application/ms-excel";

  this.EnableViewState = false;

  二、定义一个输入流

  system.IO.StringWriter oStringWriter = new system.IO.StringWriter();

  system.Web.UI.HtmlTextWriter oHtmlTextWriter = new system.Web.UI.HtmlTextWriter(oStringWriter);

  三、将目标数据绑定到输入流输出

  this.RenderControl(oHtmlTextWriter);

  //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件

  Response.Write(oStringWriter.ToString());

  Response.End();

  四、 这时如果发生"只能在执行 Render() 的过程中调用 RegisterForEventValidation"的.错误提示。

  有两种方法可以解决:

  1.修改web.config(不推荐)

  2.直接在导出Execl的页面修改

  总结:本例程在Microsoft Visual Studio .NET 2003平台下测试通过,适用于C#和VB,当采用VB的时候将 this 关键字改成 me 。

【Asp.Net数据输出到EXCEL表格中的教程】相关文章:

1.excel表格数据筛选教程

2.excel表格数据排序教程

3.Scott Mitchell 的ASP.NET 2.0数据教程

4.Excel圈出无效数据的教程

5.Excel表格条件格式教程

6.Excel表格公式的使用教程

7.Word中的数据引用教程

8.excel表格数据的分类汇总教程