如何使用poi

时间:2017-01-08 18:02:04

标签: java apache-poi vlookup

我有一个带有合并和公式单元格的excel文件,它正在使用VLOOKUP(B16,Data!$ A $ 2:$ C $ 7,3,0)。在特定的单元格中,40美元显示为价值。但是当我尝试读取它的值时,它只读取40并跳过$ part。

public static void main(String[] args) {

    try {

        String excelFilePath = "D:\\test\\1_sample1_UA Logo Order form update on 2016-12-06.xls";

        InputStream input = new BufferedInputStream(new FileInputStream(
                excelFilePath));
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        workbook.setForceFormulaRecalculation(true);

        Sheet sheet = workbook.getSheetAt(3);
        Row row = sheet.getRow(15);
        Cell cell = row.getCell(7);

        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress region = sheet.getMergedRegion(i); // Region of
                                                                // merged
                                                                // cells

            int colIndex = region.getFirstColumn();
            int rowNum = region.getFirstRow();

            if (region.isInRange(15, 7)) {

                CellReference cellReference = new CellReference("H15");
                Row row2 = sheet.getRow(cellReference.getRow());
                Cell cell2 = row2.getCell(cellReference.getCol());

                // formulla evaluato
                FormulaEvaluator forEvaluator = workbook
                        .getCreationHelper().createFormulaEvaluator();
                workbook.getCreationHelper().createFormulaEvaluator()
                        .evaluateAll();
                workbook.setForceFormulaRecalculation(true);

                CellValue cellValue = forEvaluator.evaluate(cell2);

                System.out.println("cell string value2 :: "
                        + cellValue.formatAsString());

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案