1 /
55
56
62 package org.apache.poi.hssf.dev;
63
64 import java.io.InputStream;
65 import java.io.IOException;
66 import java.io.ByteArrayInputStream;
67 import java.io.FileInputStream;
68 import java.io.FileOutputStream;
69
70
71 import java.util.List;
72
73 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
74 import org.apache.poi.util.LittleEndian;
75 import org.apache.poi.util.HexDump;
76 import org.apache.poi.hssf.record.*;
77 import org.apache.poi.hssf.record.formula.*;
78 import org.apache.poi.hssf.model.*;
79 import org.apache.poi.hssf.usermodel.*;
80
81
87
88 public class FormulaViewer
89 {
90 private String file;
91
92
93
94 public FormulaViewer()
95 {
96 }
97
98
105
106 public void run()
107 throws Exception
108 {
109 POIFSFileSystem fs =
110 new POIFSFileSystem(new FileInputStream(file));
111 List records =
112 RecordFactory
113 .createRecords(fs.createDocumentInputStream("Workbook"));
114
115 for (int k = 0; k < records.size(); k++)
116 {
117 Record record = ( Record ) records.get(k);
118
119 if (record.getSid() == FormulaRecord.sid)
120 {
121 parseFormulaRecord(( FormulaRecord ) record);
122 }
123 }
124 }
125
126 /**
127 * Method parseFormulaRecord
128 *
129 *
130 * @param record
131 *
132 */
133
134 public void parseFormulaRecord(FormulaRecord record)
135 {
136 System.out.println("==============================");
137 System.out.print("row = " + record.getRow());
138 System.out.println(", col = " + record.getColumn());
139 System.out.println("value = " + record.getValue());
140 System.out.print("xf = " + record.getXFIndex());
141 System.out.print(", number of ptgs = "
142 + record.getNumberOfExpressionTokens());
143 System.out.println(", options = " + record.getOptions());
144 System.out.println("RPN List = "+formulaString(record));
145 System.out.println("Formula text = "+ composeForumla(record));
146 }
147
148 private String formulaString(FormulaRecord record) {
149 StringBuffer formula = new StringBuffer("=");
150 int numptgs = record.getNumberOfExpressionTokens();
151 List tokens = record.getParsedExpression();
152 StringBuffer buf = new StringBuffer();
153 for (int i=0;i<numptgs;i++) {
154 buf.append( ( (Ptg)tokens.get(i)).toFormulaString());
155 buf.append(' ');
156 }
157 return buf.toString();
158 }
159
160
161 private String composeForumla(FormulaRecord record)
162 {
163 return FormulaParser.toFormulaString(record.getParsedExpression());
164 }
165
166 /**
167 * Method setFile
168 *
169 *
170 * @param file
171 *
172 */
173
174 public void setFile(String file)
175 {
176 this.file = file;
177 }
178
179 /**
180 * Method main
181 *
182 * pass me a filename and I'll try and parse the formulas from it
183 *
184 * @param args pass one argument with the filename or --help
185 *
186 */
187
188 public static void main(String args[])
189 {
190 if ((args == null) || (args.length != 1)
191 || args[ 0 ].equals("--help"))
192 {
193 System.out.println(
194 "FormulaViewer .8 proof that the devil lies in the details (or just in BIFF8 files in general)");
195 System.out.println("usage: Give me a big fat file name");
196 }
197 else
198 {
199 try
200 {
201 FormulaViewer viewer = new FormulaViewer();
202
203 viewer.setFile(args[ 0 ]);
204 viewer.run();
205 }
206 catch (Exception e)
207 {
208 System.out.println("Whoops!");
209 e.printStackTrace();
210 }
211 }
212 }
213 }
214 ??????????????????createRecords????????????????????????????????fs???????????????????????????????????createDocumentInputStream?????????????????????????k?????????????????????????????records?????????????????????????????????????????????k?????????????Record???????????????????????????????Record????????????????????????????????????????records????????????????????????????????????????????????????k?????????????????record????????????????????????getSid????????????????????????????????????FormulaRecord??????????????????????????????????????????????????sid?????????????????parseFormulaRecord??????????????????????????????????????FormulaRecord??????????????????????????????????????????????????????record??????????????????????parseFormulaRecord????????????????????????????????????FormulaRecord?????????????????????????????????????record?????????????????????????????????????????record?????????????????????????????????????????record????????????????????????????????????record??????????????????????????????record?????????????????????????????????????????????record??????????????????????????????????????????formulaString????????????????????????????????????????????????????????record???????????????????????????????????????????????composeForumla??????????????????????????????????????????????????????????????record????????????????????formulaString??????????????????????????????????FormulaRecord????????????????????????????????record??????????????????????????????????record?????????????????????????i???????????????????????????numptgs???????????????????????????????????i?????????????buf????????????????????????????Ptg????????????????????????????????tokens???????????????????????????????????????????i?????????????buf????????????????buf????????????????????composeForumla???????????????????????????????????FormulaRecord????????????????FormulaParser??????????????????????????????toFormulaString??????????????????????????????????????????????record??????????????????????setFile?????????????????????file?????????????????????????????main??????????????args????????????????????????????????args????????????????????args?????????????????FormulaViewer????????????????????????????????????????????FormulaViewer?????????????????viewer????????????????????????setFile????????????????????????????????args?????????????????viewer????????????????????????run?????????????????e