2014-01-30 114 views
0
protected void ExportToPdf(DataTable dt, string str) 
     { 
      ArrayList ADA = new ArrayList(); 
      ADA.Add(FirstAssignment.SelectedItem); 
      ADA.Add(SecondAssignment.SelectedItem); 
      GridView GridView1 = new GridView(); 
      GridView GridView2 = new GridView(); 
      GridView1.ShowHeaderWhenEmpty = true; 
      GridView1.AllowPaging = false; 
      GridView1.DataSource = dt; 

      GridView1.DataBind(); 
      int i1 = GridView1.Columns.Count; 
      GridView1.HeaderRow.Cells[1].Text = ADA[0].ToString(); 
      GridView1.HeaderRow.Cells[2].Text = ADA[1].ToString(); 
      GridView1.HeaderRow.Cells[3].Text = "% of Improvement from " + (ADA[0].ToString()) + " to " + (ADA[1].ToString()); 
      HttpContext.Current.Response.Clear(); 
      HttpContext.Current.Response.Buffer = true; 
      HttpContext.Current.Response.ContentType = "application/pdf"; 
      HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + str + ".pdf"); 

      HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      StringWriter sw = new StringWriter(); 
      HtmlTextWriter hw = new HtmlTextWriter(sw); 


      GridView1.AllowPaging = false; 
      GridView1.HeaderRow.ForeColor = System.Drawing.Color.Black; 
      GridView1.FooterRow.ForeColor = System.Drawing.Color.Black; 

      GridView1.HeaderRow.Style.Add("font-Color", "Black"); 
      GridView1.HeaderRow.Style.Add("font-size", "13px"); 
      GridView1.HeaderRow.Style.Add("text-decoration", "none"); 
      GridView1.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;"); 

      GridView1.Style.Add("font-Color", "Black"); 
      GridView1.Style.Add("text-decoration", "none"); 
      GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;"); 
      GridView1.Style.Add("font-size", "11px"); 
      GridView1.ForeColor = System.Drawing.Color.Black; 
      Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f); 
      GridView1.RenderControl(hw); 

      StringReader sr = new StringReader(sw.ToString()); 
      HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 

      // HTMLWorker htmlparser1 = new HTMLWorker(pdfDoc); 
      PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream); 
      pdfDoc.Open(); 
      pdfDoc.Add(new Paragraph("hey! rashmi")); 
      htmlparser.Parse(sr); 

      pdfDoc.Close(); 
      HttpContext.Current.Response.Write(pdfDoc); 
      HttpContext.Current.ApplicationInstance.CompleteRequest(); 
     } 

這是用於創建一個網格視圖。任何人都可以幫助我使用此代碼創建一個動態網格視圖。我試圖給出一個for循環GridView1.RenderControl(hw);但我得到相同的數據單網格視圖。提前感謝。使用itextsharp以pdf格式動態創建網格視圖

+0

意味着你要GridView控件PDF? –

+0

yes.i已創建one.i要動態創建gridview – user3239173

+0

它是動態的目標是什麼?你有多個數據集? –

回答

0

如果你想顯示PDF GridView控件,可以顯示爲表也在這裏是 代碼:

 public void createPdf() 
      { 
      // step 1 
       using (MemoryStream ms = new MemoryStream()) 
       { 
        Document document = new Document(iTextSharp.text.PageSize.A4, 10, 22, 34, 34); 
        // step 2 
        PdfWriter writer = PdfWriter.GetInstance(document, ms); 
        // step 3 
        document.Open(); 
        MemoryStream stream = new MemoryStream(); 
        // step 4 how many tables you want to create 
        for (int i = 0; i < 5; i++) 
        { 
         document.Add(new Paragraph("Table:")); 
         document.Add(createFirstTable()); 

        }      
// step 5 
        document.Close(); 
        writer.Close(); 
        Response.ContentType = "pdf/application"; 
        Response.AddHeader("content-disposition", 
        "attachment;filename=First PDF document.pdf"); 
        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); 
       } 
      } 

      public static PdfPTable createFirstTable(DataTable dt) 
      { 


       // a table with three columns 
       PdfPTable table = new PdfPTable(dt.Columns.Count); 
       // the cell object 
       PdfPCell cell; 
       // we add a cell with colspan 3 
       //cell = new PdfPCell(new Phrase("Cell with colspan 3")); 


       table.AddCell(cell); 
       //// now we add a cell with rowspan 2 
       cell = new PdfPCell(new Phrase("Cell with rowspan 2")); 

        table.AddCell("ADXHGS"); 
       table.AddCell("WFEWSA"); 
       table.AddCell("EWSFCEDSW"); 
       table.AddCell("EWSEWSFDFCEDSW"); 



       //PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table    

       return table; 
      }