我正在尝试使用 iTextSharp 包将 gridview 导出为 pdf。
我在我的 .aspx 文件中这样做:
<form id="formOptions" runat="server">
[...]
<asp:GridView ID="gvReportingStockComp" runat="server" AutoGenerateColumns="false" Visible="true">
<Columns>
<asp:BoundField DataField="cod_wo" HeaderText="N° OF" />
<asp:BoundField DataField="composant" HeaderText="Composant" />
<asp:BoundField DataField="BESOIN" HeaderText="Besoin/OF" />
<asp:BoundField DataField="BESOIN_T" HeaderText="Besoin total" />
<asp:BoundField DataField="stock_dispo" HeaderText="Stock dispo" />
<asp:BoundField DataField="QTE_RESTANTE" HeaderText="Qte restante" />
</Columns>
</asp:GridView>
</form>
在我后面的代码中,我填充了 gridview :
OracleConnection oConnexion = new OracleConnection();
oConnexion.ConnectionString = "X";
oConnexion.Open();
string reqStockCompTotal = "intitulé de ma requete"
OracleCommand cmdReqStockComp = new OracleCommand(reqStockCompTotal);
cmdReqStockComp.Connection = oConnexion;
OracleDataReader readerReqStockComp = cmdReqStockComp.ExecuteReader();
gvReportingStockComp.DataSource = readerReqStockComp;
gvReportingStockComp.DataBind();
oConnexion.Close();
oConnexion.Dispose();
如果我添加下一个代码以进行导出,它将起作用:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestMES.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvReportingStockComp.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();
在第一手它阻塞:
gvReportingStockComp.RenderControl(hw);
和视觉工作室说:“GridView 必须用 runat=server 声明”
或者它阻止:
pdfDoc.Close();
他说:pdfDoc是空的......
有人有想法吗?