【问题标题】:Change the Color of header when exporting gridview to PDf将 gridview 导出为 PDf 时更改标题的颜色
【发布时间】:2014-08-12 21:17:19
【问题描述】:

我正在将 gridview 导出为 pdf。一切正常,但是,当生成 PDF 时,背景为白色,标题字体颜色为灰色。真的很难看。我正在尝试在导出之前将标题前景色更改为黑色。它不工作。

有什么建议吗?

  public void ExportToPDF()
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=CallDetail.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        this.GetData();
        GridView1.RenderControl(hw);
        HtmlForm frm = new HtmlForm();
        frm.Attributes["runat"] = "server";
        GridView1.HeaderRow.Style.Add("width", "15%");
        GridView1.HeaderRow.Style.Add("font-size", "10px");
        GridView1.Style.Add("text-decoration", "none");
        GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        GridView1.Style.Add("font-size", "26px");
        for (int col = 0; col < GridView1.HeaderRow.Controls.Count; col++)
        {
            TableCell tc = GridView1.HeaderRow.Cells[col];
            tc.Style.Add("color", "#FFFFFF");
            tc.Style.Add("background-color", "#444");
        }
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();


    }

UPDATE*** 添加了更新的代码和屏幕截图。

【问题讨论】:

    标签: c# asp.net pdf gridview colors


    【解决方案1】:

    在导出前直接设置表头的ForeColor 并不会真正影响表头样式。可能的替代方法是遍历所有标题单元格并设置为您需要的任何样式(这确实有效)。您可以尝试更改以下代码行

    GridView1.HeaderStyle.ForeColor = System.Drawing.Color.Black;
    

    for (int col = 0; col < GridView1.HeaderRow.Controls.Count; col++)
    {
        TableCell tc = GridView1.HeaderRow.Cells[col];
        tc.Style.Add("color", "#FFFFFF");
        tc.Style.Add("background-color", "#444");
    }
    

    【讨论】:

    • 感谢您的回复。我应用了以下代码,但输出仍然相同。
    • 我更新了我的帖子。它现在包含最新的代码和屏幕截图。
    • @DennisR 谢谢这对我有帮助 请问如何更改边框颜色?
    • @User6675636b20796f7521 你可以像tc.Style.Add("border-color", "#cfdbe2");这样添加边框颜色或尝试像这样tc.Style.Add("border", "1px solid #333333");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多