2017-08-03 66 views
0

我在做什么错? 文件excelFileNew.xlsm中单元格D5的值为:#DIV/0! 为什么?Apache POI:如何用许多公式更新excel文件?

- 程序输出:

Formula is: C34 
1. case Cell.CELL_TYPE_NUMERIC --> Last evaluated as: 407.2521754511886 
Formula is: C34 

- 单元格值:

Value of the cell D5 in file excelFileOrig.xlsm is: 407,25 
Value of the cell D5 in file excelFileNew.xlsm is: #DIV/0! 
Value of the cell C8 in file excelFileOrig.xlsm is: 5,0 
Value of the cell D5 in file excelFileNew.xlsm is: 15,0 

- 验证码:看1个回答

+1

什么是'C34'? –

+0

所述链的开头看起来像这样: D5 = C34 C34 = SUM(A8:A13) A8 = IFERROR(SUM(B22); 0) A9 “是空的” A10 = SUM(B24) A11 “空” A12 = SUM(B26) A13 “是空的” B22 = SUM(D12:F12) B24 = SUM(D14:F14) B26 = SUM(D16:F16) D12 = SUM(D30:D40) E12 = IF(H9 =“是”; MAX(E18-(J18-J20)* SheetA!B2; SheetA!C2); MAX(E18-(J18-J20)* SheetA!F3; (D9 + F3)-IF(E9> 0; F59; 0)) F12 = F228 /(D458 + E58)*(D82 + E82) D14 = IF(H54> 0; D40/H40/I40/J40 * H64 * I64 * J64; 0) E14 = E40/H40 * H64 + E40/J40 *(J64-J40)* 0.F14 = F40 /(D40 + E40)*(D64 + E64) 等 – Hehabr

+0

公式中的Excel文件 : AND,FALSE,IF,IFERROR,MAX,OR,SUM,SUMPRODUCT,VLOOKUP – Hehabr

回答

0
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.DateUtil; 
import org.apache.poi.ss.util.CellReference; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

import java.io.*; 

public class POITestRB { 

    static String excelFileOrig = "C:/Test/1.xlsm"; 
    static String excelFileNew = "C:/Test/excelFileNew.xlsm"; 
    static FileInputStream fis; 
    static XSSFWorkbook workbook; 

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

     fis = new FileInputStream(excelFileOrig); 
     workbook = new XSSFWorkbook(fis); 
     gettingCellContents(workbook, "D5"); 
     updateCell(workbook, 10.0); 
     fis.close(); 
     workbook.close(); 

     fis = new FileInputStream(excelFileNew); 
     workbook = new XSSFWorkbook(fis); 
     gettingCellContents(workbook, "D5"); 
     fis.close(); 
     workbook.close(); 

    } 


    private static void gettingCellContents(XSSFWorkbook workbook, String cellId) { 

     workbook.setForceFormulaRecalculation(true); 

     XSSFSheet sheet = workbook.getSheetAt(1); 
     CellReference ref = new CellReference(cellId); 
     int row = ref.getRow(); 
     int col = ref.getCol(); 
     Cell cell = sheet.getRow(row).getCell(col); 

     switch (cell.getCellTypeEnum()) { 
      case STRING: 
       System.out.println(cell.getRichStringCellValue().getString()); 
       break; 
      case NUMERIC: 
       if (DateUtil.isCellDateFormatted(cell)) { 
        System.out.println(cell.getDateCellValue()); 
       } else { 
        System.out.println(cell.getNumericCellValue()); 
       } 
       break; 
      case BOOLEAN: 
       System.out.println(cell.getBooleanCellValue()); 
       break; 
      case FORMULA: 
       System.out.println("Formula is: " + cell.getCellFormula()); 
       System.out.println("cell.getCachedFormulaResultType(): " + cell.getCachedFormulaResultType()); 

       switch(cell.getCachedFormulaResultType()) { 
        case Cell.CELL_TYPE_NUMERIC: 
         System.out.println("0. case Cell.CELL_TYPE_NUMERIC --> Last evaluated as: " + cell.getNumericCellValue()); 
         break; 
        case Cell.CELL_TYPE_STRING: 
         System.out.println("4. case Cell.CELL_TYPE_STRING --> Last evaluated as \"" + cell.getRichStringCellValue() + "\""); 
         break; 
        case Cell.CELL_TYPE_ERROR: 
         System.out.println("5. case Cell.CELL_TYPE_ERROR --> "); 
         break; 
       } 
       break; 
      case BLANK: 
       System.out.println(); 
       break; 
      default: 
       System.out.println("default"); 
     } 
    } 

    public static void updateCell(XSSFWorkbook workbook, Double newData) { 
     try { 
      XSSFSheet sheet = workbook.getSheetAt(1); 
      CellReference ref = new CellReference("C8"); 
      int row = ref.getRow(); 
      int col = ref.getCol(); 
      Cell cell = sheet.getRow(row).getCell(col); 
      if (cell != null) { 
       cell.setCellValue(newData); 
      } 

      workbook.getCreationHelper().createFormulaEvaluator().clearAllCachedResultValues(); 
      workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); 

      OutputStream os = new FileOutputStream(excelFileNew); 
      workbook.write(os); 
      os.flush(); 
      os.close(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

// output: 
// Formula is: C34 
// cell.getCachedFormulaResultType(): 0 
// 0. case Cell.CELL_TYPE_NUMERIC --> Last evaluated as: 407.2521754511886 
// Formula is: C34 
// cell.getCachedFormulaResultType(): 5 
// 5. case Cell.CELL_TYPE_ERROR -->