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