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

标签 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);
            }
        }
    }
}

}

最佳答案

我不会重写您的代码,但您可以从中获得帮助。创建一个包含城市和公式列的 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

关于java - 当前值/单元格 POI 的 Excel 公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53483652/

相关文章:

java - Sonarqube 中是否真的需要混淆三元规则?

python - 将 Excel 数据加速到 Pandas

vba - 运行时错误'7' : Out of Memory when moving columns

JAVA POI内存不足问题

java - Apache POI 只写入一条记录

java - 如何使用poi jar在java api中读取docx文件内容

java - ArrayList indexOf() 返回错误的索引?

java - 如何遍历文件夹中的所有文件(如果文件名未知)?

java - 需要在 Tomcat 中将两个应用程序映射到相同的基本 URL 下

vba - 如何重复执行 CommandButton1 直到满足条件?