.NET技术支持者

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::
利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM等类型的文档很容易的。主要分为三步来完成。

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

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(); 

把Excel文件中的数据读入到DataGrid中

使用Excel文件做为DataGrid的数据源是非常简单的,一旦数据被装载进来,就可以把数据再保存进SQL Server或XML中。我们只需要简单地使用OLE DB Provider 来访问Excel文件,然后返回DataSet即可。
下面是要显示的Excel数据contact.xls:


姓名 性别 地址 
net_lover Male amxh@21cn.com 
amxh Male amxh@21cn.com 
孟子 E 章 Male amxh@21cn.com 


<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<script runat="server">
private DataSet CreateDataSource(){
string strConn;
strConn 
= "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\Inetpub\\wwwroot\\contacts.xls;"+
"Extended Properties=Excel 8.0;";
OleDbConnection conn 
= new OleDbConnection(strConn);
OleDbDataAdapter myCommand 
= new OleDbDataAdapter("SELECT * FROM [ContactList$]", strConn);
DataSet myDataSet 
= new DataSet();
myCommand.Fill(myDataSet);
return myDataSet;
}


public void Page_Load(Object sender, EventArgs e){
if (!IsPostBack) {
mygrid.DataSource 
= CreateDataSource();
mygrid.DataBind();
}
 
}


</script>

<center>
<form runat="server">
<asp:datagrid runat="server" AutoGenerateColumns="false" 
width
="500" id="mygrid">
<Headerstyle BorderColor="White" BackColor="black" 
ForeColor
="White" 
Font
-Bold="True" 
Font
-Name="Arial" 
Font
-Size="9" HorizontalAlign="Center"/>
<Itemstyle   BorderColor="" 
BackColor
="#FFFFF0" 
ForeColor
="Black" 
Font
-Name="Arial" 
Font
-Size="8" 
Font
-Bold="False" HorizontalAlign="Center"/>
<Columns>
<asp:BoundColumn HeaderText="姓名" ReadOnly="true" DataField="姓名"/>
<asp:BoundColumn HeaderText="性别" ReadOnly="true" DataField="性别"/>
<asp:BoundColumn HeaderText="Email" ReadOnly="true" DataField="地址"/>
</Columns>
</asp:datagrid>
</form>
只需要指定Excel路径,并用[]选择一个工作表即可。
#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
/Files/ghd258/tool.rar
posted on 2005-10-19 18:02  LDAR泄漏检测与修复  阅读(750)  评论(0编辑  收藏  举报