java - Apache POI : Updating Excel file

标签 java apache apache-poi

Apache POI:Excel 更新问题, 将新值写入单元格后: java.lang.IllegalStateException:无法从字符串单元格获取数字值

可能是什么问题?如何解决?

我使用 5 个文件,其中 3 个可以正常工作,另外 2 个则不能。 2 个错误文件的解决方法: Runtime.getRuntime().exec("cmd/c start "+ excel.getAbsolutePath());

    // ---------------------------------------------------------------------------

// Here: new values are written into the cells

workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
workbook.setForceFormulaRecalculation(true);

OutputStream output = new FileOutputStream(excel.getAbsolutePath());
workbook.write(output);
output.flush();
output.close();

// Here, new values are subtracted from the cells, 
after Excel resolves with new values 


// ---------------------------------------------------------------------------

// Value in bad Cell: =B24

java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
    at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1050)
    at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:310)
    at quicc.excel.api.ExcelHandlerXSSF.handleCell(ExcelHandlerXSSF.java:275)
    at quicc.excel.api.ExcelHandlerXSSF.readCell(ExcelHandlerXSSF.java:251)

最佳答案

package poi.service;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class POITestEva {

    private String excelFilePath = "C:/Test/1.xlsm";
    private FileInputStream inputStream;
    private XSSFWorkbook workbook;

    public static void main(String[] args) {
        POITestEva pOITestEva = new POITestEva();
        pOITestEva.updateCell(3.0);
        System.out.println("D5 = " + pOITestEva.readCellTest("D5"));        // Line 23
    }


    public void updateCell(Double newData) {
        try {
            File excel = new File(excelFilePath);
            inputStream = new FileInputStream(excel);
            workbook = new XSSFWorkbook(inputStream);
            workbook.setForceFormulaRecalculation(true);

            Cell cell = getCell(1, "C8");
            if (cell != null) {
                cell.setCellValue(newData);
            }

            workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
            OutputStream output = new FileOutputStream(excel);
            workbook.write(output);
            output.flush();
            output.close();
            workbook.close();
            inputStream.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    private Cell getCell(int sheetNr, String cellId) {
        CellReference ref = new CellReference(cellId);
        return getCell(sheetNr, ref.getCol(), ref.getRow());
    }

    private Cell getCell(int sheetNr, int col, int row) {
        XSSFSheet sheet = workbook.getSheetAt(sheetNr);
        if (sheet.getRow(row) != null
                && sheet.getRow(row).getCell(col) != null
                && !(sheet.getRow(row).getCell(col).getCellType() == Cell.CELL_TYPE_BLANK)) {
            return sheet.getRow(row).getCell(col);
        }
        return null;
    }


    public Double readCellTest(String cellId) {
        try {
            File excel = new File(excelFilePath);
            inputStream = new FileInputStream(excel);
            workbook = new XSSFWorkbook(inputStream);
            Double result = ( (Double) (readCell(cellId)) );            // Line 74
            if (workbook != null) {
                workbook.close();
            }
            if (inputStream != null) {
                workbook.close();
            }
            return result;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private Object readCell(String cellId) {
        Cell cell = getCell(1, cellId);
        return handleCell(cell.getCellType(), cell);                    // Line 91
    }


    @SuppressWarnings("deprecation")
    private Object handleCell(int type, Cell cell) {
        switch (type) {
            case XSSFCell.CELL_TYPE_STRING:
                return cell.getStringCellValue();
            case XSSFCell.CELL_TYPE_NUMERIC:
                return cell.getNumericCellValue();
            case XSSFCell.CELL_TYPE_BOOLEAN:
                return cell.getBooleanCellValue();
            case XSSFCell.CELL_TYPE_BLANK:
                return null;
            case XSSFCell.CELL_TYPE_ERROR:
                return null;
            case XSSFCell.CELL_TYPE_FORMULA:
                return cell.getNumericCellValue();                  // Line 109
            default:
                return null;
        }
    }

}

关于java - Apache POI : Updating Excel file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44998231/

相关文章:

java - 有没有一种简单的方法可以使用 JavaFX 创建多个 TextField?

java - 为什么第一次交换尝试有效,但第二次无效?

java - 如何处理 Sonarlint java :S2259 (Null pointers should not be dereferenced)

java - 如何在 Java 的 POI 中使用 XWPFTable 合并单元格(或应用 colspan)?

java - Maven 使用 Jetty 运行多模块应用程序

php - Drupal 7 全新安装显示 session 错误

java - Apache 错误: "FAIL - Application at context path/tibclient could not be started"

apache - 我的etc文件夹下没有apache2文件夹,很困惑?等/apache2?

java - Solr 中缺少强制 uniquekey 字段错误

java - Apache POI 公式单元格复制非常慢