将Excel文件表导入到java时出错
我想创build一个双数组中的值从两列在Java中,它不认可我的工作表。
我认为我的path名称可能是问题,除了这是从目录复制和粘贴。 (除了额外的斜线当然)。 任何在这个方向的指针都会有所帮助。
这是我的代码:
“XSSFWorkbook workbook = new XSSFWorkbook(fis)”引发exception。
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
这个exceptionjava.io.CharConversionException: Characters larger than 4 bytes are not supported: byte 0xa6 implies a length of more than 4 bytes
(在apache-poi的情况下)当你尝试读取一个.xlsb格式(这不是支持的)。
你确定你的Excel是xlsx格式,而不是xlsb格式吗?
请参阅以下链接,了解类似的例外情况,但仅限于xlsb格式: http ://comments.gmane.org/gmane.comp.jakarta.poi.user/12151
读取XLSB文件的exceptionApache POI java.io.CharConversionException