package jasper.test;
public class DataBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package jasper.test;
import java.util.ArrayList;
import java.util.List;
public class DataBeanList {
public static List getData(){
List dataBeans = new ArrayList();
for(int i = 0; i < 20; i++) {
DataBean db = new DataBean();
db.setName("jackson_"+i);
db.setAge(i+20);
dataBeans.add(db);
}
return dataBeans;
}
}
package jasper.test;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.ExporterFilter;
import net.sf.jasperreports.engine.export.JRExportProgressMonitor;
import net.sf.jasperreports.engine.export.JRHyperlinkProducerFactory;
import net.sf.jasperreports.export.ExporterInputItem;
import net.sf.jasperreports.export.Graphics2DExporterOutput;
import net.sf.jasperreports.export.Graphics2DReportConfiguration;
import net.sf.jasperreports.export.ReportExportConfiguration;
import net.sf.jasperreports.export.SimpleExporterInput;
public class ImageExportConfig implements ExporterInputItem {
private JasperPrint m_jasperPrint;
private Graphics2D m_graphics2D;
public ImageExportConfig(JasperPrint jasperPrint, Graphics2D graphics2D) {
m_jasperPrint = jasperPrint;
m_graphics2D = graphics2D;
}
@Override
public JasperPrint getJasperPrint() {
return m_jasperPrint;
}
@Override
public ReportExportConfiguration getConfiguration() {
return new Graphics2DReportConfiguration() {
@Override
public Integer getStartPageIndex() {
// TODO Auto-generated method stub
return null;
}
@Override
public Integer getEndPageIndex() {
// TODO Auto-generated method stub
return null;
}
@Override
public Integer getPageIndex() {
// TODO Auto-generated method stub
return null;
}
@Override
public JRExportProgressMonitor getProgressMonitor() {
// TODO Auto-generated method stub
return null;
}
@Override
public ExporterFilter getExporterFilter() {
// TODO Auto-generated method stub
return null;
}
@Override
public Integer getOffsetX() {
// TODO Auto-generated method stub
return null;
}
@Override
public Integer getOffsetY() {
// TODO Auto-generated method stub
return null;
}
@Override
public JRHyperlinkProducerFactory getHyperlinkProducerFactory() {
// TODO Auto-generated method stub
return null;
}
@Override
public Boolean isOverrideHints() {
// TODO Auto-generated method stub
return null;
}
@Override
public Float getZoomRatio() {
return Float.valueOf(4);
}
@Override
public Boolean isMinimizePrinterJobSize() {
// TODO Auto-generated method stub
return null;
}
@Override
public Boolean isIgnoreMissingFont() {
// TODO Auto-generated method stub
return null;
}
@Override
public Boolean isWhitePageBackground() {
// TODO Auto-generated method stub
return null;
}
};
}
/**
* build a SimpleExporterInput
* @return
*/
public SimpleExporterInput getSimpleExporterInput(){
List items = new ArrayList();
items.add(this);
return new SimpleExporterInput(items);
}
/**
* build a Graphics2DExporterOutput
* @return
*/
public Graphics2DExporterOutput getGraphics2DExporterOutput() {
return new Graphics2DExporterOutput() {
@Override
public Graphics2D getGraphics2D() {
return m_graphics2D;
}
};
}
}
package jasper.test;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRGraphics2DExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.oasis.JROdsExporter;
import net.sf.jasperreports.engine.export.oasis.JROdtExporter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRPptxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.util.AbstractSampleApp;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOdsReportConfiguration;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
public class DemoApp extends AbstractSampleApp {
public static void main(String[] args) {
try {
new DemoApp().test();
} catch (JRException e) {
e.printStackTrace();
}
System.out.println("=========done===========");
}
@Override
public void test() throws JRException {
fill();
pdf();
xmlEmbed();
xml();
html();
rtf();
xls();
csv();
odt();
ods();
docx();
xlsx();
pptx();
}
/**
*
*/
public void fill() throws JRException {
long start = System.currentTimeMillis();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(DataBeanList.getData());
JasperFillManager.fillReportToFile("D:/tool/jasperfile/report1.jasper", null, beanColDataSource);
System.err.println("Filling time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void print() throws JRException {
long start = System.currentTimeMillis();
JasperPrintManager.printReport("D:/tool/jasperfile/report1.jrprint", true);
System.err.println("Printing time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void pdf() throws JRException {
long start = System.currentTimeMillis();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput("D:/tool/jasperfile/report1.jrprint"));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("D:/tool/jasperfile/report1.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setCreatingBatchModeBookmarks(true);
exporter.setConfiguration(configuration);
exporter.exportReport();
// JasperExportManager.exportReportToPdfFile("D:/tool/jasperfile/report1.jrprint");
System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void xml() throws JRException {
long start = System.currentTimeMillis();
JasperExportManager.exportReportToXmlFile("D:/tool/jasperfile/report1.jrprint", false);
System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void xmlEmbed() throws JRException {
long start = System.currentTimeMillis();
JasperExportManager.exportReportToXmlFile("D:/tool/jasperfile/report1.jrprint", true);
System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void html() throws JRException {
long start = System.currentTimeMillis();
JasperExportManager.exportReportToHtmlFile("D:/tool/jasperfile/report1.jrprint");
System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void rtf() throws JRException {
long start = System.currentTimeMillis();
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".rtf");
JRRtfExporter exporter = new JRRtfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(destFile));
exporter.exportReport();
System.err.println("RTF creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void xls() throws JRException {
long start = System.currentTimeMillis();
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls");
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(false);
exporter.setConfiguration(configuration);
exporter.exportReport();
System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void csv() throws JRException {
long start = System.currentTimeMillis();
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".csv");
JRCsvExporter exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(destFile));
exporter.exportReport();
System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void odt() throws JRException {
long start = System.currentTimeMillis();
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".odt");
JROdtExporter exporter = new JROdtExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
exporter.exportReport();
System.err.println("ODT creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void ods() throws JRException {
long start = System.currentTimeMillis();
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".ods");
JROdsExporter exporter = new JROdsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
SimpleOdsReportConfiguration configuration = new SimpleOdsReportConfiguration();
configuration.setOnePagePerSheet(true);
exporter.setConfiguration(configuration);
exporter.exportReport();
System.err.println("ODS creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void docx() throws JRException {
long start = System.currentTimeMillis();
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".docx");
JRDocxExporter exporter = new JRDocxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
exporter.exportReport();
System.err.println("DOCX creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void xlsx() throws JRException {
long start = System.currentTimeMillis();
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xlsx");
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setOnePagePerSheet(false);
exporter.setConfiguration(configuration);
exporter.exportReport();
System.err.println("XLSX creation time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void pptx() throws JRException {
long start = System.currentTimeMillis();
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".pptx");
JRPptxExporter exporter = new JRPptxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
exporter.exportReport();
System.err.println("PPTX creation time : " + (System.currentTimeMillis() - start));
}
public void generateImage() throws JRException, IOException {
File sourceFile = new File("D:/tool/jasperfile/report1.jrprint");
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
// 创建graphics输出器
JRGraphics2DExporter exporter = new JRGraphics2DExporter();
// 创建一个影像对象
BufferedImage bufferedImage = new BufferedImage(jasperPrint.getPageWidth() * 4, jasperPrint.getPageHeight() * 4,
BufferedImage.TYPE_INT_RGB);
// 取graphics
Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
// 设置相应参数信息
ImageExportConfig imageExportConfig = new ImageExportConfig(jasperPrint, graphics2D);
exporter.setExporterInput(imageExportConfig.getSimpleExporterInput());
exporter.setExporterOutput(imageExportConfig.getGraphics2DExporterOutput());
exporter.exportReport();
// 释放资源信息
graphics2D.dispose();
// 生成图片
ImageIO.write(bufferedImage, "JPEG", new File("D:/tool/jasperfile/aa.png"));
}
}