如何一次导出PDF格式的所有文件 [英] How to export all the files in PDF format at a time

查看:70
本文介绍了如何一次导出PDF格式的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何一次通过ASP.NET导出PDF格式的多个文件。 Crystal报表中的文件格式已更新。我的代码附在这里。这仅用于一次保存一个PDF文件。

How to export multiple files in PDF format through ASP.NET at a time. File format is already updated in Crystal report. My code attached here. this is use to only one PDFfile save at a time.

public void updatestaus(string F)
    {
        sc.Close();
        sc.Open();
        SqlCommand cmd = new SqlCommand("update BOND_REG set status='P' where fliono='"+F+"'", sc);
        cmd.ExecuteNonQuery();
        sc.Close();


    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        sc.Open();
        SqlCommand cmd = new SqlCommand("select Fliono from BOND_REG where status='A'", sc);
        SqlDataReader SDR = cmd.ExecuteReader();
        while (SDR.Read() == true)
        {
            string St = SDR.GetValue(0).ToString();
            //SDR.Close();
          // SDR.Dispose();
            fileexport(St);
            updatestaus(St);

        }
        SDR.Close();
        sc.Close();

    }

    public void fileexport(string st1)
    {
       // SDR.Close(); 
        SqlCommand cmd1 = new SqlCommand("select * from BOND_REG where status='A' and Fliono='" + st1 + "'", sc);
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd1;
        DataTable datatable = new DataTable();
        da.Fill(datatable);
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("~/bond.rpt"));
        crystalReport.SetDataSource(datatable);
        ExportOptions rptExportOption;
        DiskFileDestinationOptions rptFileDestOption = new DiskFileDestinationOptions();
        PdfRtfWordFormatOptions rptFormatOption = new PdfRtfWordFormatOptions();
        string reportFileName = @"C:\pdfd\ st1.pdf";
        rptFileDestOption.DiskFileName = reportFileName;
        rptExportOption = crystalReport.ExportOptions;
        {
            rptExportOption.ExportDestinationType = ExportDestinationType.DiskFile;
                       rptExportOption.ExportFormatType = ExportFormatType.PortableDocFormat;
            rptExportOption.ExportDestinationOptions = rptFileDestOption;
            rptExportOption.ExportFormatOptions = rptFormatOption;
        }

        crystalReport.Export();
    }

推荐答案

我认为问题出在您的应用程序覆盖相同pdf文件的每条记录中。尝试构建如下所示的唯一文件名



I think the problem is in each record your application overwrite the same pdf file. try to build unique file name like below

string reportFileName =string.Format(@"C:\pdfd\st1_{0}.pdf",st1) ;





将时间跨度附加到文件名。 [ ^ ]



通过引入方法来执行数据库操作并在这些方法中正确处理数据库连接,可以避免与连接相关的问题。例如,





or append timespan to file name.[^]

You can avoid connection related issues by introducing methods to do the database operations and properly handling database connections in those methods. for example,

public List<string> GetFlionoList()
{
    List<string>  flionoList = new List<string>();
    using(SqlConnection connection = new SqlConnection(yourConnectionString) )
	using(SqlCommand cmd = new SqlCommand("select Fliono from BOND_REG where status='A'", connection))
	{
		connection.Open();
		using(SqlDataReader sdr= cmd.ExecuteReader())
		{
			while (sdr.Read())
			{
			  flionoList.Add(sdr.Getstring(0));
			}
		}
	}
	return flionoList;
}

public void UpdateStaus(string fliono)
{
   using(SqlConnection connection = new SqlConnection(yourConnectionString) )
   using(SqlCommand cmd = new SqlCommand("update BOND_REG set status='P' where fliono='"+fliono+"'", connection))
   {
   		connection.Open();
   		cmd.ExecuteNonQuery();
   }
}





现在你可以打电话给 GetFlionoList()并获取列表并迭代列表中的每个项目,并为每个项目调用 UpdateStaus 等方法。确保您使用使用块处理连接和读者



Now you can call GetFlionoList() and get the list and iterate each item of the list and call methods like UpdateStaus for each item. make sure that you have dispose the connection and readers using "using blocks"


这篇关于如何一次导出PDF格式的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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