我想用包iTextSharp將gridview導出爲pdf格式。iTextSharp生成pdf格式的gridview
我做它在我的.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();
它的工作BUIT如果我添加在接下來的代碼訂製出口:
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);
和Visual Studio說:
「的GridView必須RUNAT =服務器被宣佈」 或者,它阻止用戶:
pdfDoc.Close();
,他說:pdfDoc是空的...
有人有想法嗎?
謝謝,你的鏈接非常有用:) – user2265252