paul_vin Ответов: 2

Я хочу скачать gridview в формате PDF


Я хочу скачать grid view в формате pdf но когда я нажимаю на кнопку это просто освежает страницу
но не скачивая gridview в формате pdf




в блоке catch он показывает исключение как


System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.AbortInternal()
   at System.Threading.Thread.Abort(Object stateInfo)
   at System.Web.HttpResponse.AbortCurrentThread()
   at System.Web.HttpResponse.End()
   at User_WorkOrder.btnPrint_Click(Object sender, EventArgs e) in c:\Users\DELL\Desktop\Vinayak\VCHEM Current Updated\User\WorkOrder.aspx.cs:line 2553


Что я уже пробовал:

У меня есть страница ASPX следующим образом

<asp:GridView ID="gvpdf" AutoGenerateColumns="False" runat="server" HeaderStyle-BackColor="dimgray" HeaderStyle-ForeColor="white" Width="100%" PageSize="100" Font-Size="15px" CellPadding="2" AllowPaging="True" BackColor="#F8F8F8" BorderStyle="None" OnRowDataBound="gvWorkOrder_RowDataBound">
                       <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                       <Columns>
                           <asp:BoundField HeaderStyle-Width="70px" HeaderText="Work_Order_Id" DataField="Work_Order_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide">
                               <FooterStyle BackColor="#CCCCCC" />
                               <HeaderStyle></HeaderStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" />
                           </asp:BoundField>

                           <%--<asp:BoundField HeaderStyle-Width="150px" HeaderText="Package  Name" ItemStyle-HorizontalAlign="Center" DataField="PackageName" />--%>
                           <%--<asp:TemplateField HeaderText="Sr. No.">
                                                   <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                                                   <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                                                   <ItemTemplate>
                                                       <%#Container.DataItemIndex+1 %>
                                                   </ItemTemplate>
                                                   <ItemStyle Width="80px" />
                                               </asp:TemplateField>--%>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Sample No" DataField="Sample_No">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="200px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Sample Reg. No" DataField="Work_Order_No">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="200px" />
                           </asp:BoundField>

                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Customer Name" DataField="Customer_Name">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="300px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Date" DataField="Date">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Delivery Date" DataField="Delivery_Date">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="D Challan No" DataField="Customer_D_Challan_No">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="250px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Challan Date" DataField="Challan_Date">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="No of Sample" DataField="Quantity">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Name for Analysis Print" DataField="Name_for_Analysis_Print">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Remark" DataField="Remark">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="IsFerrous" DataField="IsFerrous" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:ButtonField ButtonType="Link" CausesValidation="false" HeaderText="Edit" ControlStyle-ForeColor="blue" ControlStyle-Width="50px" CommandName="Select" Text="Edit">
                               <ControlStyle ForeColor="Blue"></ControlStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="50px" />
                           </asp:ButtonField>
                           <asp:TemplateField HeaderText="Delete">
                               <ControlStyle ForeColor="Blue"></ControlStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="50px" />
                               <ItemTemplate>
                                   <asp:LinkButton ID="lnkDelete" OnClientClick="javascript:scroll(0,0);" runat="server" CommandName="cmdDelete">Delete</asp:LinkButton>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Customer Id" DataField="Customer_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide" />
                           <asp:TemplateField HeaderText="Print Work Order">
                               <ControlStyle ForeColor="Blue"></ControlStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="50px" />
                               <ItemTemplate>
                                   <asp:LinkButton ID="lnkPrint" runat="server" CommandName="cmdPrint">Print</asp:LinkButton>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="Print Internal Challan">
                               <ControlStyle ForeColor="Blue"></ControlStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="50px" />
                               <ItemTemplate>
                                   <asp:LinkButton ID="lnkPrintchallan" runat="server" CommandName="cmdPrintChallan">Print</asp:LinkButton>
                               </ItemTemplate>
                           </asp:TemplateField>

                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Uploaded_Doc" DataField="Uploaded_Doc">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" CssClass="hidden"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" CssClass="hidden" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="IsNon_NABL" DataField="IsNon_NABL" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide" />

                       </Columns>

                       <EditRowStyle BorderColor="#CCCCCC" />

                       <HeaderStyle BackColor="DimGray" ForeColor="White"></HeaderStyle>

                       <PagerSettings PageButtonCount="5" />

                       <PagerStyle HorizontalAlign="Right" CssClass="GridPager" />
                   </asp:GridView>


страница aspx.cs выглядит следующим образом


DataTable dt1 = new DataTable();
dt1 = (DataTable)ViewState["DataTable"];
gvpdf.DataSource = dt1;
gvpdf.DataBind();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=WorkOrder.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter s_w = new StringWriter();
HtmlTextWriter h_w = new HtmlTextWriter(s_w);
gvpdf.RenderControl(h_w);
gvpdf.HeaderRow.Style.Add("width", "15%");
gvpdf.HeaderRow.Style.Add("font-size", "10px");

gvpdf.Style.Add("text-decoration", "none");
gvpdf.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvpdf.Style.Add("font-size", "8px");

StringReader sr = new StringReader(s_w.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//  Paragraph title = new Paragraph("Delta Metallurgical Laboratory & Services", new Font(Font.FontFamily.TIMES_ROMAN, 14f, Font.BOLD));
//    title.Alignment = Element.ALIGN_CENTER;
//    pdfDoc.Add(title);
//    pdfDoc.Add(new Paragraph(" Unpaid Bill Report", new Font(Font.FontFamily.TIMES_ROMAN, 12f, Font.BOLD)));
//   pdfDoc.Add(new Paragraph("."));
htmlparser.Parse(sr);

pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

Matias Lopez

Может быть, это просто, но я спрашиваю вас: вы использовали GridView в UpdatePanel? И Сохранить PDF-файл с многопоточностью? Может ли тайм-аут быть проблемой?

2 Ответов

Рейтинг:
1

Gerry Schmitz

Печать в "драйвере печати PDF".

Может быть сделано клиентом (самим клиентом); или на стороне сервера.

Полученный файл можно "забрать" откуда угодно и для чего угодно.


Рейтинг:
0

Member 12893295

https://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx[^]


Это вам поможет.просим проверить его.