java poi解析Excel2007+版本报错原因和解决办法

错误信息org.apache.poi.openxml4j.exceptions.InvalidFormatException

报错原因:
1、jar包冲突;
2、jar包版本过高;
我的原因就是jar包的版本过高,后引入poi3.11版本的jar包;
这里提供完整版本,其中包括poi所需的所有包
百度云下载链接:https://pan.baidu.com/s/1dx53t0Dks7FYVEqUEYq36Q
提取码:kwn3

实现代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.poi.ss.usermodel.Workbook;
public class ImportExcelUtil {

public static final Logger logger = LoggerFactory.getLogger(ImportExcelUtil.class);

// path,表头
public static List> readExcel(String path, int header) throws IOException {
	File file = new File(path);
	ArrayList> dataList = new ArrayList>();
	InputStream inputStream = new FileInputStream(file);
	try {
		Workbook book = createworkbook(inputStream);
		Sheet sheet = book.getSheetAt(0);
		Row row = null;
		Row rowHead = sheet.getRow(header);
		int length = rowHead.getLastCellNum();
		String[] head = new String[length];
		for (int i = rowHead.getFirstCellNum(); i < rowHead.getLastCellNum(); i++) {
			head[i] = getValueFromCell(rowHead.getCell(i));
		}
		for (int j = header + 1; j <= sheet.getLastRowNum(); j++) {
			row = sheet.getRow(j);
			Map map = new HashMap();
			for (int i = rowHead.getFirstCellNum(); i < rowHead.getLastCellNum(); i++) {
				map.put(head[i], getValueFromCell(row.getCell(i)));
			}
			dataList.add(map);
		}
	} catch (InvalidFormatException e) {
		e.printStackTrace();
	}

	return dataList;
}

public static Workbook createworkbook(InputStream inp) throws IOException, InvalidFormatException {
	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("你的excel版本目前poi解析不了");
}

public static final String getValueFromCell(Cell cell) {
	if (cell == null) {
		return "";
	}
	cell.setCellType(Cell.CELL_TYPE_STRING);
	String value = null;
	switch (cell.getCellType()) {
	case Cell.CELL_TYPE_NUMERIC: // 数字
		value = String.valueOf(cell.getNumericCellValue());
		break;
	case Cell.CELL_TYPE_STRING: // 字符串
		value = cell.getStringCellValue();
		break;
	case Cell.CELL_TYPE_FORMULA: // 公式
		// 用数字方式获取公式结果,根据值判断是否为日期类型
		double numericValue = cell.getNumericCellValue();
		value = String.valueOf(numericValue);
		break;
	case Cell.CELL_TYPE_BLANK: // 空白
		value = StringUtils.EMPTY;
		break;
	case Cell.CELL_TYPE_BOOLEAN: // Boolean
		value = String.valueOf(cell.getBooleanCellValue());
		break;
	case Cell.CELL_TYPE_ERROR: // Error,返回错误码
		value = String.valueOf(cell.getErrorCellValue());
		break;
	default:
		value = StringUtils.EMPTY;
		break;
	}
	return value;
}

}

通过createworkbook方法创建WorkBook,获取03和07版本不同的类型,解决其报InvalidFormatException的错误!
这个工具类实现的是将Excel文件解析后以表头为key,存储数据。数据格式是List>!

你可能感兴趣的:(java,poi解析Excel文档)