2011-11-09 61 views
2

我目前正試圖將gridview導出爲pdf文件。爲什麼table.addcell(單元格)將只顯示html代碼int iTextSharp

我的代碼是:

public void GvExportPDF(GridView gvListing, string fileName,int TotalColumns) 
{ 
     http.Response.ContentType = "application/pdf"; 

     http.Response.AddHeader("content-disposition", "attachment;filename= " + fileName + ".pdf"); 

     http.Response.Cache.SetCacheability(HttpCacheability.NoCache); 

     StringWriter sw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 

     gvListing.AllowPaging = false; 

     PdfPTable table = new PdfPTable(TotalColumns); 

     foreach (GridViewRow rows in gvListing.Rows) 
     { 
      if (rows.RowType == DataControlRowType.Header) 
      { 
       for (int i = 0; i < rows.Cells.Count; i++) 
       { 
        PdfPCell cell = new PdfPCell(); 
        cell.Phrase = new Phrase(getCellText(rows.Cells[i])); 
        table.AddCell(cell); 
       } 
      } 
      else if (rows.RowType == DataControlRowType.DataRow) 
      { 

       System.Web.UI.WebControls.Image Image1 = (System.Web.UI.WebControls.Image)rows.FindControl("Image1"); 
       string url = Image1.ImageUrl; 
       Image1.Visible = false; 

       iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(new Uri(url)); 
       jpg.ScaleToFit(8f, 10f); 
       jpg.Border = Rectangle.NO_BORDER; 
       for (int i = 0; i < rows.Cells.Count; i++) 
       { 
        if (i == 0) 
         table.AddCell(jpg); 
        else 
        { 

         PdfPCell cell = new PdfPCell(new Phrase(HttpContext.Current.Server.HtmlDecode(getCellText(rows.Cells[i])))); 


         table.AddCell(cell); 
        } 
       } 

      } 
     } 
     //gvListing.RenderControl(hw); 
     StringReader sr = new StringReader(sw.ToString()); 
     Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); 
     HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
     PdfWriter.GetInstance(pdfDoc, http.Response.OutputStream); 
     try 
     { 
      pdfDoc.Open(); 
      pdfDoc.Add(table); 
     } 
     catch (DocumentException dex) 
     { 
      //http.Response.Write(dex.Message); 
      System.Diagnostics.Debug.WriteLine(dex.Message); 
     } 
     catch (IOException ioex) 
     { 
      //http.Response.Write(ioex.Message); 
      System.Diagnostics.Debug.WriteLine(ioex.Message); 
     } 
     catch (Exception ex) 
     { 
      //http.Response.Write(ex.Message); 
      System.Diagnostics.Debug.WriteLine(ex.Message); 
     } 

     finally 
     { 
      pdfDoc.Close(); 
      //http.Response.Write(StyleOfExportTable()); 
      http.Response.Output.Write(pdfDoc); 
      http.Response.End(); 
     } 
    } 

    private string getCellText(TableCell cell) 
    { 
     StringBuilder sb = new StringBuilder(cell.Text); 

     foreach (Control c in cell.Controls) 
     { 
      if (c.Visible) 
      { 
       string controlText = getTextProperty(c); 
       if (controlText != null) 
       { 
        if (sb.Length > 0) sb.Append(" "); 
        sb.Append(controlText); 
       } 
      } 
     } 
     return sb.ToString(); 
    } 

    private string getTextProperty(object o) 
    { 
     PropertyInfo propertyInfo = o.GetType().GetProperty("Text"); 
     if (propertyInfo != null) 
     { 
      MethodInfo getMethod = propertyInfo.GetGetMethod(); 
      if (getMethod != null) 
      { 
       return (string)getMethod.Invoke(o, null); 
      } 
     } 
     return string.Empty; 
    } 

它可以導出PDF格式,並可以導出總的專欄中,我想,但除了圖像,其他細胞只顯示HTML代碼<table><tr><td>text</td></tr></table>

這是什麼,我該如何解決它?

+0

你使用'sw','hw','sr'和'htmlparser'或者是其他東西嗎? –

+0

你,克里斯哈斯。我正在使用htmlparser和stringbuilder – 456qwe123asd

回答

0

我沒有直接的答案,但我幾乎可以肯定地告訴您,問題將與您在設置PdfPCell的內容時所做的任何操作有關。 iTextSharp的核心沒有HTML的概念,幾乎所有的命令都是基於PDF的,並且有幾個.Net特定的命令。有解析器,它將HTML轉換爲基於PDF的命令,但您沒有使用它。因此,無論您如何獲取單元格的內容,都會返回iTextSharp將其視爲字符串文字的HTML。您需要讓算法只返回文本或使用iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList將HTML轉換爲iTextSharp對象。

+0

謝謝克里斯,但我應該把它放在哪裏? – 456qwe123asd