使用单个过程在c#中打印多个网格视图

时间:2014-04-22 10:47:02

标签: c# asp.net sql pdf itextsharp

这是我的工作代码。因为我不熟悉itextsharp,所以我需要一些帮助来完成这个迭代。

当我点击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 个答案:

答案 0 :(得分: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)
    { }