jar
jinlin
2025-03-04 23f02e6b45dd7cf0ab2e7827144913ca59575ea4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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<CellRangeAddress> 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;
    }
}