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.IOException ; 27 import java.io.UnsupportedEncodingException ; 28 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 32 import jxl.Workbook; 33 import jxl.Sheet; 34 import jxl.Cell; 35 import jxl.CellType; 36 import jxl.FormulaCell; 37 import jxl.biff.CellReferenceHelper; 38 import jxl.biff.formula.FormulaParser; 39 import jxl.biff.formula.ExternalSheet; 40 import jxl.biff.formula.FormulaException; 41 42 47 public class Formulas 48 { 49 58 public Formulas(Workbook w, OutputStream out, String encoding) 59 throws IOException 60 { 61 if (encoding == null || !encoding.equals("UnicodeBig")) 62 { 63 encoding = "UTF8"; 64 } 65 66 try 67 { 68 OutputStreamWriter osw = new OutputStreamWriter (out, encoding); 69 BufferedWriter bw = new BufferedWriter (osw); 70 71 ArrayList parseErrors = new ArrayList (); 72 73 for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++) 74 { 75 Sheet s = w.getSheet(sheet); 76 77 bw.write(s.getName()); 78 bw.newLine(); 79 80 Cell[] row = null; 81 Cell c = null; 82 83 for (int i = 0 ; i < s.getRows() ; i++) 84 { 85 row = s.getRow(i); 86 87 for (int j = 0; j < row.length; j++) 88 { 89 c = row[j]; 90 if (c.getType() == CellType.NUMBER_FORMULA || 91 c.getType() == CellType.STRING_FORMULA || 92 c.getType() == CellType.BOOLEAN_FORMULA || 93 c.getType() == CellType.DATE_FORMULA || 94 c.getType() == CellType.FORMULA_ERROR) 95 { 96 FormulaCell nfc = (FormulaCell) c; 97 StringBuffer sb = new StringBuffer (); 98 CellReferenceHelper.getCellReference 99 (c.getColumn(), c.getRow(), sb); 100 101 try 102 { 103 bw.write("Formula in " + sb.toString() + 104 " value: " + c.getContents()); 105 bw.flush(); 106 bw.write(" formula: " + nfc.getFormula()); 107 bw.flush(); 108 bw.newLine(); 109 } 110 catch (FormulaException e) 111 { 112 bw.newLine(); 113 parseErrors.add(s.getName() + '!' + 114 sb.toString() + ": " + e.getMessage()); 115 } 116 } 117 } 118 } 119 } 120 bw.flush(); 121 bw.close(); 122 123 if (parseErrors.size() > 0) 124 { 125 System.err.println(); 126 System.err.println("There were " + parseErrors.size() + " errors"); 127 128 Iterator i = parseErrors.iterator(); 129 while (i.hasNext()) 130 { 131 System.err.println(i.next()); 132 } 133 } 134 } 135 catch (UnsupportedEncodingException e) 136 { 137 System.err.println(e.toString()); 138 } 139 } 140 141 } 142 143 | Popular Tags |