- public static List
- //便利Sheet标签页,获得每一行数
- int lastRowNum = sheet.getLastRowNum();
- ArrayList
- for(int i=0;i<=lastRowNum;i++) {
- if(i==0) continue;
- XSSFRow row = sheet.getRow(i);
- //获取当前行最后一个单元格索引
- short lastCellNum = row.getLastCellNum();
- HashMap<Integer, String> lies = new HashMap<Integer, String>();
- for (int j=0;j
- //根据单元格索引获取行
- XSSFCell cell = row.getCell(j);
- lies.put(j,getCellFormatValue(cell));
- // System.out.println(cell);
- }
- rows.add(lies);
- }
- return rows;
- }
-
- /**
- * 获取单元格格式化的值
- * @param cell 单元格
- * @return 值
- */
- public static String getCellFormatValue(Cell cell) {
- if (cell == null)
- return "";
- String cellvalue = "";
- // 判断当前单元格的type
- switch (cell.getCellType()) {
- case STRING:
- // /取得当前Cell的字符串
- cellvalue = cell.getRichStringCellValue().getString();
- break;
-
- // 如果当前Cell的type为NUMERIC或者_FORMULA
- case NUMERIC:
- case FORMULA:
- // 判断当前的Cell是否为Date
- if (DateUtil.isCellDateFormatted(cell)) {
- // 如果是在Date类型,则取得该Cell的Date值
- Date date = cell.getDateCellValue();
- //格式转换
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String format = sdf.format(date);
- //System.out.println(format);
- } else {
- //纯数字
- //区分整数与小数
- Double aDouble = cell.getNumericCellValue();
- if (aDouble == aDouble.intValue()) {
- cellvalue = aDouble.intValue() + "";
- } else {
- cellvalue = aDouble + "";
- }
- }
- break;
- case BOOLEAN:
- cellvalue = String.valueOf(cell.getBooleanCellValue());
- break;
- default:
- cellvalue = "";
- }
- return cellvalue;
- }
pom文件:
org.apache.poi
poi
4.0.1
org.apache.poi
poi-ooxml
4.0.1