利用不同的服务器控件将数据导出到Excel文件

<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _Default "  EnableEventValidation = " false "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 利用不同的服务器控件将数据导出到Excel文件 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        
< asp:Button ID = " outExcelBtn "  runat = " server "  Text = " Button "  OnClick = " outExcelBtn_Click "   />
    
</ div >
    
</ form >
</ body >
</ html >
后台代码:
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Text;

public   partial   class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
{
    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }

    
protected void outExcelBtn_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
string filename = "FileFlow.xls";

        OutPutExcel(filename); 
    }


    
public void OutPutExcel(string filename)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        DataTable table 
= new DataTable();
        table.Columns.Add(
"name"typeof(string));
        table.Columns.Add(
"age"typeof(int));
        
for (int i = 0; i < 10; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            DataRow row 
= table.NewRow();
            row[
"name"= "Name_" + i.ToString();
            row[
"age"= 20 + i;
            table.Rows.Add(row);
        }


        DataGrid dg 
= new DataGrid();
        dg.DataSource 
= table;
        dg.DataBind();
        

        
//定义文档类型、字符编码 
        Response.Clear();
        Response.Buffer 
= true;
        Response.Charset 
= "GB2312";
        
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开 
        
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm 
        Response.AppendHeader("Content-Disposition""attachment;filename=" + filename);
        Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("GB2312");
        
//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);

ContractedSubBlock.gifExpandedSubBlockStart.gif        
通过Datagrid导出Excel#region 通过Datagrid导出Excel
        
//dg.RenderControl(oHtmlTextWriter);
        #endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
通过HTMLTable导出Excel#region 通过HTMLTable导出Excel
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*HtmlTable htmlTable = new HtmlTable();
        foreach (DataRow row in table.Rows)
        {
            HtmlTableRow htmlRow = new HtmlTableRow();
            HtmlTableCell cellName = new HtmlTableCell();
            HtmlTableCell cellAge = new HtmlTableCell();
            cellName.InnerText = row["name"].ToString();
            cellAge.InnerText = row["age"].ToString();
            htmlRow.Cells.Add(cellName);
            htmlRow.Cells.Add(cellAge);
            htmlTable.Rows.Add(htmlRow);
        }
        htmlTable.RenderControl(oHtmlTextWriter);
*/

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
通过literal导出Excel#region 通过literal导出Excel
        Literal literal 
= new Literal();
        StringBuilder sb 
= new StringBuilder();
        sb.AppendLine(
@"Name"+"\t"+"Age");
        
foreach (DataRow row in table.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            sb.AppendLine(
@""+ row["name"].ToString() + "\t" + row["age"].ToString());
        }

        literal.Text 
= sb.ToString();
        literal.RenderControl(oHtmlTextWriter);
        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
将本页内容导出为Excel#region 将本页内容导出为Excel
        
//this.RenderControl(oHtmlTextWriter); 
        
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件 
        #endregion


        Response.Write(oStringWriter.ToString());
        Response.End();
    }

}

转载于:https://www.cnblogs.com/windthunder/archive/2009/09/21/1571450.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值