0

在此处输入图像描述

[更好的质量]:。http://imageshack.us/photo/my-images/51/v5cg.png/

Problem: 我在 workBook.xlsx 中使用公式。根据情况我改变值(3,3)。在此示例中,我将 10 更改为 20。之后我想计算使用此单元格的公式。完成后,它将覆盖单元格的值,删除公式并记录值。该单元格不再用于计算。

Code:

public class ReaderXlsx {
    public List<List<String>> ReaderXlsx(String sfilename, int firstColumn, int columnsCount, int rowsCount, int moduleCount){
        int lastColumn=firstColumn+columnsCount;
        List<List<String>> rowsContent = new ArrayList<List<String>>();
        try ( FileInputStream fileInputStream = new FileInputStream("C:\\Users\\student3\\"+sfilename+".xlsx");)
        {
            XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream);
            XSSFSheet sheet = workBook.getSheetAt(0);
            int firstRow = findOneInFirstColumn(sheet,50);
            setModuleCount(sheet,moduleCount);
            workBook.getCreationHelper().createFormulaEvaluator().evaluateAll();

            toNewLine:
            for (int lineId=firstRow;lineId<rowsCount;lineId++)            {
                List<String> columnsContent = new ArrayList<String>();
                Row row = sheet.getRow(lineId);
                if (row==null){continue toNewLine;}
                if (row.getCell(2)==null || row.getCell(2).getStringCellValue().equals("") ) {continue toNewLine;}
                    for (int columnId=firstColumn+1;columnId<lastColumn;columnId++)     {
                        Cell cell = row.getCell(columnId-1);
                        if ((cell==null)) { continue toNewLine;}
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        if ((columnId==0 & cell.getStringCellValue().equals(""))) {continue toNewLine;}
                        if ((columnId==5 & cell.getStringCellValue().equals("")) || (columnId==6 & cell.getStringCellValue().equals("")))  cell.setCellValue("0");
                        columnsContent.add(cell.getStringCellValue());
                    }
                rowsContent.add(columnsContent);
            }
            try(FileOutputStream out = new FileOutputStream("C:\\Users\\student3\\Used"+sfilename+".xlsx"); ) {
                workBook.write(out);
                out.close();         }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return rowsContent;
    }
    private Integer findOneInFirstColumn(XSSFSheet sheet, Integer maxRows){
        int result=0;
        toNextLine:
        for (int lineId=0;lineId<maxRows;lineId++){
        Row row = sheet.getRow(lineId);
            if (row==null | row.getCell(0)==null) {result++; continue toNextLine; }
            else{
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                if (row.getCell(0).getStringCellValue().equals("1")){return result;}
                else {result++;}
            }
        }
        return result;
    }
    private void setModuleCount(XSSFSheet sheet, Integer moduleCount){
        Row row = sheet.getRow(1);
        if (moduleCount==0) {}
        if (moduleCount>0) {row.createCell(2).setCellValue(moduleCount.toString());}
    }

}

现在我使用 2 个文件,因为我需要保存我的公式。我只想使用 1 (源)并正确更新它。

4

0 回答 0