我想在Excel中使用公式的結果來劃分其他單元格。我的代碼如下:使用Excel中的公式結果java jxl api
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不能轉換爲jxl.NumberFormulaCell 在CreateExcel.main(CreateExcel.java:31)
有誰知道這是怎麼應該做?
編輯:
這是我修改後的代碼基本上沒有我想要它做的只是在一個更長的路是什麼。我計算什麼公式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();
}
}
請問我們知道這個問題解決了嗎?這將有益於社區中的其他人...... – Mike 2013-09-16 11:08:17