当前值/单元格POI的Excel公式

时间:2018-11-26 14:49:41

标签: java excel apache-poi formula

我正在研究POI应用程序以便操纵excel文件。

实际上,用户正在提供公式和文件,而我正在将公式应用于输出文件。公式必须修改单元格上列的值。

例如,在B列上,我想在所有列上应用公式。 用户给了我LEFT(x,2),我必须将其应用于所有列。 (x定义了所有列)

但是当我应用公式时,我将公式作为字符串。我尝试在公式中传递单元格值,但是它当然不起作用...

我想我应该将所有数据复制到另一个excel文件中,对其进行处理,然后将其复制粘贴到输出文件中,否则它们是另一种方式吗?

此致

代码:

for (int i = 0; i < cell[0].length; i++){ //Checking the header
    for (int j = 0; j < ruleArray.length; j++){  //Checking the Header of the array with the rule to apply
        if (cell[0][i].toString().equals(ruleArray[j][0])){  //Comparing 
        String testF = ruleArray[j][1];
            if (testF.contains("X") || testF.contains("x")){ //Replacing X with value for the formula
                for (int k = 0; k < cell.length; k++){
                indexT = cell[0][i].getColumnIndex(); 
                indexC = cell[k][i].getRowIndex()+1;
                String colLetter = CellReference.convertNumToColString(indexT);

                formula = testF.replace("x", colLetter+indexC);
                cell[k][i].setCellType(CellType.FORMULA);      
                cell[k][i].setCellFormula(formula);
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

我不是在重写您的代码,但是您可以从中寻求帮助。使用列城市和公式创建一个Excel文件,然后运行此代码。我已经附上了一些excel文件的快照。我的工具包会帮助您。 LEFT(X,2)仅解析字符串中的前两个字符

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class TestProblem

{
    public static void main(String[] args) throws IOException {


        InputStream inp = null;
        inp = new FileInputStream("E:\\Projects\\PoiAdvanceExample\\stackProblem.xlsx");

        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);

        int rowsCount = sheet.getLastRowNum();
        int columnCount = sheet.getRow(0).getLastCellNum();
        String[][] inputData = new String[rowsCount+1][columnCount];

        for (int i = 0; i <= rowsCount; i++) {
            Row row = sheet.getRow(i);
            int colCounts = row.getLastCellNum();
            for (int j = 0; j < colCounts; j++) {
                Cell cell = row.getCell(j);
                if(cell.getCellType() == CellType.NUMERIC) {
                    inputData[i][j] = Double.toString(cell.getNumericCellValue());
                }
                if(cell.getCellType() == CellType.FORMULA) {
                    inputData[i][j] = cell.getCellFormula();
                }
                if(cell.getCellType() == CellType.STRING) {
                    inputData[i][j] = cell.getStringCellValue();
                }
            }
        }

       writeData(inputData);

    }

    private static void writeData(String[][] inputData) throws IOException {

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        int r = 0;
        for (String[] dataRow : inputData) {
            Row row = sheet.createRow(r++);
            int column = 0;
            for (String dataCell : dataRow) {
                Cell cell = row.createCell(column++);
                if (r == 1 || column == 1) cell.setCellValue(dataCell);

                else if (column == 2) {
                    CellReference cellReference = new CellReference(cell);
                    String thisR = cellReference.getCellRefParts()[1];
                    cell.setCellFormula("LEFT(A" + thisR + ",2)");
                }
            }
        }


        FileOutputStream fileOut = new FileOutputStream("stackProblem.xlsx");
        workbook.write(fileOut);
        workbook.close();
    }
}

运行前的Excel文件将是这样。

Excel file before run like this

运行此代码后的Excel文件将如下所示。

Excel file after run this code