📄 jasperprintutil.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package personalpetvet.ui;import java.sql.DriverManager;import java.sql.SQLException;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Locale;import java.util.Map;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JasperCompileManager;import net.sf.jasperreports.engine.JasperExportManager;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.JasperReport;import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;import net.sf.jasperreports.view.JasperViewer;import personalpetvet.CustomBean;import personalpetvet.CustomDataSource;import personalpetvet.DBHelper;import personalpetvet.JatoReportDataSource;import personalpetvet.model.Expense;import personalpetvet.model.Pet;import personalpetvet.model.PetControl;import personalpetvet.model.Petsowner;/** * * @author James */public class JasperPrintUtil { String reportSource = "report/templates/"; String reportDest = "report/results/"; Map<String, Object> params; //= new HashMap<String, Object>(); public JasperPrintUtil(String reportSource, String reportDest, Map<String, Object> params) { this.reportSource = this.reportSource + reportSource; this.reportDest = this.reportDest + reportDest; this.params = params; } private static void printPet() { final String reportTemplate = "PetReport.jrxml"; final String reportExport = "PetReport.html"; int minPetId = Integer.MIN_VALUE; int maxPetId = Integer.MAX_VALUE; HashMap<String, Object> params = new HashMap<String, Object>(2); params.put("MIN_ID", minPetId); params.put("MAX_ID", maxPetId); new JasperPrintUtil(reportTemplate, reportExport, params).print(); } public static void main(String[] args) { printPet();// JasperPrintUtil jpu = new JasperPrintUtil();// jpu.params.put("MIN_ID", 1);// jpu.params.put("MAX_ID", 11111);// Date date=new Date(1,1,1);// jpu.params.put("BEGIN_DATE",date );// jpu.print();// List<Petsowner> pov = new ArrayList<Petsowner>();// pov.add(new Petsowner("AAA", "AAA", "AAA", "AAA", "AAA", "AAA", "AAA"));// //new JasperPrintUtil().print(new JatoReportDataSource(pov),"CustomerDataSource");// new JasperPrintUtil().print(new CustomDataSource(), "CustomerDataSource"); } public void print() { try { JasperReport jasperReport = JasperCompileManager.compileReport(reportSource); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, params, DBHelper.getConnection()); JasperExportManager.exportReportToHtmlFile( jasperPrint, reportDest); JasperViewer.viewReport(jasperPrint, false); } catch (JRException e) { e.printStackTrace(); } } //for javabeans datasource. not used. public void print(JRDataSource data, String dataClassName) throws Throwable { throw new Throwable("Not implemented!");// // try {// //params.put("DataFile", "ViewMedications.java - Bean Collection");// params.put("DataFile", dataClassName);// JasperReport jasperReport =// JasperCompileManager.compileReport(reportSource);// // JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JRBeanCollectionDataSource(data));//// JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new CustomDataSource());// //JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, data);// JasperFillManager.fillReportToFile(jasperReport, reportDest, params, data);// // JasperExportManager.exportReportToHtmlFile( jasperPrint, reportDest);//// // JasperViewer.viewReport(jasperPrint);// JasperViewer.viewReport(reportDest, false);// } catch (JRException e) {// e.printStackTrace();// } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -