Apache Workbook XSSF获取的单元格值格式错误

时间:2017-01-07 17:51:22

标签: java excel apache xssf

我创建了一个包含数据的excel(xlsx)文件。

Map<String, List<Object>> dataXlsx = new HashMap<>();
Workbook workBook = new WorkBook();
 final XSSFFont blackFont = (XSSFFont) workBook.createFont();
        blackFont.setFontHeightInPoints((short) 11);
        blackFont.setFontName(CALIBRI);
        blackFont.setBold(true);
        final XSSFCellStyle blackCellStyle = (XSSFCellStyle) workBook.createCellStyle();
        blackCellStyle.setFont(blackFont);

        final XSSFFont redFont = (XSSFFont) workBook.createFont();
        redFont.setFontHeightInPoints((short) 11);
        redFont.setFontName(CALIBRI);
        redFont.setBold(true);
        redFont.setColor(IndexedColors.RED.getIndex());
        final XSSFCellStyle redCellStyle = (XSSFCellStyle) workBook.createCellStyle();
        redCellStyle.setFont(redFont);
        redCellStyle.setLocked(true);

        final XSSFFont blueFont = (XSSFFont) workBook.createFont();
        blueFont.setFontHeightInPoints((short) 11);
        blueFont.setFontName(CALIBRI);
        blueFont.setBold(true);
        blueFont.setColor(IndexedColors.BLUE.getIndex());
        final XSSFCellStyle blueCellStyle = (XSSFCellStyle) workBook.createCellStyle();
        blueCellStyle.setFont(blueFont);

        final XSSFSheet newSheet = (XSSFSheet) workBook.createSheet();
        final Set<String> rows = dataXlsx.keySet();

        for (final String key : rows) {
            ...
            }

        }

我想读它。 当我在XLSX中写道时,例如3453453453我得到3.453453453E。 https://i.stack.imgur.com/cmTgj.png

final Iterator<Row> rowIterator = sheet.iterator();
        for (int i = 0; rowIterator.hasNext(); i++) {
            final Row row = rowIterator.next();
            final Iterator<Cell> cellIterator = row.cellIterator();
            for (int k = 0; cellIterator.hasNext(); k++) {
                final Cell cell = cellIterator.next();

                String cellValue = cell.toString(); //set 3453453453, but get 3.45345345E
                ...

如何设置所有单元格的通用格式? 可能有其他人有解决方案吗?

最好的问候。

我在XLSX文件中写入了值,例如'some string'(第一列,第一个单元格),3453453453(第二列,第一个单元格)并将其保存在光盘上。 然后我使用WorkBook API在Java代码中读取此文件。当我拿第一个单元格时,获得“一些字符串”值。当我拿第二个单元格时,我得到3.453453453E值(格式错误)。我希望这有帮助。 最好的问候。

0 个答案:

没有答案