iTextSharp的,以生成PDF一个gridview [英] iTextSharp to generate a gridview in pdf

查看:257
本文介绍了iTextSharp的,以生成PDF一个gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想导出一个gridview为PDF包iTextSharp的。

I'm trying to export a gridview to pdf with package iTextSharp.

我要尽我的.aspx文件:

I'm doing it in my .aspx files :

<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>

和我的$ C $落后C,我填的是GridView控件:

And in my code behind, I fill the 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如果我补充下code,以使出口:

It work buit if I add the next code in order to make the export :

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 =服务器

and visual studio says : "GridView must be declared in with runat=server"

或者,它阻止到:

pdfDoc.Close();

他说:pdfDoc是空的......

and he says : pdfDoc is empty...

有人有一个想法吗?

推荐答案

在下面添加code,以避免=服务器的错误。

Add below code to avoid runat="server" error.

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

您可能需要禁用分页,使其出口的GridView的所有行为PDF。

You may want to disable paging so that it exports all rows of Gridview to PDF.

在下面添加code之前GridView的rendercontrol方法。

Add below code before gridview's rendercontrol method.

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

柜面你已经有任何疑问,请参考下面的文章。

Incase you've any doubt, refer below article.

<一个href="http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx" rel="nofollow">http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx

这篇关于iTextSharp的,以生成PDF一个gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆