2013-06-20 107 views
0

我想用包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是空的...

有人有想法嗎?

回答

0

添加Rendering功能的PDF代碼

public override void VerifyRenderingInServerForm(Control control) 
    { 
     // verifies the control is rendered here 
    } 
1

後添加以下代碼,以避免RUNAT = 「服務器」 的錯誤。

public override void VerifyRenderingInServerForm(Control control) 
{ 
    /* Verifies that the control is rendered */ 
} 

您可能希望禁用分頁,以便將Gridview的所有行導出爲PDF。

在gridview的rendercontrol方法之前添加下面的代碼。

gvReportingStockComp.AllowPaging = false; 
gvReportingStockComp.DataBind(); 

如果您有任何疑問,請參閱以下文章。

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

+0

謝謝,你的鏈接非常有用:) – user2265252