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