如何使用itextsharp将gridview数据导出为pdf

时间:2014-06-23 08:08:52

标签: c# itextsharp asp.net-4.0

我只想用itextSharp导出网格视图数据。这是我的代码如何将网格视图导出到excel是否有办法使用此技术并使用itextSharp创建表。

这是我的代码:

 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();
    }

我在回答中包含了这个屏幕截图:

enter image description here

这个解决方案对我有用:

  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();

1 个答案:

答案 0 :(得分:4)

试试这种方式

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();
}