首页 > 解决方案 > 无法使用 Jexcel API 读取单元格值

问题描述

我正在尝试通过 JExecel Api 读取 .xls 文件。我可以通过使用cell.getContents()轻松读取一些列数据。但是有些列给了我神秘的值 1,但列类型看起来像Date。所以我检查了那些列类型并发现它们是数字类型。所以我将那些单元格投射到NumberCell中。但是输出看起来仍然很神秘 1。

Workbook workbook = null;
            try {
                workbook = Workbook.getWorkbook("StandardReport.xls");
                Sheet sheet = workbook.getSheet(0);
                Cell[] cellRow = sheet.getColumn(0);
                for (int row = 4; row < cellRow.length; row++) {
                    String id = cellRow[row].getContents().trim();
                    if (id.isEmpty() == false) {
                        Cell cell1 = sheet.getCell(1, row);
                        Cell cell2 = sheet.getCell(2, row);
                        Cell cell3 = sheet.getCell(3, row);      

                        System.out.println(cell1.getContents()+"");
                        System.out.println(cell2.getContents()+"");
                        System.out.println(cell3.getContents()+"");

                        if (cell3.getType() == CellType.NUMBER) {                                 
                            NumberCell date=(NumberCell)cell3;                                
                            System.out.println(date.getContents()+"");
                        }


                        /*if (cell3.getType() == CellType.BOOLEAN) {
                            System.out.println("2");
                        }
                        if (cell3.getType() == CellType.BOOLEAN_FORMULA) {
                            System.out.println("3");
                        }
                        if (cell3.getType() == CellType.DATE_FORMULA) {
                            System.out.println("4");
                        }
                        if (cell3.getType() == CellType.EMPTY) {
                            System.out.println("5");
                        }
                        if (cell3.getType() == CellType.ERROR) {
                            System.out.println("6");
                        }
                        if (cell3.getType() == CellType.FORMULA_ERROR) {
                            System.out.println("7");
                        }
                        if (cell3.getType() == CellType.LABEL) {
                            System.out.println("8");
                        }
                        if (cell3.getType() == CellType.NUMBER) {
                            System.out.println("9");
                        }
                        if (cell3.getType() == CellType.NUMBER_FORMULA) {
                            System.out.println("11");
                        }
                        if (cell3.getType() == CellType.STRING_FORMULA) {
                            System.out.println("12");
                        }*/

                    }
                }
            } catch (IOException | BiffException ex) {
               StaticAccess.showMessageDialog("Failed!!", ex);
            }

这是StandardReport.xls的文件链接

标签: javajexcelapi

解决方案


推荐阅读