1 19 20 package jxl.demo; 21 22 import java.io.File ; 23 import java.io.OutputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.BufferedWriter ; 27 import java.text.DecimalFormat ; 28 29 import common.Logger; 30 31 import jxl.Workbook; 32 import jxl.Range; 33 import jxl.Cell; 34 35 40 public class Demo 41 { 42 private static final int CSVFormat = 13; 43 private static final int XMLFormat = 14; 44 45 48 private static Logger logger = Logger.getLogger(Demo.class); 49 50 53 private static void displayHelp() 54 { 55 System.err.println 56 ("Command format: Demo [-unicode] [-csv] [-hide] excelfile"); 57 System.err.println(" Demo -xml [-format] excelfile"); 58 System.err.println(" Demo -readwrite|-rw excelfile output"); 59 System.err.println(" Demo -biffdump | -bd | -wa | -write | -formulas | -features excelfile"); 60 System.err.println(" Demo -ps excelfile [property] [output]"); 61 System.err.println(" Demo -version | -logtest | -h | -help"); 62 63 } 64 65 72 public static void main(String args[]) 73 { 74 if (args.length == 0) 75 { 76 displayHelp(); 77 System.exit(1); 78 } 79 80 if (args[0].equals("-help") || args[0].equals("-h")) 81 { 82 displayHelp(); 83 System.exit(1); 84 } 85 86 if (args[0].equals("-version")) 87 { 88 System.out.println("v"+Workbook.getVersion()); 89 System.exit(0); 90 } 91 92 if (args[0].equals("-logtest")) 93 { 94 logger.debug("A sample \"debug\" message"); 95 logger.info("A sample \"info\" message"); 96 logger.warn("A sample \"warning\" message"); 97 logger.error("A sample \"error\" message"); 98 logger.fatal("A sample \"fatal\" message"); 99 System.exit(0); 100 } 101 102 boolean write = false; 103 boolean readwrite = false; 104 boolean formulas = false; 105 boolean biffdump = false; 106 boolean jxlversion = false; 107 boolean propertysets = false; 108 boolean features = false; 109 String file = args[0]; 110 String outputFile = null; 111 String propertySet = null; 112 113 if (args[0].equals("-write")) 114 { 115 write = true; 116 file = args[1]; 117 } 118 else if (args[0].equals("-formulas")) 119 { 120 formulas = true; 121 file = args[1]; 122 } 123 else if (args[0].equals("-features")) 124 { 125 features = true; 126 file = args[1]; 127 } 128 else if (args[0].equals("-biffdump") || args[0].equals("-bd")) 129 { 130 biffdump = true; 131 file = args[1]; 132 } 133 else if (args[0].equals("-wa")) 134 { 135 jxlversion = true; 136 file = args[1]; 137 } 138 else if (args[0].equals("-ps")) 139 { 140 propertysets = true; 141 file = args[1]; 142 143 if (args.length > 2) 144 { 145 propertySet = args[2]; 146 } 147 148 if (args.length == 4) 149 { 150 outputFile = args[3]; 151 } 152 } 153 else if (args[0].equals("-readwrite") || args[0].equals("-rw")) 154 { 155 readwrite = true; 156 file = args[1]; 157 outputFile = args[2]; 158 } 159 else 160 { 161 file = args[args.length - 1]; 162 } 163 164 String encoding = "UTF8"; 165 int format = CSVFormat; 166 boolean formatInfo = false; 167 boolean hideCells = false; 168 169 if (write == false && 170 readwrite == false && 171 formulas == false && 172 biffdump == false && 173 jxlversion == false && 174 propertysets == false && 175 features == false) 176 { 177 for (int i = 0; i < args.length - 1; i++) 178 { 179 if (args[i].equals("-unicode")) 180 { 181 encoding="UnicodeBig"; 182 } 183 else if (args[i].equals("-xml")) 184 { 185 format = XMLFormat; 186 } 187 else if (args[i].equals("-csv")) 188 { 189 format = CSVFormat; 190 } 191 else if (args[i].equals("-format")) 192 { 193 formatInfo = true; 194 } 195 else if (args[i].equals("-hide")) 196 { 197 hideCells = true; 198 } 199 else 200 { 201 System.err.println 202 ("Command format: CSV [-unicode] [-xml|-csv] excelfile"); 203 System.exit(1); 204 } 205 } 206 } 207 208 try 209 { 210 if (write) 211 { 212 Write w = new Write(file); 213 w.write(); 214 } 215 else if (readwrite) 216 { 217 ReadWrite rw = new ReadWrite(file, outputFile); 218 rw.readWrite(); 219 } 220 else if (formulas) 221 { 222 Workbook w = Workbook.getWorkbook(new File (file)); 223 Formulas f = new Formulas(w, System.out, encoding); 224 w.close(); 225 } 226 else if (features) 227 { 228 Workbook w = Workbook.getWorkbook(new File (file)); 229 Features f = new Features(w, System.out, encoding); 230 w.close(); 231 } 232 else if (biffdump) 233 { 234 BiffDump bd = new BiffDump(new File (file), System.out); 235 } 236 else if (jxlversion) 237 { 238 WriteAccess bd = new WriteAccess(new File (file)); 239 } 240 else if (propertysets) 241 { 242 OutputStream os = System.out; 243 if (outputFile != null) 244 { 245 os = new FileOutputStream (outputFile); 246 } 247 PropertySetsReader psr = new PropertySetsReader(new File (file), 248 propertySet, 249 os); 250 } 251 else 252 { 253 Workbook w = Workbook.getWorkbook(new File (file)); 254 255 257 if (format == CSVFormat) 258 { 259 CSV csv = new CSV(w, System.out, encoding, hideCells); 260 } 261 else if (format == XMLFormat) 262 { 263 XML xml = new XML(w, System.out, encoding, formatInfo); 264 } 265 266 w.close(); 267 } 268 } 269 catch (Throwable t) 270 { 271 System.out.println(t.toString()); 272 t.printStackTrace(); 273 } 274 } 275 276 279 private static void findTest(Workbook w) 280 { 281 logger.info("Find test"); 282 283 Cell c = w.findCellByName("named1"); 284 if (c != null) 285 { 286 logger.info("named1 contents: " + c.getContents()); 287 } 288 289 c = w.findCellByName("named2"); 290 if (c != null) 291 { 292 logger.info("named2 contents: " + c.getContents()); 293 } 294 295 c = w.findCellByName("namedrange"); 296 if (c != null) 297 { 298 logger.info("named2 contents: " + c.getContents()); 299 } 300 301 Range[] range = w.findByName("namedrange"); 302 if (range != null) 303 { 304 c = range[0].getTopLeft(); 305 logger.info("namedrange top left contents: " + c.getContents()); 306 307 c = range[0].getBottomRight(); 308 logger.info("namedrange bottom right contents: " + c.getContents()); 309 } 310 311 range = w.findByName("nonadjacentrange"); 312 if (range != null) 313 { 314 for (int i = 0; i < range.length; i++) 315 { 316 c = range[i].getTopLeft(); 317 logger.info("nonadjacent top left contents: " + c.getContents()); 318 319 c = range[i].getBottomRight(); 320 logger.info("nonadjacent bottom right contents: " + c.getContents()); 321 } 322 } 323 324 range = w.findByName("horizontalnonadjacentrange"); 325 if (range != null) 326 { 327 for (int i = 0; i < range.length; i++) 328 { 329 c = range[i].getTopLeft(); 330 logger.info("horizontalnonadjacent top left contents: " + 331 c.getContents()); 332 333 c = range[i].getBottomRight(); 334 logger.info("horizontalnonadjacent bottom right contents: " + 335 c.getContents()); 336 } 337 } 338 339 } 340 } 341 342 343 344 | Popular Tags |