2016-07-15 192 views
0

我试图将c#中的gridview表导出为PDF文件。我已经搜索了其他解决方案,似乎每个人都试图首先从SQL数据库填充表。这一步是不必要的,因为我已经通过活动目录和soforth填充了表格。目前我的代码工作,但它是纯黑色和白色,看起来相当无聊。将GridView导出为PDF格式的c#

这里是(使用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文档中工作。谁能帮忙?

+0

这可能帮助:http://www.pdfsharp.net/wiki/invoice-sample.ashx –

+0

首先,完全删除'Response.Write(pdfDoc);',该行不正确,实际上可能会创建损坏的PDF。其次,'HTMLWorker'已经很老了,不再被支持,并且在iText 5中被替换为'XMLWorker'。前者对CSS只有最基本的支持。请参阅[这个主题很长但内容翔实的帖子](http://stackoverflow.com/a/25164258/231316) –

回答

0

尝试ASPX使用Alternaterowcolors在GridView控件如下,

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> 

Click for futher reference

+0

我已经在我的aspx底部有。 –