异常处理:Package should contain a content type part [M1.13]

public static Workbook create(InputStream in) throws     
    IOException,InvalidFormatException {
        if (!in.markSupported()) {
            in = new PushbackInputStream(in, 8);
        }
        if (POIFSFileSystem.hasPOIFSHeader(in)) {
            return new HSSFWorkbook(in);
        }
        if (POIXMLDocument.hasOOXMLHeader(in)) {
            return new XSSFWorkbook(OPCPackage.open(in));
        }
        throw new IllegalArgumentException("你的excel版本目前poi解析不了");
    }

读写xls和xlsx格式时,需要分不同的类

  • 针对.xls后缀时:使用HSSFWorkbook,
  • 针对.xlsx后缀时:使用XSSFWorkbook

换一下处理类就ok了

你可能感兴趣的:(java,开发语言)