| | |
| | | import java.util.List; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.esotericsoftware.minlog.Log; |
| | | import org.apache.poi.hssf.usermodel.HSSFDataFormat; |
| | | import org.apache.poi.hssf.usermodel.HSSFDateUtil; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | |
| | | throw new Exception("创建Excel工作薄为空!"); |
| | | } |
| | | List<List<List<Object>>> returnList = new ArrayList<List<List<Object>>>(); |
| | | for (Sheet sheet : work) { |
| | | Row row = null; |
| | | Cell cell = null; |
| | | try { |
| | | for (Sheet sheet : work) { |
| | | Row row = null; |
| | | Cell cell = null; |
| | | |
| | | list = new ArrayList<List<Object>>(); |
| | | if (sheet == null) { |
| | | continue; |
| | | } |
| | | // 取得Excel的总列数,总行数 |
| | | int columns = 0; |
| | | Row firstRow = sheet.getRow((short) 0); |
| | | if (firstRow != null) { |
| | | columns = firstRow.getPhysicalNumberOfCells(); |
| | | } |
| | | list = new ArrayList<List<Object>>(); |
| | | if (sheet == null) { |
| | | continue; |
| | | } |
| | | // 取得Excel的总列数,总行数 |
| | | int columns = 0; |
| | | Row firstRow = sheet.getRow((short) 0); |
| | | if (firstRow != null) { |
| | | columns = firstRow.getPhysicalNumberOfCells(); |
| | | } |
| | | |
| | | int rows = sheet.getPhysicalNumberOfRows(); |
| | | List<Object> dataRow = null; |
| | | int rows = sheet.getPhysicalNumberOfRows(); |
| | | List<Object> dataRow = null; |
| | | |
| | | // 首行为定义标题的行,数据从第2行开始 |
| | | for (int i = 1; i < rows; i++) { |
| | | dataRow = new ArrayList<Object>(); |
| | | // 获取行 |
| | | row = sheet.getRow(i); |
| | | if (row == null) { |
| | | break; |
| | | } |
| | | // 首行为定义标题的行,数据从第2行开始 |
| | | for (int i = 1; i < rows; i++) { |
| | | dataRow = new ArrayList<Object>(); |
| | | // 获取行 |
| | | row = sheet.getRow(i); |
| | | if (row == null) { |
| | | break; |
| | | } |
| | | |
| | | cell = row.getCell(0); |
| | | if (StrUtil.isBlank(getCellValue(cell).toString())) { |
| | | break; |
| | | } |
| | | cell = row.getCell(0); |
| | | if (StrUtil.isBlank(getCellValue(cell).toString())) { |
| | | break; |
| | | } |
| | | |
| | | if (row != null) { |
| | | // columns=row.getPhysicalNumberOfCells();//不在这里设置,通过firstRow来获取列数信息。 |
| | | for (int j = 0; j < columns; j++) { |
| | | // 获取某行某列的某一个单元格 |
| | | cell = row.getCell(j); |
| | | if (row != null) { |
| | | // columns=row.getPhysicalNumberOfCells();//不在这里设置,通过firstRow来获取列数信息。 |
| | | for (int j = 0; j < columns; j++) { |
| | | // 获取某行某列的某一个单元格 |
| | | cell = row.getCell(j); |
| | | |
| | | // 往dataRow存值 |
| | | dataRow.add(getCellValue(cell)); |
| | | } |
| | | list.add(dataRow); |
| | | } else { |
| | | list.add(new ArrayList<>()); |
| | | } |
| | | } |
| | | // 往dataRow存值 |
| | | dataRow.add(getCellValue(cell)); |
| | | } |
| | | list.add(dataRow); |
| | | } else { |
| | | list.add(new ArrayList<>()); |
| | | } |
| | | } |
| | | |
| | | work.close(); |
| | | returnList.add(list); |
| | | } |
| | | work.close(); |
| | | returnList.add(list); |
| | | } |
| | | } catch (Exception e){ |
| | | Log.error(e.getMessage()); |
| | | } |
| | | |
| | | return returnList; |
| | | } |