[英]Exporting GridView to PDF in c#
我正在嘗試將c#中的gridview表導出為PDF文件。 我搜索了其他解決方案,似乎每個人都在嘗試首先從SQL數據庫填充表。 這一步是不必要的,因為我已經通過活動目錄填寫了表格。 目前,我的代碼可以正常工作,但是它是純黑白的,看上去很乏味。
這是我當前的代碼(使用iTextSharp):
protected void ExportToPDF()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
grdvList.AllowPaging = false;
// grdvList.DataBind();
grdvList.RenderBeginTag(hw);
grdvList.HeaderRow.RenderControl(hw);
foreach (GridViewRow row in grdvList.Rows)
{
row.RenderControl(hw);
}
grdvList.FooterRow.RenderControl(hw);
grdvList.RenderEndTag(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
而我的GridView:
<asp:GridView ID="grdvList" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Name" datafield="Name" ReadOnly="True" >
<ItemStyle Width="17.5%" />
</asp:BoundField>
<asp:BoundField HeaderText="Phone Ext" datafield="Phone Ext" ReadOnly="True" >
<ItemStyle Width="11.5%" />
</asp:BoundField>
<asp:BoundField HeaderText="Mobile" datafield="Mobile" ReadOnly="True" >
<ItemStyle Width="16%" />
</asp:BoundField>
<asp:BoundField HeaderText="Email" datafield="Email" ReadOnly="True" >
<ItemStyle Width="47.5%" />
</asp:BoundField>
<asp:BoundField HeaderText="Department" DataField="Department" ReadOnly="True" >
<ItemStyle Width="17.5%" />
</asp:BoundField>
</Columns>
<alternatingrowstyle backcolor="#D6D6D6" />
</asp:GridView>
我想為所有表格單元格添加邊框,使第二行具有灰色背景。 它在我的網頁上效果很好,但我也需要在PDF文檔中使用它。 有人可以幫忙嗎?
嘗試按以下方式在aspx的gridview中使用Alternaterowcolors,
1. Alternative color for Gridview rows
</div>
<br />
<asp:GridView ID="GridVwRowcolorchange" runat="server" AutoGenerateColumns="False"
Font-Names="Verdana" PageSize="5" Width="75%" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px">
<AlternatingRowStyle BackColor="#BFE4FF" />
<PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="15px" BorderColor="#CCCCCC"
BorderStyle="Solid" BorderWidth="1px" />
<RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" />
<Columns>
<asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" />
<asp:BoundField DataField="Emp_id" HeaderText="Employee ID" />
<asp:BoundField DataField="Emp_job" HeaderText="Job title" />
<asp:BoundField DataField="Emp_Dep" HeaderText="Department" />
</Columns>
</asp:GridView>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.