2014-06-23 94 views
0

我只是想用itextSharp導出網格視圖數據。這裏是我的代碼如何將網格視圖導出爲Excel有辦法使用此技術並使用itextSharp創建表。如何使用itext將網格視圖數據導出爲pdf

這是我的代碼:

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

你想將你的數據導出爲ex​​cel還是pdf? –

+0

這是我如何將網格視圖數據導出爲ex​​cel的代碼示例,現在我只是想使用此邏輯導出爲pdf。 – Shal

回答

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

但這隻給我列的數據不是所有的行 – Shal

+0

@Shal我編輯我的代碼,請檢查它 –

+0

嘿@Shal你的問題解決了嗎? –

相關問題