ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM等类型的文档很容易的。主要分为三步来完成。
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中  一、定义文档类型、字符编码
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中Response.Clear(); 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中Response.Buffer
= true
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中Response.Charset
="utf-8";
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中

ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中Response.AppendHeader(
"Content-Disposition","attachment;filename=FileFlow.xls");
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中Response.ContentEncoding
=System.Text.Encoding.GetEncoding("utf-8");
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中

ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中Response.ContentType 
= "application/ms-excel";
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
this.EnableViewState = false
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中  二、定义一个输入流
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中System.IO.StringWriter oStringWriter 
= new System.IO.StringWriter(); 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter); 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中  三、将目标数据绑定到输入流输出
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
this.RenderControl(oHtmlTextWriter); 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中

ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中Response.Write(oStringWriter.ToString());
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中Response.End(); 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中把Excel文件中的数据读入到DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中使用Excel文件做为DataGrid的数据源是非常简单的,一旦数据被装载进来,就可以把数据再保存进SQL Server或XML中。我们只需要简单地使用OLE DB Provider 来访问Excel文件,然后返回DataSet即可。
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中下面是要显示的Excel数据contact.xls:
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中姓名 性别 地址 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中net_lover Male amxh@21cn.com 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中amxh Male amxh@21cn.com 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中孟子 E 章 Male amxh@21cn.com 
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
<%@ Page Language="C#" Debug="true" %>
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
<%@ Import Namespace="System.Data"%>
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
<%@ Import Namespace="System.Data.OleDb"%>
ASP.NET中将数据输出到Excel 和把Excel数据读入DataGrid中
<script runat="server">
#region    导出excel文件    
private void Export_Click(object sender, System.EventArgs e)
{
HttpContext.Current.Response.AppendHeader(
"Content-Disposition""attachment;filename=matArrivedReport.xls"); 
HttpContext.Current.Response.Charset        
=    "gb2312"
HttpContext.Current.Response.ContentEncoding
=    System.Text.Encoding.UTF7;  
HttpContext.Current.Response.ContentType    
=    "application/ms-excel";

System.IO.StringWriter strWrite                
=    new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter htmlWrite        
=    new System.Web.UI.HtmlTextWriter(strWrite);
htmlWrite.Write(
"<table width=\"100%\"><tr><td width=\"100%\" align=\"center\" colspan=\"9\"><span style=\"FONT-SIZE: 28px; FONT-FAMILY: 黑体\"><p>****汇总表</p></span></td></tr></table>");
Repeater1.RenderControl(htmlWrite); 
//这里是活动内容,你可以从窗体中任何控件获取字符流
HttpContext.Current.Response.Write(strWrite.ToString()); 
HttpContext.Current.Response.End(); 


}   
#endregion

相关文章: