2012-03-08 54 views
1

我想在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(); 
} 
} 
+1

請問我們知道這個問題解決了嗎?這將有益於社區中的其他人...... – Mike 2013-09-16 11:08:17

回答

3

唯一的例外是明確的,你不能施放方法getCell()的迴歸NumberFormulaCell,它返回Cell接口。它們是不同的界面。我在下面有源代碼更改,我沒有你的Excel源文件,所以不能真正測試它,但該類拋出異常現在不應該是一個問題。在沒有原始excel源文件的情況下,在下面的這些更改中,與Sheet.addCell相關的行會在我的工作站上發生異常,但這與您所報告的問題完全不同。希望它能在你的最終結果。

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 = 1; 
     for (int i = 8; i < 68; i++) 
     { 
      Cell cell1 = domesticSheet.getCell(3, i); 

      if (cell1.getType() == CellType.ERROR) 
      { 
       System.out.println("this cell is not exist!"); 
      } 

      if (cell1.getType() == CellType.NUMBER) 
      { 
       NumberCell nc = (NumberCell)cell1; 
       x = nc.getValue(); 
       System.out.println(x); 
       ASheet.addCell(new Label(1, i, String.valueOf(Double.valueOf(domesticSheet.getCell(1, i).getContents())/x))); 
      }       
     } 
     copy.write(); 
     copy.close(); 
    } 
} 
+0

是啊,我知道異常似乎很清楚,只是我看到一個例子,它像這樣投了,所以我很困惑,並尋找正確的方法。我試過你的代碼,但公式將永遠不會輸入,如果聲明。也許我沒有足夠清楚地解釋我的目標。我有一個只包含公式的列。 (SUM(AX *:BE *)其中*是一個整數)。我想獲取這些單元格中每個單元格的計算數值,而不是獲取公式或表示此公式的字符串。 – 2012-03-08 21:05:23

+0

@ user1256415您可以上傳帶有鏈接的源文件USA-IO-2005.xls,因此我可以在這裏查看它嗎? – Jasonw 2012-03-09 02:49:58

+0

https://docs.google.com/spreadsheet/ccc?key=0AkBMdTFRb0T-dHFXTU5TMXI2c0FTTDRKSEJRZWVwRlE#gid=0 – 2012-03-09 03:56:44

0

那麼,沒有輸入CellType.Number的情況下,原因是公式單元格不是數字單元格。這是一個公式單元格。它除了公式本身之外沒有任何「價值」(解析函數名稱來標記它們,但未評估)。它返回CellType.ERROR(至少在jxl 2.6.9中)。該庫創建單元格的Excel文件,它不評估任何公式,甚至檢查它的有效語法。它只是解析它們的函數名稱(令牌),所以函數名稱的I18N起作用。 當從Excel讀取式細胞文件已經打開,計算並用Excel保存,則foumula電池具有CellType.NUMBER_FORMULA並實現接口讀取二者,式和式結果(通過Excel作爲評價)。

您不能創建公式單元格,將它添加到工作表,然後立即讀取結果。