1 28 import java.io.File ; 29 30 import net.sf.jasperreports.engine.JREmptyDataSource; 31 import net.sf.jasperreports.engine.JRException; 32 import net.sf.jasperreports.engine.JRExporterParameter; 33 import net.sf.jasperreports.engine.JasperExportManager; 34 import net.sf.jasperreports.engine.JasperFillManager; 35 import net.sf.jasperreports.engine.JasperPrint; 36 import net.sf.jasperreports.engine.JasperPrintManager; 37 import net.sf.jasperreports.engine.JasperRunManager; 38 import net.sf.jasperreports.engine.export.JExcelApiExporter; 39 import net.sf.jasperreports.engine.export.JRCsvExporter; 40 import net.sf.jasperreports.engine.export.JRRtfExporter; 41 import net.sf.jasperreports.engine.export.JRXlsExporter; 42 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 43 import net.sf.jasperreports.engine.util.JRLoader; 44 45 46 50 public class ImagesApp 51 { 52 53 54 57 private static final String TASK_FILL = "fill"; 58 private static final String TASK_PRINT = "print"; 59 private static final String TASK_PDF = "pdf"; 60 private static final String TASK_XML = "xml"; 61 private static final String TASK_XML_EMBED = "xmlEmbed"; 62 private static final String TASK_HTML = "html"; 63 private static final String TASK_RTF = "rtf"; 64 private static final String TASK_XLS = "xls"; 65 private static final String TASK_JXL = "jxl"; 66 private static final String TASK_CSV = "csv"; 67 private static final String TASK_RUN = "run"; 68 69 70 73 public static void main(String [] args) 74 { 75 String fileName = null; 76 String taskName = null; 77 78 if(args.length == 0) 79 { 80 usage(); 81 return; 82 } 83 84 int k = 0; 85 while ( args.length > k ) 86 { 87 if ( args[k].startsWith("-T") ) 88 taskName = args[k].substring(2); 89 if ( args[k].startsWith("-F") ) 90 fileName = args[k].substring(2); 91 92 k++; 93 } 94 95 try 96 { 97 long start = System.currentTimeMillis(); 98 if (TASK_FILL.equals(taskName)) 99 { 100 JasperFillManager.fillReportToFile(fileName, null, new JREmptyDataSource()); 101 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 102 System.exit(0); 103 } 104 else if (TASK_PRINT.equals(taskName)) 105 { 106 JasperPrintManager.printReport(fileName, true); 107 System.err.println("Printing time : " + (System.currentTimeMillis() - start)); 108 System.exit(0); 109 } 110 else if (TASK_PDF.equals(taskName)) 111 { 112 JasperExportManager.exportReportToPdfFile(fileName); 113 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 114 System.exit(0); 115 } 116 else if (TASK_XML.equals(taskName)) 117 { 118 JasperExportManager.exportReportToXmlFile(fileName, false); 119 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 120 System.exit(0); 121 } 122 else if (TASK_XML_EMBED.equals(taskName)) 123 { 124 JasperExportManager.exportReportToXmlFile(fileName, true); 125 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 126 System.exit(0); 127 } 128 else if (TASK_HTML.equals(taskName)) 129 { 130 JasperExportManager.exportReportToHtmlFile(fileName); 131 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); 132 System.exit(0); 133 } 134 else if (TASK_RTF.equals(taskName)) 135 { 136 File sourceFile = new File (fileName); 137 138 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 139 140 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".rtf"); 141 142 JRRtfExporter exporter = new JRRtfExporter(); 143 144 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 145 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 146 147 exporter.exportReport(); 148 149 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); 150 System.exit(0); 151 } 152 else if (TASK_XLS.equals(taskName)) 153 { 154 File sourceFile = new File (fileName); 155 156 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 157 158 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".xls"); 159 160 JRXlsExporter exporter = new JRXlsExporter(); 161 162 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 163 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 164 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 165 166 exporter.exportReport(); 167 168 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 169 System.exit(0); 170 } 171 else if (TASK_JXL.equals(taskName)) 172 { 173 File sourceFile = new File (fileName); 174 175 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 176 177 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); 178 179 JExcelApiExporter exporter = new JExcelApiExporter(); 180 181 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 182 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 183 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 184 185 exporter.exportReport(); 186 187 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 188 System.exit(0); 189 } 190 else if (TASK_CSV.equals(taskName)) 191 { 192 File sourceFile = new File (fileName); 193 194 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 195 196 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".csv"); 197 198 JRCsvExporter exporter = new JRCsvExporter(); 199 200 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 201 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 202 203 exporter.exportReport(); 204 205 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); 206 System.exit(0); 207 } 208 else if (TASK_RUN.equals(taskName)) 209 { 210 JasperRunManager.runReportToPdfFile(fileName, null, new JREmptyDataSource()); 211 System.err.println("PDF running time : " + (System.currentTimeMillis() - start)); 212 System.exit(0); 213 } 214 else 215 { 216 usage(); 217 System.exit(0); 218 } 219 } 220 catch (JRException e) 221 { 222 e.printStackTrace(); 223 System.exit(1); 224 } 225 catch (Exception e) 226 { 227 e.printStackTrace(); 228 System.exit(1); 229 } 230 } 231 232 233 236 private static void usage() 237 { 238 System.out.println( "ImagesApp usage:" ); 239 System.out.println( "\tjava ImagesApp -Ttask -Ffile" ); 240 System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" ); 241 } 242 243 244 } 245 | Popular Tags |