1 28 import java.awt.Image ; 29 import java.awt.MediaTracker ; 30 import java.awt.Panel ; 31 import java.awt.Toolkit ; 32 import java.io.File ; 33 import java.sql.Connection ; 34 import java.sql.DriverManager ; 35 import java.sql.SQLException ; 36 import java.util.HashMap ; 37 import java.util.Locale ; 38 import java.util.Map ; 39 40 import net.sf.jasperreports.engine.JRException; 41 import net.sf.jasperreports.engine.JRExporterParameter; 42 import net.sf.jasperreports.engine.JasperExportManager; 43 import net.sf.jasperreports.engine.JasperFillManager; 44 import net.sf.jasperreports.engine.JasperPrint; 45 import net.sf.jasperreports.engine.JasperPrintManager; 46 import net.sf.jasperreports.engine.JasperRunManager; 47 import net.sf.jasperreports.engine.export.JExcelApiExporter; 48 import net.sf.jasperreports.engine.export.JRCsvExporter; 49 import net.sf.jasperreports.engine.export.JRRtfExporter; 50 import net.sf.jasperreports.engine.export.JRXlsExporter; 51 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 52 import net.sf.jasperreports.engine.util.JRLoader; 53 54 55 59 public class JasperApp 60 { 61 62 63 66 private static final String TASK_FILL = "fill"; 67 private static final String TASK_PRINT = "print"; 68 private static final String TASK_PDF = "pdf"; 69 private static final String TASK_XML = "xml"; 70 private static final String TASK_XML_EMBED = "xmlEmbed"; 71 private static final String TASK_HTML = "html"; 72 private static final String TASK_RTF = "rtf"; 73 private static final String TASK_XLS = "xls"; 74 private static final String TASK_JXL = "jxl"; 75 private static final String TASK_CSV = "csv"; 76 private static final String TASK_RUN = "run"; 77 78 79 82 public static void main(String [] args) 83 { 84 String fileName = null; 85 String taskName = null; 86 87 if(args.length == 0) 88 { 89 usage(); 90 return; 91 } 92 93 int k = 0; 94 while ( args.length > k ) 95 { 96 if ( args[k].startsWith("-T") ) 97 taskName = args[k].substring(2); 98 if ( args[k].startsWith("-F") ) 99 fileName = args[k].substring(2); 100 101 k++; 102 } 103 104 try 105 { 106 long start = System.currentTimeMillis(); 107 if (TASK_FILL.equals(taskName)) 108 { 109 Image image = Toolkit.getDefaultToolkit().createImage("dukesign.jpg"); 111 MediaTracker traker = new MediaTracker (new Panel ()); 112 traker.addImage(image, 0); 113 try 114 { 115 traker.waitForID(0); 116 } 117 catch (Exception e) 118 { 119 e.printStackTrace(); 120 } 121 122 Map parameters = new HashMap (); 123 parameters.put("ReportTitle", "The First Jasper Report Ever"); 124 parameters.put("MaxOrderID", new Integer (10500)); 125 parameters.put("SummaryImage", image); 126 127 JasperFillManager.fillReportToFile(fileName, parameters, getConnection()); 128 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 129 System.exit(0); 130 } 131 else if (TASK_PRINT.equals(taskName)) 132 { 133 JasperPrintManager.printReport(fileName, true); 134 System.err.println("Printing time : " + (System.currentTimeMillis() - start)); 135 System.exit(0); 136 } 137 else if (TASK_PDF.equals(taskName)) 138 { 139 JasperExportManager.exportReportToPdfFile(fileName); 140 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 141 System.exit(0); 142 } 143 else if (TASK_XML.equals(taskName)) 144 { 145 JasperExportManager.exportReportToXmlFile(fileName, false); 146 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 147 System.exit(0); 148 } 149 else if (TASK_XML_EMBED.equals(taskName)) 150 { 151 JasperExportManager.exportReportToXmlFile(fileName, true); 152 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 153 System.exit(0); 154 } 155 else if (TASK_HTML.equals(taskName)) 156 { 157 JasperExportManager.exportReportToHtmlFile(fileName); 158 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); 159 System.exit(0); 160 } 161 else if (TASK_RTF.equals(taskName)) 162 { 163 File sourceFile = new File (fileName); 164 165 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 166 167 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".rtf"); 168 169 JRRtfExporter exporter = new JRRtfExporter(); 170 171 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 172 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 173 174 exporter.exportReport(); 175 176 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); 177 System.exit(0); 178 } 179 else if (TASK_XLS.equals(taskName)) 180 { 181 File sourceFile = new File (fileName); 182 Map dateFormats = new HashMap (); 183 dateFormats.put("EEE, MMM d, yyyy", "ddd, mmm d, yyyy"); 184 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 185 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".xls"); 186 187 JRXlsExporter exporter = new JRXlsExporter(); 188 189 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 190 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 191 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 192 exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); 193 exporter.setParameter(JRXlsExporterParameter.FORMAT_PATTERNS_MAP, dateFormats); 194 195 exporter.exportReport(); 196 197 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 198 System.exit(0); 199 } 200 else if (TASK_JXL.equals(taskName)) 201 { 202 File sourceFile = new File (fileName); 203 204 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 205 206 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); 207 208 JExcelApiExporter exporter = new JExcelApiExporter(); 209 210 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 211 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 212 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 213 214 exporter.exportReport(); 215 216 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 217 System.exit(0); 218 } 219 else if (TASK_CSV.equals(taskName)) 220 { 221 File sourceFile = new File (fileName); 222 223 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 224 225 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".csv"); 226 227 JRCsvExporter exporter = new JRCsvExporter(); 228 229 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 230 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 231 232 exporter.exportReport(); 233 234 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); 235 System.exit(0); 236 } 237 else if (TASK_RUN.equals(taskName)) 238 { 239 Image image = Toolkit.getDefaultToolkit().createImage("dukesign.jpg"); 241 MediaTracker traker = new MediaTracker (new Panel ()); 242 traker.addImage(image, 0); 243 try 244 { 245 traker.waitForID(0); 246 } 247 catch (Exception e) 248 { 249 e.printStackTrace(); 250 } 251 252 Map parameters = new HashMap (); 253 parameters.put("ReportTitle", "The First Jasper Report Ever"); 254 parameters.put("MaxOrderID", new Integer (10500)); 255 parameters.put("SummaryImage", image); 256 257 JasperRunManager.runReportToPdfFile(fileName, parameters, getConnection()); 258 System.err.println("PDF running time : " + (System.currentTimeMillis() - start)); 259 System.exit(0); 260 } 261 else 262 { 263 usage(); 264 System.exit(0); 265 } 266 } 267 catch (JRException e) 268 { 269 e.printStackTrace(); 270 System.exit(1); 271 } 272 catch (Exception e) 273 { 274 e.printStackTrace(); 275 System.exit(1); 276 } 277 } 278 279 280 283 private static void usage() 284 { 285 System.out.println( "JasperApp usage:" ); 286 System.out.println( "\tjava JasperApp -Ttask -Ffile" ); 287 System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" ); 288 } 289 290 291 294 private static Connection getConnection() throws ClassNotFoundException , SQLException 295 { 296 String driver = "org.hsqldb.jdbcDriver"; 298 String connectString = "jdbc:hsqldb:hsql://localhost"; 299 String user = "sa"; 300 String password = ""; 301 302 303 Class.forName(driver); 304 Connection conn = DriverManager.getConnection(connectString, user, password); 305 return conn; 306 } 307 308 309 } 310 | Popular Tags |