none
页面gridview导出问题 RRS feed

答案

  • 你好,

    我建议你可以使用iTextSharp插件。

    这个插件可以从VS的管理Nuget程序包直接下载下来。

    直接搜索就行了。

    然后你可以使用如下代码来实现你的需求:

    ASXP:
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
                    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                    <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
                    <asp:BoundField DataField="CityId" HeaderText="CityId" SortExpression="CityId" />
                    <asp:BoundField DataField="ClassId" HeaderText="ClassId" SortExpression="ClassId" />
                    <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MysqlConnectionString %>" SelectCommand="SELECT * FROM [Userinfo]"></asp:SqlDataSource>
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出PDF" />
        </div>
    

    按钮代码:
     public override void VerifyRenderingInServerForm(Control control)
            {
                /* Verifies that the control is rendered */
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                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);
                GridView1.AllowPaging = false;
                GridView1.DataBind();
                GridView1.RenderControl(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();
            }



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已建议为答案 Brando Zhang 2016年11月11日 6:31
    • 已标记为答案 Anne_Wx 2016年11月14日 2:35
    2016年11月3日 11:51