简体   繁体   English

Apache-poi。 XSSF。 工作簿

[英]Apache-poi. XSSF. Workbook

I already work with XLS, but today i'm trying new one - xlsx. 我已经在使用XLS,但是今天我正在尝试新的-xlsx。 For XLS I needed only one library, in contrast to XLSX (four libraries). 对于XLS,与XLSX(四个库)相比,我只需要一个库。 Also I get the whole package errors. 我也得到整个包装错误。 Why is this happening? 为什么会这样呢?

Main: 主要:

    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); }
}

The code: 编码:

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;   }

}

And massive of errors: 大量错误:

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\

The key part of that exception is: 该异常的关键部分是:

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

This means that you're running with too old a copy of XMLBeans. 这意味着您使用的XMLBeans副本过旧。 You'll need to upgrade to XMLBeans 2.3 or later. 您需要升级到XMLBeans 2.3或更高版本。 I think you're on 2.2, based on the error 我认为您使用的是2.2版本,基于错误

Also note that you'll need to remove the older XMLBeans jar before adding the new one. 还要注意,在添加新的XMLBeans jar之前,您需要删除它。 If you just drop in a new one, there's a high chance that the JVM will continue to use the old jar, so make sure to replace it! 如果您只使用一个新的jar,那么JVM很有可能会继续使用旧的jar,因此请确保将其替换!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM