1 19 20 package jxl.demo; 21 22 import java.io.File ; 23 import java.io.OutputStream ; 24 import java.io.OutputStreamWriter ; 25 import java.io.BufferedWriter ; 26 import java.io.UnsupportedEncodingException ; 27 import java.io.IOException ; 28 import java.util.Date ; 29 import java.util.Calendar ; 30 import java.util.TimeZone ; 31 import java.net.URL ; 32 import java.net.MalformedURLException ; 33 34 import common.Logger; 35 36 import jxl.Workbook; 37 import jxl.CellType; 38 import jxl.Cell; 39 import jxl.Sheet; 40 import jxl.FormulaCell; 41 import jxl.Range; 42 import jxl.biff.formula.FormulaException; 43 import jxl.format.CellFormat; 44 import jxl.format.UnderlineStyle; 45 import jxl.format.Colour; 46 import jxl.read.biff.BiffException; 47 import jxl.write.WritableWorkbook; 48 import jxl.write.WritableSheet; 49 import jxl.write.WritableCell; 50 import jxl.write.WritableCellFormat; 51 import jxl.write.WritableFont; 52 import jxl.write.WritableHyperlink; 53 import jxl.write.Label; 54 import jxl.write.NumberFormat; 55 import jxl.write.NumberFormats; 56 import jxl.write.Number; 57 import jxl.write.DateFormat; 58 import jxl.write.DateFormats; 59 import jxl.write.DateTime; 60 import jxl.write.Formula; 61 import jxl.write.WriteException; 62 import jxl.write.WritableImage; 63 import jxl.write.WritableCellFeatures; 64 65 73 public class ReadWrite 74 { 75 78 private static Logger logger = Logger.getLogger(ReadWrite.class); 79 80 83 private File inputWorkbook; 84 87 private File outputWorkbook; 88 89 95 public ReadWrite(String input, String output) 96 { 97 inputWorkbook = new File (input); 98 outputWorkbook = new File (output); 99 logger.setSuppressWarnings(Boolean.getBoolean("jxl.nowarnings")); 100 logger.info("Input file: " + input); 101 logger.info("Output file: " + output); 102 } 103 104 110 public void readWrite() throws IOException , BiffException, WriteException 111 { 112 logger.info("Reading..."); 113 Workbook w1 = Workbook.getWorkbook(inputWorkbook); 114 115 Sheet s = w1.getSheet(0); 116 117 logger.info("Copying..."); 118 WritableWorkbook w2 = Workbook.createWorkbook(outputWorkbook, w1); 119 120 if (inputWorkbook.getName().equals("jxlrwtest.xls")) 121 { 122 modify(w2); 123 } 124 125 w2.write(); 126 w2.close(); 127 logger.info("Done"); 128 } 129 130 136 private void modify(WritableWorkbook w) throws WriteException 137 { 138 logger.info("Modifying..."); 139 140 WritableSheet sheet = w.getSheet("modified"); 141 142 WritableCell cell = null; 143 CellFormat cf = null; 144 145 cell = sheet.getWritableCell(1,3); 147 WritableFont bold = new WritableFont(WritableFont.ARIAL, 148 WritableFont.DEFAULT_POINT_SIZE, 149 WritableFont.BOLD); 150 cf = new WritableCellFormat(bold); 151 cell.setCellFormat(cf); 152 153 cell = sheet.getWritableCell(1,4); 155 WritableFont underline = new WritableFont(WritableFont.ARIAL, 156 WritableFont.DEFAULT_POINT_SIZE, 157 WritableFont.NO_BOLD, 158 false, 159 UnderlineStyle.SINGLE); 160 cf = new WritableCellFormat(underline); 161 cell.setCellFormat(cf); 162 163 cell = sheet.getWritableCell(1,5); 165 WritableFont tenpoint = new WritableFont(WritableFont.ARIAL, 10); 166 cf = new WritableCellFormat(tenpoint); 167 cell.setCellFormat(cf); 168 169 cell = sheet.getWritableCell(1,6); 171 if (cell.getType() == CellType.LABEL) 172 { 173 Label lc = (Label) cell; 174 lc.setString(lc.getString() + " - mod"); 175 } 176 177 cell = sheet.getWritableCell(1,9); 179 NumberFormat sevendps = new NumberFormat("#.0000000"); 180 cf = new WritableCellFormat(sevendps); 181 cell.setCellFormat(cf); 182 183 184 cell = sheet.getWritableCell(1,10); 186 NumberFormat exp4 = new NumberFormat("0.####E0"); 187 cf = new WritableCellFormat(exp4); 188 cell.setCellFormat(cf); 189 190 cell = sheet.getWritableCell(1,11); 192 cell.setCellFormat(WritableWorkbook.NORMAL_STYLE); 193 194 cell = sheet.getWritableCell(1,12); 196 if (cell.getType() == CellType.NUMBER) 197 { 198 Number n = (Number ) cell; 199 n.setValue(42); 200 } 201 202 cell = sheet.getWritableCell(1,13); 204 if (cell.getType() == CellType.NUMBER) 205 { 206 Number n = (Number ) cell; 207 n.setValue(n.getValue() + 0.1); 208 } 209 210 cell = sheet.getWritableCell(1,16); 212 DateFormat df = new DateFormat("dd MMM yyyy HH:mm:ss"); 213 cf = new WritableCellFormat(df); 214 cell.setCellFormat(cf); 215 216 cell = sheet.getWritableCell(1,17); 218 cf = new WritableCellFormat(DateFormats.FORMAT9); 219 cell.setCellFormat(cf); 220 221 cell = sheet.getWritableCell(1,18); 223 if (cell.getType() == CellType.DATE) 224 { 225 DateTime dt = (DateTime) cell; 226 Calendar cal = Calendar.getInstance(); 227 cal.set(1998, 1, 18, 11, 23, 28); 228 Date d = cal.getTime(); 229 dt.setDate(d); 230 } 231 232 cell = sheet.getWritableCell(1,22); 235 if (cell.getType() == CellType.NUMBER) 236 { 237 Number n = (Number ) cell; 238 n.setValue(6.8); 239 } 240 241 cell = sheet.getWritableCell(1, 29); 244 if (cell.getType() == CellType.LABEL) 245 { 246 Label l = (Label) cell; 247 l.setString("Modified string contents"); 248 } 249 sheet.insertRow(34); 251 252 sheet.removeRow(38); 254 255 sheet.insertColumn(9); 257 258 sheet.removeColumn(11); 260 261 sheet.removeRow(43); 264 sheet.insertRow(43); 265 266 WritableHyperlink hyperlinks[] = sheet.getWritableHyperlinks(); 268 269 for (int i = 0; i < hyperlinks.length; i++) 270 { 271 WritableHyperlink wh = hyperlinks[i]; 272 if (wh.getColumn() == 1 && wh.getRow() == 39) 273 { 274 try 275 { 276 wh.setURL(new URL ("http://www.andykhan.com/jexcelapi/index.html")); 278 } 279 catch (MalformedURLException e) 280 { 281 logger.warn(e.toString()); 282 } 283 } 284 else if (wh.getColumn() == 1 && wh.getRow() == 40) 285 { 286 wh.setFile(new File ("../jexcelapi/docs/overview-summary.html")); 287 } 288 else if (wh.getColumn() == 1 && wh.getRow() == 41) 289 { 290 wh.setFile(new File ("d:/home/jexcelapi/docs/jxl/package-summary.html")); 291 } 292 else if (wh.getColumn() == 1 && wh.getRow() == 44) 293 { 294 sheet.removeHyperlink(wh); 296 } 297 } 298 299 WritableCell c = sheet.getWritableCell(5,30); 301 WritableCellFormat newFormat = new WritableCellFormat(c.getCellFormat()); 302 newFormat.setBackground(Colour.RED); 303 c.setCellFormat(newFormat); 304 305 Label l = new Label(0, 49, "Modified merged cells"); 307 sheet.addCell(l); 308 309 Number n = (Number ) sheet.getWritableCell(0, 70); 311 n.setValue(9); 312 313 n = (Number ) sheet.getWritableCell(0, 71); 314 n.setValue(10); 315 316 n = (Number ) sheet.getWritableCell(0, 73); 317 n.setValue(4); 318 319 Formula f = new Formula(1, 80, "ROUND(COS(original!B10),2)"); 321 sheet.addCell(f); 322 323 f = new Formula(1, 83, "value1+value2"); 325 sheet.addCell(f); 326 327 f = new Formula(1, 84, "AVERAGE(value1,value1*4,value2)"); 329 sheet.addCell(f); 330 331 334 Label label = new Label(0, 88, "Some copied cells", cf); 336 sheet.addCell(label); 337 338 label = new Label(0,89, "Number from B9"); 339 sheet.addCell(label); 340 341 WritableCell wc = sheet.getWritableCell(1, 9).copyTo(1,89); 342 sheet.addCell(wc); 343 344 label = new Label(0, 90, "Label from B4 (modified format)"); 345 sheet.addCell(label); 346 347 wc = sheet.getWritableCell(1, 3).copyTo(1,90); 348 sheet.addCell(wc); 349 350 label = new Label(0, 91, "Date from B17"); 351 sheet.addCell(label); 352 353 wc = sheet.getWritableCell(1, 16).copyTo(1,91); 354 sheet.addCell(wc); 355 356 label = new Label(0, 92, "Boolean from E16"); 357 sheet.addCell(label); 358 359 wc = sheet.getWritableCell(4, 15).copyTo(1,92); 360 sheet.addCell(wc); 361 362 label = new Label(0, 93, "URL from B40"); 363 sheet.addCell(label); 364 365 wc = sheet.getWritableCell(1, 39).copyTo(1,93); 366 sheet.addCell(wc); 367 368 for (int i = 0 ; i < 6; i++) 370 { 371 Number number = new Number (1,94+i, i + 1 + i/8.0); 372 sheet.addCell(number); 373 } 374 375 label = new Label(0,100, "Formula from B27"); 376 sheet.addCell(label); 377 378 wc = sheet.getWritableCell(1, 26).copyTo(1,100); 379 sheet.addCell(wc); 380 381 label = new Label(0,101, "A brand new formula"); 382 sheet.addCell(label); 383 384 Formula formula = new Formula(1, 101, "SUM(B94:B96)"); 385 sheet.addCell(formula); 386 387 label = new Label(0,102, "A copy of it"); 388 sheet.addCell(label); 389 390 wc = sheet.getWritableCell(1,101).copyTo(1, 102); 391 sheet.addCell(wc); 392 393 WritableImage wi = sheet.getImage(1); 395 sheet.removeImage(wi); 396 397 wi = new WritableImage(1, 116, 2, 9, 398 new File ("resources/littlemoretonhall.png")); 399 sheet.addImage(wi); 400 401 cell = sheet.getWritableCell(0, 156); 403 l = (Label) cell; 404 l.setString("Label text modified"); 405 406 cell = sheet.getWritableCell(0, 157); 407 WritableCellFeatures wcf = cell.getWritableCellFeatures(); 408 wcf.setComment("modified comment text"); 409 } 410 } 411 412 413 414 415 416 417 418 419 420 | Popular Tags |