Apache的POI。 XSSF。 工作簿

我已经与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\ 

该例外的关键部分是:

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

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

另外请注意,在添加新的XMLBean jar之前,您需要删除旧的XMLBean jar。 如果您只是放入一个新的JVM,那么JVM很有可能会继续使用旧的jar,所以一定要把它replace掉!