package com.example.server.utils; 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.ss.util.CellRangeAddress; import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.*; public class ImportUtil { /** * 获取当前单元格有几行 * @param cell * @param sheet * @return */ public static int getMergeRowNum(Cell cell, Sheet sheet) { int mergeSize = 1; List mergedRegions = sheet.getMergedRegions(); for (CellRangeAddress cellRangeAddress : mergedRegions) { if (cellRangeAddress.isInRange(cell)) { mergeSize = cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow() + 1; break; } } return mergeSize; } /** * 判断对象是否为空 * * @param obj * @return */ public static String ObjectFormat(Object obj) { return obj == null ? "" : obj.toString(); } // 将所有的值转换为String类型 public static String getCellValue(Row row, Integer i, String Pattern) { Cell cell = row.getCell(i); String cellStr = ""; cellStr = getCellValue(Pattern, cell, cellStr); return cellStr; } public static String getCellValue(String Pattern, Cell cell, String cellStr) { if (cell != null) { switch (cell.getCellType()) { case BLANK:// 空值 cellStr = ""; break; case NUMERIC:// 数字 if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {// 时间 DateFormat format = new SimpleDateFormat(Pattern); Date date = cell.getDateCellValue(); cellStr = format.format(date); break; } else { cellStr = String.valueOf(cell.getNumericCellValue()); if (cellStr.split("\\.").length == 2) {// 去.0 String[] ss = cellStr.split("\\."); if (ss[1].equals("0")) { cellStr = ss[0] + ""; } } } break; default: cellStr = cell.getStringCellValue(); break; } } return cellStr; } }