Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - JasperReport setParameter() Deprecated?

I have recently upgraded the Jasper Reports library for my project, from 3.7.6 to 6.0.0. I can finally Maven build and the reports are working just great. However, the setParameter() function appears to have been deprecated between releases, and I am unsure how to refactor my code to accommodate for this.

Example of Deprecated Code:

private static void exportMultipleToCSV(Collection<JasperPrint> jasperPrints, OutputStream baos) throws JRException {
    JRCsvExporter csvExporter = new JRCsvExporter();

    csvExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
    csvExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
    csvExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, Integer.valueOf(1500000));
    csvExporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, Integer.valueOf(40000000));
    csvExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, Integer.valueOf(4));
    csvExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, Integer.valueOf(15));

    csvExporter.exportReport();
}

I have looked through the SourceForge page, and can see that it has been replaced by ExporterInput, ExporterConfiguration and ExporterOutput but I am unsure how to utilise them all together to achieve the desired output.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

The equivalent code would look something like this:

JRCsvExporter csvExporter = new JRCsvExporter();
//jasperPrints is Collection, but we need a List
csvExporter.setExporterInput(SimpleExporterInput.getInstance(new ArrayList(jasperPrints)));
csvExporter.setExporterOutput(new SimpleWriterExporterOutput(baos));
SimpleCsvExporterConfiguration exporterConfiguration = new SimpleCsvExporterConfiguration();
//nothing to set here, but you could do things like exporterConfiguration.setFieldDelimiter
csvExporter.setConfiguration(exporterConfiguration);
csvExporter.exportReport();

Note that the old API allowed you do things like csvExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT). The problem with that was that the CSV exporter did not actually use that parameter, only the text exporter was looking at JRTextExporterParameter.PAGE_HEIGHT. With the new API, it's clear what settings/configuration you can do on each exporter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...