将Excel文件表导入java时出错

时间:2015-07-08 15:06:56

标签: java excel apache-poi

我正在尝试使用java中两列的值创建一个double数组,但它无法识别我的工作表。

我认为我的路径名可能是问题,除了从目录中复制和粘贴它。 (当然除了额外的斜线)。这方面的任何指示都会有所帮助。

这是我的代码:

“XSSFWorkbook工作簿=新的XSSFWorkbook(fis)”这句话引发了一个例外;

package adina.twitter;

import java.io.IOException;
import java.util.Iterator;
import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import  java.lang.Iterable;

public class ReadFile {

    public static void main(String[] args) throws IOException {

        final String FILE_PATH = "C:\\Users\\silbeab1\\Documents\\seedsActivity12Junv2.xlsx";
        String fileSheet = "screen_name-user_id";

        String[][] snId = null;

        try{
            FileInputStream fis = new FileInputStream(new File(FILE_PATH));
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet worksheet = workbook.getSheet(fileSheet);

            boolean flag = true;
            System.out.println(flag);
            for (int x = 0; flag == true; x++){
                for (int y = 0; y < 2; y++){
                    String test = worksheet.getRow(x).getCell(y).toString();
                    if (test != null){
                        snId[x][y] = test;
                    }
                    else{
                        flag = false;
                    }
                }
            }           
        }
        catch (Exception e){
            System.out.printf("Cannot open %s \n error: %s",FILE_PATH,e);
        }       
    }   
}

这是我的输出: 无法打开C:\ Users \ silbeab1 \ Documents \ seedsActivity12Junv2.xlsx  错误:org.apache.poi.POIXMLException:java.lang.reflect.InvocationTargetException

这是完整的堆栈跟踪:

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:456)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:162)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:271)
    at adina.twitter.ReadFile.main(ReadFile.java:29)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
    ... 4 more
Caused by: java.io.CharConversionException: Characters larger than 4 bytes are not supported: byte 0xa6 implies a length of more than 4 bytes
    at org.apache.xmlbeans.impl.piccolo.xml.UTF8XMLDecoder.decode(UTF8XMLDecoder.java:162)
    at org.apache.xmlbeans.impl.piccolo.xml.XMLStreamReader$FastStreamDecoder.read(XMLStreamReader.java:762)
    at org.apache.xmlbeans.impl.piccolo.xml.XMLStreamReader.read(XMLStreamReader.java:162)
    at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.yy_refill(PiccoloLexer.java:3477)
    at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.yylex(PiccoloLexer.java:3962)
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yylex(Piccolo.java:1290)
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yyparse(Piccolo.java:1400)
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:714)
    at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3479)
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1277)
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1264)
    at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.SstDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xssf.model.SharedStringsTable.readFrom(SharedStringsTable.java:119)
    at org.apache.poi.xssf.model.SharedStringsTable.<init>(SharedStringsTable.java:106)
    ... 9 more

1 个答案:

答案 0 :(得分:3)

当您尝试读取.xlsb格式(不支持)时,此异常java.io.CharConversionException: Characters larger than 4 bytes are not supported: byte 0xa6 implies a length of more than 4 bytes(如果是apache-poi)主要出现。

您确定您的Excel是xlsx格式而不是xlsb格式吗?

请参阅以下类似例外的链接,但仅限xlsb格式: http://comments.gmane.org/gmane.comp.jakarta.poi.user/12151

Exception reading XLSB File Apache POI java.io.CharConversionException