POI 3.9 Excel 읽기

5483 단어 poi3.9
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.

좋은 웹페이지 즐겨찾기