2014-06-23 4 views
0

itextSharp를 사용하여 그리드보기 데이터를 내보내고 싶습니다. 여기 내 코드입니다 어떻게 수출 표보기를 Excel 로이 기술을 사용하고 itextSharp를 사용하여 테이블을 만드는 방법입니다.itextSharp를 사용하여 그리드보기 데이터를 PDF로 내보내는 방법

enter image description here

이 솔루션은 나를 위해 작동합니다 : 난 대답 아래이 스크린 샷이 포함

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

:

내 코드는이 방법을 시도

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

데이터를 Excel 또는 PDF로 내보내시겠습니까? –

+0

이것은 표보기 데이터를 내보내는 방법에 대한 코드 샘플입니다. 이제는이 논리를 사용하여 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 문제가 해결 되었습니까? –