这是我的第一篇文章,请耐心等待。我正在尝试做一些简单的事情:查看包含公式的电子表格单元格的值。似乎没有什么工作,而不是正确的答案(579),我得到“4”。 (下面的输出)。该程序创建一个简单的电子表格,其中包含A1和A2中的数字,以及A3中的总和。
import java.io.*;
import jxl.*;
import jxl.Workbook;
import jxl.biff.FormulaData;
import jxl.biff.formula.FormulaException;
import jxl.write.Number;
import jxl.write.*;
import jxl.write.Formula;
import jxl.read.biff.CellValue;
public class ReadCell {
public static void main(String[] args) throws IOException,
jxl.read.biff.BiffException,
WriteException, FormulaException
{
String filename = "readcell.xls";
WritableWorkbook workbook = Workbook.createWorkbook(new File(filename));
WritableSheet ws = workbook.createSheet("Sheet1", 0);
Number n = new Number(0,0,123); // 123 in A1
ws.addCell(n);
n = new Number(0,1,456); // 456 in A2
ws.addCell(n);
Formula f = new Formula(0,2, "A1+A2"); // formula in A3, yields 579
ws.addCell(f);
workbook.write();
workbook.close();
//now try to retrieve the sum of A1 + A2, which is in A3 (value should be 579)
Workbook wb = Workbook.getWorkbook( new File (filename));
Sheet sheet = wb.getSheet(0);
Cell cell = sheet.getCell("A3");
System.out.println("\n\nCell A3 contents = " + cell.getContents());
//that didn't work, maybe try it by casting to a NumberCell?
NumberCell numbercell = (NumberCell)cell;
System.out.println("NumberCell value = " + numbercell.getValue());
System.out.println("NumberCell contents = " + numbercell.getContents());
//that didn't work, maybe try it by casting to a FormulaCell?
FormulaCell fc = (FormulaCell)cell;
System.out.println("FormulaCell contents = " + fc.getContents());
System.out.println("FormulaCell formula = " + fc.getFormula());
//that didn't work, maybe try to get contents by casting to FormulaData?
FormulaData fd = (FormulaData)cell;
System.out.println("FormulaData contents = " + fd.getContents());
System.out.println("FormulaData type = " + fd.getType());
//that didn't work, maybe try to get contents by casting to NumberFormulaCell?
NumberFormulaCell nfc = (NumberFormulaCell)cell;
System.out.println("NumberFormulaCell value = " + nfc.getValue());
System.out.println("NumberFormulaCell contents = " + nfc.getContents());
System.out.println("NumberFormulaCell formula = " + nfc.getFormula());
//that didn't work, try to get contents by casting to CellValue?
CellValue cv = (CellValue)cell;
System.out.println("cellValue contents = " + cv.getContents());
//Nothing works and these casts won't compile:
//Number nnn = (Number)cell;
//NumberFormulaRecord nfr = (NumberFormulaRecord)cell;
wb.close();
}
}
这是输出,“4”应为“579”
细胞A3含量= 4
NumberCell值= 4.0
NumberCell contents = 4
FormulaCell contents = 4
FormulaCell公式= A1 + A2
FormulaData contents = 4
FormulaData type = Numerical Formula
NumberFormulaCell值= 4.0
NumberFormulaCell contents = 4
NumberFormulaCell公式= A1 + A2
cellValue contents = 4
感谢您提供的任何帮助!
答案 0 :(得分:0)
我运行了编写工作簿的代码。然后我在Excel中打开了工作簿。当我告诉它关闭时,系统会提示我一条消息:" Microsoft Excel在打开上次保存的早期版本的文件"时重新计算公式。选择保存,然后运行读取的代码(但不是编写工作簿的代码)后,我得到了正确的单元格值579.当我删除文件并运行您发布的所有代码时,我得到了4.它看起来喜欢这个公式需要先计算才能读出来。这篇文章似乎说没有修复它,并假设为什么:Updated excel file still returns old values