简体   繁体   English

在java jxl api中使用excel公式结果

[英]Use excel formula result in java jxl api

I am trying to use the result of a formula in excel to divide other cells. 我试图在excel中使用公式的结果来划分其他单元格。 My code is as follows: 我的代码如下:

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();
}
}

Unfortunately this is giving me the following exception: 不幸的是,这给了我以下例外:

Exception in thread "main" java.lang.ClassCastException: jxl.write.Formula cannot be cast to jxl.NumberFormulaCell at CreateExcel.main(CreateExcel.java:31) 线程“main”java.lang.ClassCastException中的异常:jxl.write.Formula无法在CreateExcel.main(CreateExcel.java:31)中强制转换为jxl.NumberFormulaCell

Does anyone know how this is supposed to be done?? 有谁知道应该如何做?

EDIT: 编辑:

This is my revised code which essentially does what I want it to do just in a longer way. 这是我修改后的代码,它基本上以更长的方式完成了我想要它做的事情。 I calculate what the output of what formula f will equal and keep that rather then accessing the formula's result directly. 我计算什么公式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();
}
}

The exception is clear, you cannot cast return of method getCell() to NumberFormulaCell , it return Cell interface. 例外情况很明显,你不能将方法getCell()返回到NumberFormulaCell ,它返回Cell接口。 They are different interface. 它们是不同的界面。 I have source code changes below, I do not have your excel source file and so cannot really test it but that class cast exception should not be an issue now. 我在下面有源代码更改,我没有你的excel源文件,因此无法真正测试它,但现在该类转换异常应该不是问题。 With these changes below without the original excel source file, that line with Sheet.addCell give exception on my workstation but that is entirely different issue from the one you reported. 如果没有原始的excel源文件,下面的这些更改, Sheet.addCell我的工作站上给出了异常,但这与你报告的那个完全不同。 Hope it works on your end. 希望它适合你。

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();
    }
}

Well, the reason why the if case for CellType.Number isn't entered is that the formula cell is not a number cell. 那么,没有输入CellType.Number的if情况的原因是公式单元格不是数字单元格。 It is a formula cell. 它是一个公式细胞。 It has no 'value' othe rthan the formula itself (parsed for function names to tokenize them, but not evaluated). 它没有公式本身的“价值”(解析函数名称来标记它们,但不进行评估)。 And it returns CellType.ERROR (at least in jxl 2.6.9). 它返回CellType.ERROR(至少在jxl 2.6.9中)。 The library creates excel files with cells, it does not evaluate any formula or even check it for valid syntax. 该库使用单元格创建excel文件,它不评估任何公式,甚至检查它是否有效。 It just parses them for function names (tokens), so I18N for function names works. 它只是解析函数名称(标记),因此函数名称的I18N可以正常工作。 When reading a formula cell from an excel file that has been opened, calculated and saved by excel , then the foumula cell has the CellType.NUMBER_FORMULA and implements interfaces to read both, the formula and the formula result (as evaluated by excel). 当从excel 打开,计算和保存的excel文件中读取公式单元格时,则foumula单元格具有CellType.NUMBER_FORMULA并实现接口以读取公式和公式结果(由excel评估)。

You can't create a formula cell, add it to the sheet and then instantly read the result. 您无法创建公式单元格,将其添加到工作表中,然后立即读取结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM