jxls1.0.6生成excel公式不能自動計算 原

首先 爲什麼這個時間還用1.0.6 因爲用了2.4以上 jdk6運行起來很難受 嗯 然後之前用的好好的 那個公式突然 還要點進去再按回車纔會生效 那不給客戶打死 然後就找博客 copy了一份通用的方法 

/**
     * 
     * 重新設置單元格計算公式20180615
     * 
     * */
    public static void resetCellFormula(HSSFWorkbook wb)
    {
        HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(wb);
        int sheetNum = wb.getNumberOfSheets();
        for (int i = 0; i < sheetNum; i++)
        {
            HSSFSheet sheet = wb.getSheetAt(i);
            int rows = sheet.getLastRowNum() + 1;
            for (int j = 0; j < rows; j++)
            {
                HSSFRow row = sheet.getRow(j);
                if (row == null)
                    continue;
                int cols = row.getLastCellNum();
                for (int k = 0; k < cols; k++)
                {
                    HSSFCell cell = row.getCell(k);
                    // if (cell != null)
                    // System.out.println("cell["+j+","+k+"]=:"+cell.getCellType());
                    if (cell == null)
                        continue;
                    if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
                    {
                        cell.setCellFormula(cell.getCellFormula());
                        // System.out.println("----公式:"+cell.getCellFormula());
                        cell = e.evaluateInCell(cell);
                        // System.out.println("-----------"+cell.getNumericCellValue());
                    }
                }
            }
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章