如何用pdf打印c#中的所有记录 [英] how to print in pdf all records in c#

查看:88
本文介绍了如何用pdf打印c#中的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在C#中有A​​SP.NET网站。



我在哪里用pdf格式下载CrystalReport的代码。



Hi guys,

I have ASP.NET website in C#.

Where I have code to download CrystalReport in pdf.

public ReportDocument SIM_HandoverReport(int uid)
       {
           ReportDocument rd = new ReportDocument();

           rd.Load(Page.Server.MapPath("SIM_Handover.rpt"));
           rd.SetParameterValue("@UserID", uid);
           rd.SetDatabaseLogon("DB_9B08BD_ITAccessories_admin", "sa@123456");

           return rd;
       }

       protected void Print2PDF(ReportDocument cryRpt)
       {
           try
           {

               ExportOptions CrExportOptions;

               DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();

               PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();

               string strDate = DateTime.Now.ToShortDateString();

               string Path = "~/ExportedReports/ITReport_NPS.pdf";

               if (Path != null || Path != string.Empty)
               {
                   if (System.IO.File.Exists(Path))
                   {
                       System.IO.File.Delete(Path);
                   }
               }

               CrDiskFileDestinationOptions.DiskFileName = Server.MapPath(Path);

               CrExportOptions = cryRpt.ExportOptions;

               {

                   CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;

                   CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;

                   CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;

                   CrExportOptions.FormatOptions = CrFormatTypeOptions;

               }

               cryRpt.Export();
               Response.Redirect("Save.ashx");
           }
           catch (Exception ex)
           {
               //throw;
           }
       }





以上代码工作正常。



但我想用pdf方法为所有用户下载报告。



i到目前为止尝试了



the above code is working fine.

But i want to call the pdf method to download the reports for all users.

i tried so far

protected void btnAllSim_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt = dl.GetData("Sp_GetAllAssignedSIMUsers");
    foreach (DataRow dr in dt.Rows)
    {
        int uids = Convert.ToInt32(dr["userid"].ToString());
        ReportDocument rd = new ReportDocument();
        rd = SIM_HandoverReport(uids);
        Print2PDF(rd);//But its downloading only for the 1st user id only.
    }

}







按钮点击事件仅下载第一个用户ID,





任何人都可以帮助我。如何在foreach循环中为所有代码运行。





谢谢




the button click event is downloading only for the first user id,


Can anyone please help me. How the code will run for all in the foreach loop.


Thanks

推荐答案

看起来您正在实例化ReportDocument两次,一次是在按钮单击中,一次是在SIM_handoverReport方法中。



It appears that you are instantiating the ReportDocument twice, once in the button click and once in the SIM_handoverReport method.

protected void btnAllSim_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt = dl.GetData("Sp_GetAllAssignedSIMUsers");
    foreach (DataRow dr in dt.Rows)
    {
        int uids = Convert.ToInt32(dr["userid"].ToString());
        ReportDocument rd = new ReportDocument();
        rd = SIM_HandoverReport(uids);
        Print2PDF(rd);//But its downloading only for the 1st user id only.
    }
 
}







public ReportDocument SIM_HandoverReport(int uid)
       {
           ReportDocument rd = new ReportDocument();
 
           rd.Load(Page.Server.MapPath("SIM_Handover.rpt"));







我建议你将实例化保留在SIM_HandoverReport方法中并按如下方式更改按钮代码:






I suggest you leave the instantiation inside the SIM_HandoverReport method and change the button code as follows:

protected void btnAllSim_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt = dl.GetData("Sp_GetAllAssignedSIMUsers");
    foreach (DataRow dr in dt.Rows)
    {
        int uids = Convert.ToInt32(dr["userid"].ToString());
        ReportDocument rd = new ReportDocument();
        rd = SIM_HandoverReport(uids);
        ReportDocument rd = SIM_HandoverReport(uids);
        Print2PDF(rd);//But its downloading only for the 1st user id only.
    }
 
}


这篇关于如何用pdf打印c#中的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆