简体   繁体   中英

Exporting GridView to PDF in c#

I'm trying to export a gridview table in c# to a PDF file. I have searched other solutions, and it appears that everyone is trying to fill the table from an SQL database first. This step is unnecessary, as I have already filled the table via active directory and soforth. Currently my code works, however it's plain black and white and looks rather dull.

Here is my current code (using 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();
    }

And my 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>

I'd like to add a border for all table cells, making every second row with a grey background. It's working great on my webpage, but I need it to work in my PDF document as well. Can anyone help?

Try to use Alternaterowcolors in gridview in aspx as follows,

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM