<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
public class ExcelReadUtil {
?
private static Logger logger = LoggerFactory.getLogger(ExcelReadUtil.class);
?
?
?
public static HashMap<String, ArrayList<ArrayList<String>>> readExcel(File file, int ignoreRow) {
if (file.getName().toLowerCase().endsWith(".xlsx")) {
return readExcelForXlsx(file, ignoreRow);
} else if (file.getName().toLowerCase().endsWith(".xls")) {
return readExcelForXls(file, ignoreRow);
}
return null;
}
/**
* 读取Excel xlsx后缀名文件数据
*
* @param file
*/
private static HashMap<String, ArrayList<ArrayList<String>>> readExcelForXlsx(File file, int ignoreRow) {
HashMap<String, ArrayList<ArrayList<String>>> map = new HashMap<>();
if (!file.exists()) {
logger.error("{}文件不存在", file.getName());
return null;
}
int rowSize = 0;
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook(in);
} catch (IOException e) {
e.printStackTrace();
}
XSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
XSSFSheet sheet = workbook.getSheetAt(sheetIndex);
?
?
ArrayList<ArrayList<String>> lists = new ArrayList<>();
for (int rowIndex = ignoreRow; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
XSSFRow row = sheet.getRow(rowIndex);
if (null == row) {
continue;
}
?
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize;
}