最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用Apache POI获取excel单元格显示值

SEO心得admin50浏览0评论
本文介绍了使用Apache POI获取excel单元格显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面显示突出显示的单元格,它显示为 -(连字符),但实际上里面有 0 值.

这是一个具有以下自定义格式的公式单元格.

这是一个公式单元格,但当结果为0时,它显示为-.

我的要求是获取 - 而不是 0.

cell2Update = sheet.getRow(4).getCell(3);cell2Update.setCellType(Cell.CELL_TYPE_NUMERIC);System.out.println(cell2Update.getStringCellValue());//异常System.out.println(cell2Update.getNumericCellValue());//显示为0

请帮助我解决这个疑问.我想显示显示在电子表格 (-) 中的值.

解决方案

使用 DataFormatter 如获取单元格内容.并且因为该值可能是公式的结果.使用 DataFormatter 和FormulaEvaluator:

...DataFormatter formatter = new DataFormatter();FormulaEvaluator 评估器 = workbook.getCreationHelper().createFormulaEvaluator();...cell2Update = sheet.getRow(4).getCell(3);...String value = formatter.formatCellValue(cell2Update, evaluator);System.out.println(value);...

Below shows the highlighted cell which display as - (hyphen) but actually has 0 value inside it.

It's a formula cell with following custom formatting.

It's a formula cell but when the result is 0, it displays as -.

My requirement is to fetch - instead of 0.

cell2Update = sheet.getRow(4).getCell(3); cell2Update.setCellType(Cell.CELL_TYPE_NUMERIC); System.out.println(cell2Update.getStringCellValue());//Exception System.out.println(cell2Update.getNumericCellValue());//Display as 0

Please help me with this doubt. I want to display the value which is displayed in Spreadsheet (-).

解决方案

Do using a DataFormatter as shown in Getting the cell contents. And because the value might be result of a formula. Do using using a DataFormatter together with a FormulaEvaluator:

... DataFormatter formatter = new DataFormatter(); FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); ... cell2Update = sheet.getRow(4).getCell(3); ... String value = formatter.formatCellValue(cell2Update, evaluator); System.out.println(value); ...

发布评论

评论列表(0)

  1. 暂无评论