我正在尝试使用 excel 中的公式结果来划分其他单元格。我的代码如下:
import java.io.File;
import jxl.*;
import jxl.write.*;
public class CreateExcel
{
public static void main(String[] args) throws Exception
{
Workbook workbook = Workbook.getWorkbook(new File("USA-IO-2005.xls"));
WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"),
workbook);
copy.removeSheet(0);
copy.removeSheet(2);
WritableSheet domesticSheet = copy.getSheet(1);
WritableSheet ASheet = copy.getSheet(0);
for (int i = 8; i < 68; i++)
{
Formula f = new Formula(59, i - 1, "SUM(AX" + i + ":BE" + i
+ ") - BF" + i);
domesticSheet.addCell(f);
}
double x;
for (int i = 8; i < 68; i++)
{
NumberFormulaCell newCell = (NumberFormulaCell) domesticSheet.getCell(59, i);
x = newCell.getValue();
ASheet.addCell(new Label(1, i, String.valueOf(Double
.valueOf(domesticSheet.getCell(1, i).getContents()) / x)));
}
copy.write();
copy.close();
}
}
不幸的是,这给了我以下例外:
线程“主”java.lang.ClassCastException 中的异常:jxl.write.Formula 无法在 CreateExcel.main(CreateExcel.java:31) 处转换为 jxl.NumberFormulaCell
有谁知道这应该怎么做??
编辑:
这是我修改后的代码,它基本上做了我希望它以更长的方式做的事情。我计算公式 f 的输出将等于并保留它,而不是直接访问公式的结果。
import java.io.File;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;
import jxl.SheetSettings;
public class CreateExcel
{
public static void main(String[] args) throws Exception
{
Workbook workbook = Workbook.getWorkbook(new File("USA-IO-2005.xls"));
WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"), workbook);
copy.removeSheet(3);
WritableSheet domesticSheet = copy.getSheet(2);
WritableSheet ASheet = copy.getSheet(1);
WritableSheet RDSheet = copy.getSheet(0);
ASheet.setName("A Matrix");
double x = 1;
double[] xVals = new double[60];
//WritableCellFormat cellFormat = new WritableCellFormat(new NumberFormat("#.########"));
WritableCellFormat arial8format = new WritableCellFormat (new WritableFont(WritableFont.ARIAL, 8));
ASheet.addCell(new Label(1, 3, "A Matrix", arial8format));
for (int i = 8; i < 56; i++)
{
//Workaround: Gets the same result as formula f. Just wont update as numbers in excel are changed in the future
for (int j = 49; j < 57; j++)
xVals[i - 8] += Double.valueOf(domesticSheet.getCell(j, i - 1).getContents());
xVals[i - 8] -= Double.valueOf(domesticSheet.getCell(57, i - 1).getContents());
Number num = new Number(60, i - 1, xVals[i - 8], arial8format);
Formula f = new Formula(59, i - 1, "SUM(AX" + i + ":BE" + i + ") - BF" + i, arial8format);
domesticSheet.addCell(f);
domesticSheet.addCell(num);
for (int j = 1; j < 49; j++)
{
ASheet.setColumnView(j, 10);
if (xVals[i - 8] != 0)
x = Double.valueOf(domesticSheet.getCell(j, i - 1).getContents()) / xVals[i - 8];
else
x = 0;
ASheet.addCell(new Number(j, i - 1, x, arial8format));
}
}
SheetSettings s = new SheetSettings(ASheet);
s.setVerticalFreeze(1);
s.setHorizontalFreeze(1);
ASheet.removeRow(55);
ASheet.removeRow(56);
for(int i = 0; i < 6; i ++)
ASheet.removeRow(0);
domesticSheet.removeRow(55);
domesticSheet.removeRow(56);
for(int i = 0; i < 6; i ++)
domesticSheet.removeRow(0);
copy.write();
copy.close();
}
}