2014-04-22 41 views
0

這是我的正在工作的代碼。由於我不熟悉itextsharp,因此我需要一些幫助來進行這種迭代。在c#中使用單個過程打印多個gridviews

當我點擊Print按鈕時,它會運行MySelectProcedure1並將表結果下載到名爲DataTable的PDF中。

protected void btnPrint_Click(object sender, EventArgs e) 
     { 
     Procedure("MySelectProcedure1");. 
     Procedure("MySelectProcedure2"); 
     } 
    protected void Procedure(string Proc) 
    { 
      Connection con = new Connection(); 
     SqlDataAdapter da; 
     DataTable dt; 
     con.con = new SqlConnection(con.str); 
     con.cmd.CommandText = Proc; 
     con.cmd.CommandType = CommandType.StoredProcedure; 
     da = new SqlDataAdapter(); 
     da.SelectCommand = con.cmd; 
     dt = new DataTable(); 
     try 
      { 
      con.con.Open(); 
      da.Fill(dt); 
      print(dt); 
      } 
     catch (Exception ex) 
      { 

      } 
     finally 
      { 
      con.con.Close(); 
      con.con.Dispose(); 

      } 

    } 

    protected void print (DataTable dt) 
     { 
    GridView GridView1 = new GridView(); 
    GridView1.AllowPaging = false; 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("content-disposition", "attachment;filename=DataTable.pdf"); 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(sw); 
    GridView1.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, Response.OutputStream); 
    pdfDoc.Open(); 
    htmlparser.Parse(sr); 
    pdfDoc.Close(); 
    Response.Write(pdfDoc); 
    Response.End(); 
     } 

    } 

但是,它不在同一PDF中打印MySelectProcedure1

這就是我想要的:

  • 打開PDF文檔。的MySelectProcedure1
  • 打印幾行MySelectProcedure2
  • 打印結果
  • 打印了幾行
  • 打印結果關閉並保存PDF文檔

我該怎麼辦呢?

回答

1

Yok可以創建出口PDF的公共功能。我會寫我如何使用它。

創建一個公共類,例如general.cs。並在其中添加此代碼。

public static void ExportPDF(DataTable dt1, DataTable dt2) 
{ 
    HttpContext.Current.Response.Clear(); 
    HttpContext.Current.Response.ContentType = "application/pdf"; 
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.pdf", "PDFExport")); 
    HttpContext.Current.Response.Charset = "utf-8"; 
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; 
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    //For First DataTable 
    System.IO.StringWriter stringWrite1 = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1); 
    DataGrid myDataGrid1 = new DataGrid(); 
    myDataGrid1.DataSource = dt1; 
    myDataGrid1.DataBind(); 
    myDataGrid1.RenderControl(htmlWrite1); 
    //For Second DataTable 
    System.IO.StringWriter stringWrite2 = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite2 = new HtmlTextWriter(stringWrite2); 
    DataGrid myDataGrid2 = new DataGrid(); 
    myDataGrid2.DataSource = dt2; 
    myDataGrid2.DataBind(); 
    myDataGrid2.RenderControl(htmlWrite2); 
    //You can add more DataTable 
    StringReader sr = new StringReader(stringWrite1.ToString() + stringWrite2.ToString()); 
    Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 10f, 0f); 
    pdfDoc.SetPageSize(PageSize.A4.Rotate()); 
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
    PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream); 
    pdfDoc.Open(); 
    htmlparser.Parse(sr); 
    pdfDoc.Close(); 
    HttpContext.Current.Response.Write(pdfDoc); 
    HttpContext.Current.Response.End(); 
} 

當你需要PDF只調用這個函數

protected void lnkExportPDF_Click(object sender, EventArgs e) 
    { 
     General.ExportPDF(rptList); 
    } 
public override void VerifyRenderingInServerForm(Control control) 
    { } 
+0

我希望有一個單一的PDF包含5個不同的表。我如何重複使用我的'print to pdf'功能? – divinediu

+0

我編輯了兩個數據表的代碼。你可以添加更多的表格並追加Stringreader – aydnahmet

+0

美麗。謝謝! – divinediu

相關問題