POI 解析Excel

The supplied data appears to be in the Office 2007+ XML. 
You are calling the part of POI that deals with OLE2 Office Documents. 
You need to call a different part of POI to process this data (eg XSSF instead of HSSF).
// 1.  2007及以上版本使用
Workbook  workbook1 = new XSSFWorkbook(inputStream);     
// 2 . 2003及以下版本使用 
Workbook  workbook2 = new HSSFWorkbook(inputStream);     
// 3.  不区分版本
Workbook wb = WorkbookFactory.create(inputStream);
方式 3 实现了根据版本去选择相应的解析类:
	    /**
    	 * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
    	 *  the given InputStream.
    	 * Your input stream MUST either support mark/reset, or
    	 *  be wrapped as a {@link PushbackInputStream}!
    	 */
    	public static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
    		// If clearly doesn't do mark/reset, wrap up
    		if(! inp.markSupported()) {
    			inp = new PushbackInputStream(inp, 8);
    		}
    		
    		if(POIFSFileSystem.hasPOIFSHeader(inp)) {
    			return new HSSFWorkbook(inp);
    		}
    		if(POIXMLDocument.hasOOXMLHeader(inp)) {
    			return new XSSFWorkbook(OPCPackage.open(inp));
    		}
    		throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
    	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值