jinlin
1 天以前 bf5b01b14dc7bfc214e646425a62f5593890d7e3
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.example.server.utils;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static org.apache.commons.lang3.ObjectUtils.isEmpty;
 
 
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;
    }
 
    /**
     * 判断时间格式是否正确
     *
     * @param value
     * @param format
     * @return
     */
    public static boolean isDate(String value, String format) {
 
        SimpleDateFormat sdf = null;
        ParsePosition pos = new ParsePosition(0);//指定从所传字符串的首位开始解析
 
        if (value == null || isEmpty(format)) {
            return false;
        }
        try {
            sdf = new SimpleDateFormat(format);
            sdf.setLenient(false);
            Date date = sdf.parse(value, pos);
            if (date == null) {
                return false;
            } else {
                //更为严谨的日期,如2011-03-024认为是不合法的
                if (pos.getIndex() > sdf.format(date).length()) {
                    return false;
                }
                return true;
            }
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * 导入异常处理
     * @param errMap
     * @param errSting
     * @param sheetName
     * @param row
     */
    public static void updateErrMap(Map<String, Object> errMap, String errSting, String sheetName, int row) {
        if (!errMap.containsKey(errSting)) {
            errMap.put(errSting, new ImportErrMessage(sheetName, row + "、"));
        } else {
            ImportErrMessage importErrMessage1 = (ImportErrMessage) errMap.get(errSting);
            importErrMessage1.setLineNumber(importErrMessage1.getLineNumber() + row + "、");
        }
    }
    /**
     * 从Excel的日期字段导入数据
     *  1. 如果Excel单元格为空,则返回null
     *  2. 无论Excel单元格格式是什么,只要是日期内容,都能导入,返回Date对象(单元格为文本格式时支持yyyy-mm-dd、yyyy/mm/dd、yyyy.mm.dd及yyyy年MM月dd日格式)
     *  3. 如果Excel单元格内容不是日期,则抛出ExcelImportException
     */
    public static Date getDateFromCell(Cell cell) throws ExcelImportException {
        if (cell == null) return null;
 
        //定义时间格式
        List<String> DateFormatList = new ArrayList<>();
        DateFormatList.add("yyyy年MM月dd日");
        DateFormatList.add("yyyy/MM/dd");
        DateFormatList.add("yyyy-MM-dd");
        DateFormatList.add("yyyy.MM.dd");
        List<SimpleDateFormat> formatList = new ArrayList<>();
        formatList.add(new SimpleDateFormat("yyyy年MM月dd日"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy/MM/dd"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy-MM-dd"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy.MM.dd"));//小写的mm表示的是分钟
 
        switch (cell.getCellType()) {
            case STRING:
                String strValue = cell.getStringCellValue();
                if (StringUtils.isBlank(strValue)) return null;
 
                strValue = strValue.trim();
                try {
                    for (int i = 0; i < DateFormatList.size(); i++) {
                        if (ImportUtil.isDate(strValue, DateFormatList.get(i))) {
                            Date date = formatList.get(i).parse(strValue);
                            if (date != null) {
                                return date;
                            }
                        }
                    }
                } catch (ParseException pe) {
                    throw new ExcelImportException("该单元格日期格式不正确");
                }
                throw new ExcelImportException("该单元格日期格式不正确");
 
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    // 正常日期格式
                    Date date = cell.getDateCellValue();
                    return date;
                } else {
                    // 检查是否是数字形式的日期
                    double numericValue = cell.getNumericCellValue();
                    if (isLikelyExcelDate(numericValue)) {
                        // 将Excel数字日期转换为Java日期
                        Date date = DateUtil.getJavaDate(numericValue);
                        return date;
                    } else {
                        // 确实是普通数字
                        throw new ExcelImportException("该单元格内容不是日期");
                    }
                }
 
            default:
                throw new ExcelImportException("该单元格内容不是日期");
        }
    }
 
    public static String getDateStrFromCell(Cell cell) throws ExcelImportException {
        if (cell == null) return null;
 
        //定义时间格式
        List<String> DateFormatList = new ArrayList<>();
        DateFormatList.add("yyyy年MM月dd日");
        DateFormatList.add("yyyy/MM/dd");
        DateFormatList.add("yyyy-MM-dd");
        DateFormatList.add("yyyy.MM.dd");
        List<SimpleDateFormat> formatList = new ArrayList<>();
        formatList.add(new SimpleDateFormat("yyyy年MM月dd日"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy/MM/dd"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy-MM-dd"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy.MM.dd"));//小写的mm表示的是分钟
 
        switch (cell.getCellType()) {
            case STRING:
                String strValue = cell.getStringCellValue();
                if (StringUtils.isBlank(strValue)) return null;
 
                strValue = strValue.trim();
                try {
                    for (int i = 0; i < DateFormatList.size(); i++) {
                        if (ImportUtil.isDate(strValue, DateFormatList.get(i))) {
                            Date date = formatList.get(i).parse(strValue);
                            if (date != null) {
                                return dateToString(date);
                            }
                        }
                    }
                } catch (ParseException pe) {
                    throw new ExcelImportException("该单元格日期格式不正确");
                }
                throw new ExcelImportException("该单元格日期格式不正确");
 
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    // 正常日期格式
                    Date date = cell.getDateCellValue();
                    return dateToString(date);
                } else {
                    // 检查是否是数字形式的日期
                    double numericValue = cell.getNumericCellValue();
                    if (isLikelyExcelDate(numericValue)) {
                        // 将Excel数字日期转换为Java日期
                        Date date = DateUtil.getJavaDate(numericValue);
                        return dateToString(date);
                    } else {
                        // 确实是普通数字
                        throw new ExcelImportException("该单元格内容不是日期");
                    }
                }
 
            default:
                throw new ExcelImportException("该单元格内容不是日期");
        }
    }
    /**
     * 判断数值是否可能是Excel日期
     * Excel日期通常在一定范围内(如36526-73051对应2000-21000年)
     */
    private static boolean isLikelyExcelDate(double value) {
        // Excel日期从1900-01-01开始,序列号1
        // 现代日期通常在36526-73051范围内
        return value >= 36526 && value < 73051;
    }
 
    private static String dateToString (Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
 
}