我正在将gridview导出为pdf。但是,一切正常,生成PDF时背景为白色,标题字体颜色为灰色。这真的很难看。我想在导出之前将标题forecolor更改为黑色。它不起作用。
有什么建议吗?
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();
}
更新***添加了更新的代码和屏幕截图。
答案 0 :(得分:2)
导出前直接设置标题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");
}