avenger19851224 2018-07-10 09:52 采纳率: 100%
浏览 20523
已采纳

java解析Excel,The supplied file was empty

利用poi包解析excel文件的时候,出现以下错误
org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:74)
at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:57)
at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:135)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:175)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:209)
at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:98)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:295)
at com.zzidc.tax.bookkeeping.util.XlsUtils.readExcel(XlsUtils.java:53)
at com.zzidc.tax.bookkeeping.service.GenerateReportService.getRoleForExcel(GenerateReportService.java:254)
at com.zzidc.tax.bookkeeping.service.GenerateReportService.analysisImage(GenerateReportService.java:110)
at com.zzidc.tax.rabbitmq.CustomerChoose.analy(CustomerChoose.java:188)
at com.zzidc.tax.rabbitmq.CustomerChoose.processAndSaveDeclareImg(CustomerChoose.java:169)
at com.zzidc.tax.rabbitmq.CustomerChoose.access$2(CustomerChoose.java:155)
at com.zzidc.tax.rabbitmq.CustomerChoose$1.handleDelivery(CustomerChoose.java:80)
at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:144)
at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:99)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

我的代码如下:
//读取excel
public static Workbook readExcel(String filePath){
Workbook wb = null;
InputStream is = null;
URLConnection conn = null;
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
try {
URL url = new URL(filePath);
conn = url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3 * 60 * 1000);
is = conn.getInputStream();
wb = StreamingReader.builder()
.rowCacheSize(100)
.bufferSize(1024 * 1024 * 5)
.open(is);
if(".xls".equals(extString)){
wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
wb = new XSSFWorkbook(is);
}else{
wb = null;
}
} catch (Exception e) {
logger.error("Excel工具读取文件发生异常,异常: " + e);
logger.error("文件: " + filePath);
e.printStackTrace();
return null;
}finally{
if (is != null) {

try {
is.close();
} catch (IOException e) {
}

}

}
return wb;
}
请问各位大手,这个问题是怎么产生的,该怎么解决呢,谢谢了

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-07-10 16:08
    关注

    你确认你的文件是excel格式的么?不要以为excel能打开,并且拥有xls/xlsx后缀的文件都是excel格式的,一些程序生成的根本就是文本格式、html、csv等等,也用了xls后缀。
    这些文件excel能打开,但是poi是不能解析的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00