将选定的(选中的)网格视图行导出为 PDF 格式 ASP.Net 的 PDF

本文关键字:PDF ASP Net 格式 视图 网格 | 更新日期: 2023-09-27 18:30:17

将选定的(选中的)GridView 行导出为 PDF 格式的 PDF 格式 ASP.Net 找不到图像路径的一部分如何解决此错误 ti 一次又一次地显示错误,但它可以在没有图像的情况下工作 如何将选定的网格视图转换为pdf?出错?下面的代码显示了此错误

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="624px" CssClass="grid" OnRowCommand="OnRowCommand"
        AllowPaging="True" AllowSorting="True" BackColor="White"  OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
        BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" PageSize = "5" 
        OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">          
            <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
        <asp:CheckBox ID="chkHeader" runat="server" onclick="CheckAll(this)"/>
            </HeaderTemplate>
            <ItemTemplate>
            <asp:CheckBox ID="chkchild" runat="server" />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="updatedby" HeaderText="updatedby" SortExpression="updatedby" />
            <asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
            <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
            <asp:BoundField DataField="mail" HeaderText="mail" SortExpression="mail" />
            <asp:BoundField DataField="imagename" HeaderText="imagename" SortExpression="imagename" />  
            <asp:ImageField DataImageUrlField="uploadimage" HeaderText="uploadimage" ControlStyle-Width = "60" ControlStyle-Height = "100">                
            </asp:ImageField>              
            <asp:CommandField ShowEditButton="True" />                
                <asp:TemplateField HeaderText="Delete" >
                    <ItemTemplate>                  
               <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/delete.jpg"
                   OnClientClick="return confirm('Are you sure you want to delete?')" Text="Delete" CommandName="Delete" />                              
                    </ItemTemplate>   
               </asp:TemplateField>
                <asp:TemplateField HeaderText="Update">
                    <ItemTemplate>
                       <asp:LinkButton ID="link" runat="server" Text="Choose" CssClass="linkbutton" CommandName="Choose"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Print">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td><asp:ImageButton ID="excel" runat="server" ImageUrl="images/excel-icon.png" OnClick="excel_Click" /></td>
                                <td> <asp:ImageButton ID="pdf" runat="server" ImageUrl="images/PDF-icon.png" OnClick="pdf_Click1"/></td>
                                <td><asp:ImageButton ID="word" runat="server" ImageUrl="images/MS-Office-2003-Word-icon.png" OnClick="word_Click"/></td>
                            </tr>
                        </table>        
                      </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Report">
                    <ItemTemplate>
                        <asp:Button ID="Button5" runat="server" Text="Get details" BackColor="#33CCFF" CssClass="getdetails" OnClick="Button5_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>
        </Columns>
</asp:GridView>

C# 代码

       using (StringWriter sw = new StringWriter())
       {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
         GridView1.Columns[0].Visible = false;   
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {`
                    row.Visible = (row.FindControl("chkchild") as CheckBox).Checked;   
                }
            }
            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.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    }

将选定的(选中的)网格视图行导出为 PDF 格式 ASP.Net 的 PDF

您忘记了 ImageUrls 中的波浪号 "~",请检查并更改 ImageButton 中的代码

<asp:ImageButton ID="excel" runat="server" ImageUrl="~/images/excel-icon.png" OnClick="excel_Click" />