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