博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java读取Excel文件(支持xls,xlsx,多sheet)
阅读量:4161 次
发布时间:2019-05-26

本文共 5518 字,大约阅读时间需要 18 分钟。

文章目录


Java读取Excel文件(支持xls,xlsx,多sheet)

1. pom.xml依赖

org.apache.poi
poi
4.0.1
org.apache.poi
poi-ooxml
4.0.1
org.apache.poi
poi-ooxml-schemas
4.0.1

2. 工具类封装

public class ExcelReadUtil {
private static Logger logger = LoggerFactory.getLogger(ExcelReadUtil.class); public static HashMap
>> 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
>> readExcelForXlsx(File file, int ignoreRow) {
HashMap
>> 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
> 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; } ArrayList
list = new ArrayList<>(); int col = 0; for (int colIndex = 0; colIndex <= row.getLastCellNum(); colIndex++) { cell = row.getCell(colIndex); String value = ""; if (cell != null) { CellType cellType = cell.getCellType(); switch (cellType) { case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { value = String.valueOf(cell.getDateCellValue()); } else { value = String.valueOf(new DecimalFormat("0").format(cell.getNumericCellValue())); } break; case STRING: value = String.valueOf(cell.getStringCellValue()); break; case FORMULA: value = String.valueOf(cell.getCellFormula()); break; case BLANK: value = ""; break; case BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case ERROR: value = String.valueOf(cell.getErrorCellValue()); break; default: value = ""; } if (StringUtils.isNotBlank(value)) { list.add(value); } else { col++; } } } if (col == row.getRowNum()) { continue; } if (list.size() > 0) { lists.add(list); } } map.put("sheet" + sheetIndex, lists); } } catch (Exception e) { e.printStackTrace(); } return map; } /** * 读取excel xls后缀名文件 * * @param file * @param ignoreRow * @return */ private static HashMap
>> readExcelForXls(File file, int ignoreRow) { HashMap
>> map = new HashMap<>(); if (!file.exists()) { logger.error("{}文件不存在", file.getName()); return null; } int rowSize = 0; try { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); HSSFWorkbook workbook = new HSSFWorkbook(bufferedInputStream); HSSFCell cell = null; for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) { HSSFSheet sheet = workbook.getSheetAt(sheetIndex); ArrayList
> lists = new ArrayList<>(); for (int rowIndex = ignoreRow; rowIndex < sheet.getLastRowNum(); rowIndex++) { HSSFRow row = sheet.getRow(rowIndex); if (null == row) { continue; } int tempRowSize = row.getLastCellNum() + 1; if (tempRowSize > rowSize) { rowSize = tempRowSize; } ArrayList
list = new ArrayList<>(); int col = 0; for (int colIndex = 0; colIndex < row.getLastCellNum(); colIndex++) { cell = row.getCell(colIndex); String value = ""; if (cell != null) { CellType cellType = cell.getCellType(); switch (cellType) { case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { value = String.valueOf(cell.getDateCellValue()); } else { value = String.valueOf(new DecimalFormat("0").format(cell.getNumericCellValue())); } break; case STRING: value = String.valueOf(cell.getStringCellValue()); break; case FORMULA: value = String.valueOf(cell.getCellFormula()); break; case BLANK: value = ""; break; case BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case ERROR: value = String.valueOf(cell.getErrorCellValue()); break; default: value = ""; } if (StringUtils.isNotBlank(value)) { list.add(value); } else { col++; } } } if (col == row.getRowNum()) { continue; } if (list.size() > 0) { lists.add(list); } } map.put("sheet" + sheetIndex, lists); } } catch (Exception e) { e.printStackTrace(); } return map; }}

3. 使用示例说明

@Testpublic void testExcelRead(){
HashMap
>> excelReadMap = ExcelReadUtil.readExcel(new File(excelFilePath), 1); if(excelReadMap != null){
excelReadMap.entrySet().stream().forEach(entry -> {
entry.getValue().stream().forEach(col -> {
col.stream().forEach(System.out::println); }); }); } }

转载地址:http://rzixi.baihongyu.com/

你可能感兴趣的文章
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
java杂记
查看>>
RunTime.getRuntime().exec()
查看>>
Oracle 分组排序函数
查看>>
VMware Workstation 14中文破解版下载(附密钥)(笔记)
查看>>
日志框架学习
查看>>
日志框架学习2
查看>>
SVN-无法查看log,提示Want to go offline,时间显示1970问题,error主要是 url中 有一层的中文进行了2次encode
查看>>
DeepLearning tutorial(7)深度学习框架Keras的使用-进阶
查看>>
第三方SDK:JPush SDK Eclipse
查看>>
第三方开源库:imageLoader的使用
查看>>
Android studio_迁移Eclipse项目到Android studio
查看>>