Specify which columns to export to pdf from GridView using iTextSharp

戏子无情 提交于 2020-01-06 07:09:12

问题


I started using iTextSharp to export my data from my grid to a PDF document.

As part of my grid I have columns which are not visible, however iTextSharp still export those columns which are invisible in the gridview to the PDF document.

How can I specify which columns on my GridView to export to the PDF document so that the columns which are "visible=false" do not get displayed??

This is what I have so far:

protected void btnExport_Click(object sender, EventArgs e)
{
    PdfPTable pdfTable = new PdfPTable(gvSchedule.HeaderRow.Cells.Count);
    foreach (GridViewRow gridViewRow in gvSchedule.Rows)
    {
        foreach (TableCell tableCell in gridViewRow.Cells)
        {
            if (tableCell.Text == "CenterName")
            {
                continue;
            }

            PdfPCell pdfCell = new PdfPCell(new Phrase(tableCell.Text));
            pdfTable.AddCell(pdfCell);
        }
    }
    Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    PdfWriter.GetInstance(pdfDocument, Response.OutputStream);

    pdfDocument.Open();
    pdfDocument.Add(pdfTable);
    pdfDocument.Close();

    Response.ContentType = "application/pdf";
    Response.AppendHeader("content-disposition", "attachment;filename=mySchedule.pdf");
    Response.Write(pdfDocument);
    Response.Flush();
    Response.End();
}

The two columns that I DO NOT want to display on my PDF document are:

 <asp:BoundField DataField="Address" visible="false" HeaderText="Address" SortExpression="Address"/>

 <asp:BoundField DataField="Area" visible="false" HeaderText="Area" SortExpression="Area"/>

Please advise on how I can export the data in my GridView to PDF using iTextSharp without the two columns mentioned above.

来源:https://stackoverflow.com/questions/48465254/specify-which-columns-to-export-to-pdf-from-gridview-using-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!