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 LandscapeApp 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, false); 108 System.err.println("Printing time : " + (System.currentTimeMillis() - start)); 109 System.exit(0); 110 } 111 else if (TASK_PDF.equals(taskName)) 112 { 113 JasperExportManager.exportReportToPdfFile(fileName); 114 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 115 System.exit(0); 116 } 117 else if (TASK_RTF.equals(taskName)) 118 { 119 File sourceFile = new File (fileName); 120 121 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 122 123 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".rtf"); 124 125 JRRtfExporter exporter = new JRRtfExporter(); 126 127 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 128 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 129 130 exporter.exportReport(); 131 132 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); 133 System.exit(0); 134 } 135 else if (TASK_XML.equals(taskName)) 136 { 137 JasperExportManager.exportReportToXmlFile(fileName, false); 138 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 139 System.exit(0); 140 } 141 else if (TASK_XML_EMBED.equals(taskName)) 142 { 143 JasperExportManager.exportReportToXmlFile(fileName, true); 144 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 145 System.exit(0); 146 } 147 else if (TASK_HTML.equals(taskName)) 148 { 149 JasperExportManager.exportReportToHtmlFile(fileName); 150 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); 151 System.exit(0); 152 } 153 else if (TASK_XLS.equals(taskName)) 154 { 155 File sourceFile = new File (fileName); 156 157 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 158 159 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".xls"); 160 161 JRXlsExporter exporter = new JRXlsExporter(); 162 163 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 164 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 165 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 166 167 exporter.exportReport(); 168 169 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 170 System.exit(0); 171 } 172 else if (TASK_JXL.equals(taskName)) 173 { 174 File sourceFile = new File (fileName); 175 176 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 177 178 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); 179 180 JExcelApiExporter exporter = new JExcelApiExporter(); 181 182 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 183 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 184 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 185 186 exporter.exportReport(); 187 188 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 189 System.exit(0); 190 } 191 else if (TASK_CSV.equals(taskName)) 192 { 193 File sourceFile = new File (fileName); 194 195 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 196 197 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".csv"); 198 199 JRCsvExporter exporter = new JRCsvExporter(); 200 201 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 202 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 203 204 exporter.exportReport(); 205 206 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); 207 System.exit(0); 208 } 209 else if (TASK_RUN.equals(taskName)) 210 { 211 JasperRunManager.runReportToPdfFile(fileName, null, new JREmptyDataSource()); 212 System.err.println("PDF running time : " + (System.currentTimeMillis() - start)); 213 System.exit(0); 214 } 215 else 216 { 217 usage(); 218 System.exit(0); 219 } 220 } 221 catch (JRException e) 222 { 223 e.printStackTrace(); 224 System.exit(1); 225 } 226 catch (Exception e) 227 { 228 e.printStackTrace(); 229 System.exit(1); 230 } 231 } 232 233 234 237 private static void usage() 238 { 239 System.out.println( "LandscapeApp usage:" ); 240 System.out.println( "\tjava LandscapeApp -Ttask -Ffile" ); 241 System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" ); 242 } 243 244 245 } 246 | Popular Tags |