简体   繁体   中英

how to export grid view data to pdf using itext sharp

i just want to export grid view data with itextSharp. here is my code how i export grid view to excel is there way to use this technique and create table with using itextSharp.

this is my code :

 protected void ibtnxls1_Click(object sender, ImageClickEventArgs e)
    {
        DataTable dt = (DataTable)Session["PostTable"];
        string fileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "applicssssation/vnd.ms-excel";
        string sTab = "";
        foreach (DataColumn dc in dt.Columns)
        {
            HttpContext.Current.Response.Write(sTab + dc.ColumnName);
            sTab = "\t";
        }
        HttpContext.Current.Response.Write("\n");
        int i;
        foreach (DataRow dr1 in dt.Rows)
        {
            sTab = "";
            for (i = 0; i < dt.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write(sTab + dr1[i].ToString());
                sTab = "\t";
            }
            HttpContext.Current.Response.Write("\n");
        }
        HttpContext.Current.Response.End();
    }

i include this screen shot below answer :

在此处输入图片说明

this solution works for me :

  DataTable dt = (DataTable)Session["PostTable"];
        iTextSharp.text.Table table = new iTextSharp.text.Table(dt.Columns.Count);
        table.Cellpadding = 2;
        table.Width = 100;
        //Transfer rows from GridView to table
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            string cellText = Server.HtmlDecode(dt.Columns[i].ToString());
            iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
            cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#FFFFFF"));
            table.AddCell(cell);
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                string cellText = Server.HtmlDecode(dt.Rows[i][j].ToString());
                iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);

                //Set Color of Alternating row
                if (i % 2 != 0)
                {
                    cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#FFFFFF"));
                }
                table.AddCell(cell);
            }
        }
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(table);
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" +
                                       "filename=" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();

Try this way

protected void btnExportPDF_Click(object sender, EventArgs e)
{
    GridView1.AllowPaging = false;
    GridView1.DataBind();

    BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, true);

    iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count);
    int[] widths = new int[GridView1.Columns.Count];
    for (int x = 0; x < GridView1.Columns.Count; x++)
    {
        widths[x] = (int)GridView1.Columns[x].ItemStyle.Width.Value;
        string cellText = Server.HtmlDecode(GridView1.HeaderRow.Cells[x].Text);

        //Set Font and Font Color
        iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
        font.Color = new Color(GridView1.HeaderStyle.ForeColor);
        iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));

        //Set Header Row BackGround Color
        cell.BackgroundColor = new Color(GridView1.HeaderStyle.BackColor);


        table.AddCell(cell);
    }
    table.SetWidths(widths);

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < GridView1.Columns.Count; j++)
            {
                string cellText = Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text);

                //Set Font and Font Color
                iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
                font.Color = new Color(GridView1.RowStyle.ForeColor);
                iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));

                //Set Color of row
                if (i % 2 == 0)
                {
                    //Set Row BackGround Color
                    cell.BackgroundColor = new Color(GridView1.RowStyle.BackColor);
                }

                table.AddCell(cell);
            }
        }
    }

    //Create the PDF Document
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    pdfDoc.Add(table);
    pdfDoc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM