Sizy 2015-05-15 02:22 采纳率: 0%
浏览 783

Asp.net 导出Excel 我直接绑定查询方法 请问如何添加页眉页脚

protected void Button2_Click(object sender, EventArgs e)
{
    string zhilingName = this.TxtCode.Text;
    MakeFile();
    //System.IO.StreamWriter sw = new StreamWriter(@"d:\"+DateTime.Now.Month.ToString()+@"\"+zhilingName+".xls");
    //System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);

    //打印方法
    DataTable dt = ZhiDAL.getFreeDetaliPrint(zhilingName, "5");

    System.Web.HttpContext curContext = System.Web.HttpContext.Current;
    if (dt != null)
    {
        curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//改成utf-8
        curContext.Response.ContentType = "application/excel";
        curContext.Response.ClearContent();
        curContext.Response.AddHeader("content-disposition", "attachment; filename=" + zhilingName + "-" + DateTime.Now.ToShortDateString() + ".xls");

        //导出excel文件
        System.IO.StringWriter sws = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sws);
        GridView dg = new GridView();
        dg.DataSource = dt.DefaultView;
        dg.DataBind();
        dg.RenderControl(hw);
        dg.Dispose();
        curContext.Response.Write(sws.ToString());

        try
        {
            //在指定文件夹下 保存
            // DateTime.Now.Month.ToString()
            System.IO.StreamWriter sw = new StreamWriter(@"d:\" +DateTime.Now.ToString("yyyy-MM") + @"\" + zhilingName + ".xls");
            sw.Write(sws.ToString());
            sw.Close();
        }
        catch (Exception ex)
        {
        }
        //hw = new System.Web.UI.HtmlTextWriter(sws);

        curContext.Response.End(); 

    }


}
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-03 21:31
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要为ASP.NET应用程序添加页眉和页脚到导出的Excel文件中,您可以在MakeFile()函数中使用以下步骤:

    1. 创建一个HTML文本输出流,并将其转换为StringWriter对象。
    2. 使用HtmlTextWriter创建一个新的HtmlTextWriter并设置其属性以适配您的需求。
    3. 将GridView控件的DataBind方法重写为使用此HtmlTextWriter,以便在页面上显示内容。

    以下是修改后的示例代码:

    public partial class ExportExcel : Page
    {
        protected void Button2_Click(object sender, EventArgs e)
        {
            string zhilingName = this.TxtCode.Text;
            MakeFile();
            //...
            
            using (StreamWriter sw = new StreamWriter(@"d:\"+DateTime.Now.ToString("yyyy-MM")+"\"+zhilingName+".xls"))
            {
                using (var htmlTextWriter = new HtmlTextWriter(sw))
                {
                    // 使用HtmlTextWriter渲染GridView的内容
                    GridView dg = new GridView();
                    dg.DataSource = dt.DefaultView;
                    dg.DataBind(htmlTextWriter);
                    
                    // 渲染完后关闭htmlTextWriter
                    htmlTextWriter.Close();
                }
            }
    
            // 在特定文件夹下保存生成的文件
            try
            {
                using (var sw = new StreamWriter(@"d:\\" + DateTime.Now.ToString("yyyy-MM") + @"\\" + zhilingName + ".xls"))
                {
                    sw.WriteLine(sws.ToString());
                }
            }
            catch (Exception ex)
            {
            }
    
            Response.End(); 
        }
    
        private void MakeFile()
        {
            // ...
        }
    }
    
    // 确保您的GridView控件实现了PageDataSource属性
    <asp:GridView ID="dg" runat="server" DataSourceID="ds" AutoGenerateColumns="false">
        <RowStyle Width="Auto"/>
    </asp:GridView>
    

    在这个例子中,我们使用了HtmlTextWriter来处理GridView的数据。当GridView数据被遍历时,它会自动调用RenderControl方法,从而将渲染结果写入到StreamWriter中。这样,我们就能够控制页眉和页脚的布局。

    评论

报告相同问题?