将gridview导出为ex​​cel时如何添加标题? [英] How to add a title when exporting gridview to excel?

查看:81
本文介绍了将gridview导出为ex​​cel时如何添加标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





以下代码将gridview导出为ex​​cel,



Hi,

the code below export gridview into excel,

protected void excelLinkBtn_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=StudentReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.xls";
            StringWriter stringWrite = new StringWriter();          
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
       
            //'Change the Header Row back to white color
            overspeedReportGrid.HeaderRow.Style.Add("background-color", "#FFFFFF");
            
           //'Apply style to Individual Cells 
            overspeedReportGrid.HeaderRow.Cells[0].Style.Add("background-color", "#0080C0");
            overspeedReportGrid.HeaderRow.Cells[1].Style.Add("background-color", "#0080C0");
            overspeedReportGrid.HeaderRow.Cells[2].Style.Add("background-color", "#0080C0");
            overspeedReportGrid.HeaderRow.Cells[3].Style.Add("background-color", "#0080C0");
            overspeedReportGrid.HeaderRow.Cells[4].Style.Add("background-color", "#0080C0");
            
            overspeedReportGrid.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }





如何为excel添加标题?



谢谢。



how to add a title to the excel?

Thanks.

推荐答案





使用这样的东西。



Hi,

Use some what like this.

public void export2(DataTable dt)
{
    System.Web.UI.WebControls.DataGrid grid =
                       new System.Web.UI.WebControls.DataGrid();
    grid.HeaderStyle.Font.Bold = true;
    grid.DataSource = dt;
    grid.DataBind();
    using (StreamWriter sw = new StreamWriter("C:\\Reports\\Report.xls"))
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            **hw.Write("Employee Report");**
            **hw.Write("<br>");**
            grid.RenderControl(hw);
        }
    }
}





也请参阅以下链接。

使用C#创建Excel文档



谢谢



Refer the following link too.
Using C# to Create an Excel Document

Thanks


这篇关于将gridview导出为ex​​cel时如何添加标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆