1

我正在尝试使用 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();
}
}
4

2 回答 2

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();
    }
}
于 2012-03-08T07:41:43.130 回答
0

那么,没有输入 CellType.Number 的 if 案例的原因是公式单元格不是数字单元格。它是一个公式单元格。除了公式本身之外,它没有“值”(解析函数名称以标记它们,但未评估)。它返回 CellType.ERROR(至少在 jxl 2.6.9 中)。该库创建带有单元格的 excel 文件,它不评估任何公式,甚至不检查它的有效语法。它只是将它们解析为函数名称(令牌),因此函数名称的 I18N 有效。从已由 excel打开、计算和保存的 excel 文件中读取公式单元格时,公式单元格具有 CellType.NUMBER_FORMULA 并实现接口以读取公式和公式结果(由 excel 评估)。

您不能创建公式单元格,将其添加到工作表,然后立即读取结果。

于 2018-01-17T16:57:43.937 回答