使用单个过程在c#中打印多个网格视图
本文关键字:打印 网格 视图 单个 过程 | 更新日期: 2023-09-27 17:59:23
这是我的工作代码。由于我不熟悉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文档
我该怎么做?
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)
{ }