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