繁体   English   中英

在C#中将GridView导出为PDF

[英]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.

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