0

我已经使用 XLS,但今天我正在尝试新的 - xlsx。对于 XLS,我只需要一个库,而 XLSX(四个库)。我也得到了整个包错误。为什么会这样?

主要的:

    public class Start {
    public static void main(String[] args) {
        ReaderXls read = new ReaderXls();
        List<List<String>>  listAsu = new ArrayList<List<String>>(read.ReaderXls("Sprav_oborudovania",0, 10, 30));
        System.out.println(listAsu); }
}

编码:

package workhere;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReaderXls {
    public List<List<String>> ReaderXls(String sfilename,int firstColumn, int columnsCount, int rowsCount){
        int lastColumn=firstColumn+columnsCount;
        List<List<String>> rowsContent = new ArrayList<List<String>>();
        try ( FileInputStream fileInputStream = new FileInputStream("C:\\Users\\student3\\"+sfilename+".xlsx");)
        {//Get the workbook instance for XLS file
            XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream);
            XSSFSheet sheet = workBook.getSheetAt(0);                                                                   //Get first sheet from the workbook
            toNewLine:
            for (int lineId=0;lineId<rowsCount;lineId++)            {
                List<String> columnsContent = new ArrayList<String>();
                Row row = sheet.getRow(lineId);
                try {
                    for (int columnId=firstColumn;columnId<lastColumn;columnId++)     {
                        Cell cell = row.getCell(columnId);
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        if (cell.getStringCellValue().equals("")) {}
                        else columnsContent.add(cell.getStringCellValue());
                    }
                }
                catch (NullPointerException e) {
                    e.printStackTrace();
                }
                if (columnsContent.isEmpty()) {
                    continue toNewLine;
                }
                rowsContent.add(columnsContent);
            }
            FileOutputStream out =
                    new FileOutputStream("C:\\Users\\student3\\"+sfilename+".xls");
            workBook.write(out);
            out.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();                 }
        catch (IOException e) {
            e.printStackTrace();                 
        } 
        return rowsContent;   }

}

和大量的错误:

Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:207)
    at workhere.ReaderXls.ReaderXls(ReaderXls.java:26)
    at workhere.Start.main(Start.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
    ... 10 more
Caused by: java.lang.ExceptionInInitializerError
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
    at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:140)
    at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1057)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:1038)
    at java.lang.reflect.Field.get(Field.java:379)
    at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:770)
    at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument.<clinit>(Unknown Source)
    at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:44)
    ... 15 more
Caused by: java.lang.RuntimeException: Could not instantiate SchemaTypeSystemImpl (java.lang.reflect.InvocationTargetException): is the version of xbean.jar correct?
    at schemaorg_apache_xmlbeans.system.sE130CAA0A01A7CDE5A2B4FEB8B311707.TypeSystemHolder.loadTypeSystem(Unknown Source)
    at schemaorg_apache_xmlbeans.system.sE130CAA0A01A7CDE5A2B4FEB8B311707.TypeSystemHolder.<clinit>(Unknown Source)
    ... 25 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    ... 27 more
Caused by: org.apache.xmlbeans.SchemaTypeLoaderException: XML-BEANS compiled schema: Incompatible minor version - expecting up to 23, got 24 (schemaorg_apache_xmlbeans.system.sE130CAA0A01A7CDE5A2B4FEB8B311707.index) - code 3
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.<init>(SchemaTypeSystemImpl.java:1522)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(SchemaTypeSystemImpl.java:260)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.<init>(SchemaTypeSystemImpl.java:183)
    ... 31 more\
4

1 回答 1

7

该异常的关键部分是:

XML-BEANS compiled schema: Incompatible minor version - expecting up to 23, got 24 

这意味着您运行的 XMLBeans 副本太旧了。您需要升级到 XMLBeans 2.3 或更高版本。根据错误,我认为您使用的是 2.2

另请注意,在添加新的 XMLBeans jar 之前,您需要删除旧的 XMLBeans jar。如果你只是放入一个新的,JVM 很有可能会继续使用旧的 jar,所以一定要更换它!

于 2013-09-02T12:00:28.667 回答