简体   繁体   English

Excel公式未使用Apache POI从Java应用程序中删除行删除

[英]Excel formula not updating on row delete from java application using Apache POI

I'm using Apache POI in my application to write data to an excel file. 我在我的应用程序中使用Apache POI将数据写入excel文件。 I've an excel file template and a few formulas are also there in it. 我有一个excel文件模板,其中还有一些公式。 In my application, i use the excel template, write into it ,then delete unused rows and calculate formulas in the end. 在我的应用程序中,我使用excel模板,写入,然后删除未使用的行并最终计算公式。 I'm using SUM formula in the file. 我在文件中使用SUM公式。 The problem is when rows are deleted, the SUM formula is not updating,due to which error values are coming up in excel. 问题是当删除行时,SUM公式不会更新,因为excel中会出现错误值。

Example : the formula being used is : for cell B215 : SUM(B15:B214). 示例:使用的公式为:对于单元格B215:SUM(B15:B214)。 in the application,after writing to the file i delete unused rows. 在应用程序中,写入文件后我删除了未使用的行。 now I've data till 70th row in the file.All other rows have been deleted. 现在我的数据直到文件中的第70行。所有其他行都被删除了。 So my formula should get updated to : SUM(B15:B69) for cell B70. 所以我的公式应该更新为:单元格B70的SUM(B15:B69)。 But in the file it's still showing the formula as SUM(B15:B214). 但是在文件中它仍然显示公式为SUM(B15:B214)。 Hence the value of that cell is "VALUE# 因此,该单元格的值为“VALUE#”

Code snippet : 代码段:

File file = new File(path)
InputStream is = new FileInputStream(file)
POIFSFileSystem fs = new POIFSFileSystem(is)
HSSFWorkbook wb = new HSSFWorkbook(fs)
HSSFSheet excelSheet
int[] indexArray = populateSheet(excelSheet)

//indexArray is array with 3 values as startrow, lastrow, and first empty row. // indexArray是具有3个值的数组,如startrow,lastrow和第一个空行。

removeBlankRows(excelSheet,indexArray)



//evaluate formula

FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator()
for(HSSFRow r : excelSheet) {
 for(HSSFCell c : r) {
  if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
    String formula = c.getCellFormula();
    evaluator.evaluateFormulaCell(c)
    }
 }
}

private void removeBlankRows(HSSFSheet sheet, int[] shiftInfo){
        for(int i = shiftInfo[2]; i <= shiftInfo[1]; ++i) {
            sheet.removeRow(sheet.getRow(i))
        }
        //Shift up the rows
        def startRow = shiftInfo[1]+1
        def endRow = sheet.getLastRowNum()
        def rowCount = -1* (shiftInfo[1] - shiftInfo[2] + 1)
        sheet.shiftRows(startRow, endRow, rowCount)
    }

This is an Excel bug. 这是一个Excel错误。 I've dealt with this in the past by doing the following: 我过去通过以下方式解决了这个问题:

  1. Label the sum cell BSUM 标记和细胞BSUM
  2. We need a stable range that won't be affected by inserts/deletes. 我们需要一个不受插入/删除影响的稳定范围。 Add a formula to a safe (one that won't get deleted) cell, for this example D15: ="B15:B"&ROW(BSUM)-1 This will produce a stable range. 将公式添加到安全(一个不会被删除的)单元格,对于此示例D15:=“B15:B”&ROW(BSUM)-1这将产生一个稳定的范围。
  3. Use INDIRECT in the BSUM cell like so: 在BSUM单元格中使用INDIRECT,如下所示:
    =SUM(INDIRECT(D15)) = SUM(INDIRECT(D15))

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

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