`
阅读更多

POI 读Excel非常简单,例子如下:

 

    private Map<String, Object> readExcel(File excel) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", false);
        FileInputStream fis = null;
        Workbook wb = null;
        int saveCount = 0;
        try {
            try {
                fis = new FileInputStream(excel);
                wb = WorkbookFactory.create(fis);
                for (int s = 0; s < wb.getNumberOfSheets(); s++) {
                    Sheet sheet = wb.getSheetAt(s);
                    int rowNum = sheet.getPhysicalNumberOfRows();
                    if (rowNum < 1) {
                        result.put("msg", "导入文件中没有数据");
                        return result;
                    }
                    for (int r = 0; r < rowNum; r++) {
                        Row row = sheet.getRow(r);
                        if (row == null) {
                            continue;
                        }
                        int cells = row.getPhysicalNumberOfCells();
                        for (int c = 0; c < cells; c++) {
                            Cell cell = row.getCell(c);
                            String value = null;
                            switch (cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_FORMULA:
                                value = cell.getCellFormula();
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                value = cell.getNumericCellValue() + "";
                                if (value.endsWith(".0")) {
                                    value = value.substring(0, value.length() - 2);
                                }
                                break;
                            case HSSFCell.CELL_TYPE_STRING:
                                value = cell.getStringCellValue();
                                break;
                            default:
                            }
                            System.out.println(value);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result.put("msg", "导入[" + saveCount + "]条数据");
        result.put("success", true);
        return result;
    }
 

 

关于问题:

"java.lang.NoClassDefFoundError: org/openxmlformats/schemas/*something*"

比如:org.openxmlformats.schemas.wordprocessingml.x2006

官网上有说明

 

I'm using the poi-ooxml-schemas jar, but my code is failing with "java.lang.NoClassDefFoundError: org/openxmlformats/schemas/*something*"

To use the new OOXML file formats, POI requires a jar containing the file format XSDs, as compiled by XMLBeans. These XSDs, once compiled into Java classes, live in the org.openxmlformats.schemas namespace.

There are two jar files available, as described in the components overview section. The full jar of all of the schemas is ooxml-schemas-1.1.jar, and it is currently around 15mb. The smaller poi-ooxml-schemas jar is only about 4mb. This latter jar file only contains the typically used parts though.

Many users choose to use the smaller poi-ooxml-schemas jar to save space. However, the poi-ooxml-schemas jar only contains the XSDs and classes that are typically used, as identified by the unit tests. Every so often, you may try to use part of the file format which isn't included in the minimal poi-ooxml-schemas jar. In this case, you should switch to the full ooxml-schemas-1.1.jar. Longer term, you may also wish to submit a new unit test which uses the extra parts of the XSDs, so that a future poi-ooxml-schemas jar will include them.

There are a number of ways to get the full ooxml-schemas-1.1.jar. If you are a maven user, see the the components overview section for the artifact details to have maven download it for you. If you download the source release of POI, and/or checkout the source code from subversion, then you can run the ant task "compile-ooxml-xsds" to have the OOXML schemas downloaded and compiled for you (This will also give you the XMLBeans generated source code, in case you wish to look at this). Finally, you can download the jar by hand from the POI Maven Repository.

Note that for POI 3.5 and 3.6, the full ooxml schemas jar was named ooxml-schemas-1.0.jar. For POI 3.7, the filename was bumped to ooxml-schemas-1.1.jar when generics support was added. You can use ooxml-schemas-1.1.jar with POI 3.5 and 3.6 if you wish, but POI 3.7 won't wokr with ooxml-schemas-1.0.jar (it needs thew newer one).

 

 

简单来说官方lib中的poi-ooxml-schemas-3.9-20121203是一个缩水的版本,只提供常用的一些类。所以解决的办法就是导入ooxml-schemas-1.1.jar而不是poi-ooxml-schemas-3.9-20121203

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics