Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

pdf generation - Crystal Reports "File Break"

I'm generating a Crystal Reports report which will ultimately need to be split into thousands of pdf files. What would be ideal would be if Crystal Reports had something like a "file break", like a page break, that you could insert into the file at the appropriate places.

I will need reasonably fine control over the file names, as well....something like "fileName_{CustomerId}_{CustomerIsLocal}.pdf".

I'm presuming a third-party piece of software will probably be needed. Thoughts?

TIA.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I wanted to document this for anyone else who shows up looking for the answer:

private readonly CrystalReportViewer reportViewer = new CrystalReportViewer();
...
this.reportViewer.ReportSource = @"C:PathToReportReport.rpt";

using (var crystalReport = new ReportDocument())
{
    foreach (DataRow row in dataSet.Tables[0].Rows)
    {
        var customerId = int.Parse(row["customerId"].ToString());
        var isCurrent = bool.Parse(row["isCurrent"].ToString());
        var totalSales = int.Parse(row["totalSales"].ToString());

        // generate the report for each row
        this.CreateReport(customerId, isCurrent, totalSales, crystalReport);
    }
}

private void CreateReport(int customerId, bool isCurrent, int totalSales, ReportDocument crystalReport)
{
    crystalReport.Load(this.reportViewer.ReportSource.ToString());

    crystalReport.SetParameterValue("customerId", customerId);
    crystalReport.SetParameterValue("isCurrent", isCurrent);
    crystalReport.SetParameterValue("TotalSales", totalSales);

    var fileName = string.Format("EndOfYear_{0}_{1}.pdf", customerId, isCurrent ? 1 : 0);

    var outputPath = Path.Combine(this.txtOutputDirectory.Text, fileName);

    crystalReport.ExportToDisk(ExportFormatType.PortableDocFormat, outputPath);
}

References:

CrystalDecisions.CrystalReports.Design CrystalDecisions.CrystalReports.Engine

This code yields a filename like so:

"EndOfYear_123456_1.pdf"

It is certainly possible to generate the report object for each row, rather than passing it in, but slows things down quite a bit. Reusing the same report object doesn't have any negative impact as far as I could see, and made things go about ten times faster.

The only other thing you need is how to prepare a Crystal report, which is beyond the scope of this tutorial. Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...