FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display
data from them. Only works if Formulas are enabled in "RecordFactory"
| Method from org.apache.cocoon.poi.hssf.dev.FormulaViewer Detail: |
public String composeForumla(FormulaRecord record) {
StringBuffer formula = new StringBuffer("=");
int numptgs = record.getNumberOfExpressionTokens();
List ptgs = record.getParsedExpression();
for (int ptgnum = numptgs - 1; ptgnum > (-1); ptgnum--)
{
Ptg ptg = ( Ptg ) ptgs.get(ptgnum);
OperationPtg optg = ( OperationPtg ) ptg;
int numops = optg.getNumberOfOperands();
Ptg[] ops = new Ptg[ numops ];
int opoffset = 1;
for (int opnum = ops.length - 1; opnum > -1; opnum--)
{
ops[ opnum ] = ( Ptg ) ptgs.get(ptgnum - opoffset);
opoffset++;
}
formula.append(optg.toFormulaString(ops));
ptgnum -= ops.length;
}
return formula.toString();
}
|
public static void main(String[] args) {
if ((args == null) || (args.length != 1)
|| args[ 0 ].equals("--help"))
{
System.out.println(
"FormulaViewer .8 proof that the devil lies in the details (or just in BIFF8 files in general)");
System.out.println("usage: Give me a big fat file name");
}
else
{
try
{
FormulaViewer viewer = new FormulaViewer();
viewer.setFile(args[ 0 ]);
viewer.run();
}
catch (Exception e)
{
System.out.println("Whoops!");
e.printStackTrace();
}
}
}
Method main
pass me a filename and I'll try and parse the formulas from it |
public void parseFormulaRecord(FormulaRecord record) {
System.out.println("In ParseFormula Record");
System.out.println("row = " + record.getRow());
System.out.println("col = " + record.getColumn());
System.out.println("value = " + record.getValue());
System.out.println("xf = " + record.getXFIndex());
System.out.println("number of ptgs = "
+ record.getNumberOfExpressionTokens());
System.out.println("options = " + record.getOptions());
System.out.println(composeForumla(record));
}
Method parseFormulaRecord |
public void run() throws Exception {
Filesystem fs = new Filesystem(new FileInputStream(file));
Record[] records =
RecordFactory
.createRecords(fs.createDocumentInputStream("Workbook"));
for (int k = 0; k < records.length; k++)
{
Record record = ( Record ) records[ k ];
if (record.getSid() == FormulaRecord.sid)
{
parseFormulaRecord(( FormulaRecord ) record);
}
}
}
|
public void setFile(String file) {
this.file = file;
}
|