在web中如何实现生成EXCEL表格?我想实现把数据填充到excel模板文件中去,把生成的临时EXCEL
文件提供给客户端下载!希望大家提供一些例子!谢谢大家

解决方案 »

  1.   

    将数据放入DataGrid然后导出到文件
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    {
    DataGrid1.DataSource =Panpipe.Common.TestDataBase.Instance.TestTable;
    DataGrid1.DataBind();
    }
    }
    public static void ToExcel(System.Web.UI.Control ctl,string FileName)
    {
    HttpContext.Current.Response.Charset ="UTF-8";
    HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
    HttpContext.Current.Response.ContentType ="application/ms-excel";
    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+FileName+".xls");
    ctl.Page.EnableViewState =false;
    System.IO.StringWriter  tw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
    ctl.RenderControl(hw);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();

    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    ToExcel(DataGrid1,"excel1");
    }
      

  2.   

    生成/读取(反向更新数据库) Excel文件(示例代码下载):
    http://blog.csdn.net/ChengKing/archive/2005/11/29/539514.aspx