liujie
2 天以前 ee7208a3bb7770e24fa135916fa5f7165b25fee6
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//package com.stylefeng.guns.modular.system.util;
//
//import org.apache.poi.hssf.usermodel.*;
//import org.apache.poi.ss.usermodel.*;
//import org.apache.poi.ss.usermodel.DateUtil;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import org.springframework.stereotype.Component;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
///**
// * 定义Excel工具类
// */
//@Component
//public class ExcelUtil {
//
//
//    public List<List<List<String>>> upload(String s,MultipartFile file){
//        InputStream inputStream = null;
//        try {
//            inputStream = file.getInputStream();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        //获取文件名
//        String fileName=s;
//        List<List<List<String>>> list = null;
//        if(validateExcel(fileName)) {
//            // 根据版本选择创建Workbook的方式
//            Workbook wb = null;
//            // 根据文件名判断文件是2003版本还是2007版本
//            if (isExcel2007(fileName)) {
//                try {
//                    wb = new XSSFWorkbook(inputStream);
//                } catch (IOException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            } else {
//                try {
//                    wb = new HSSFWorkbook(inputStream);
//                } catch (IOException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            }
//            int num = wb.getNumberOfSheets();//获取页数
//            list = new ArrayList<>();
//            for (int i = 0; i < num; i++) {
//                List<List<String>> sheetList = new ArrayList<>();
//                //获取每一页对象
//                Sheet sheet = wb.getSheetAt(i);
//                // 得到Excel的行数
//                int totalRows = sheet.getPhysicalNumberOfRows();
//                for (int j = 0; j < totalRows; j++) {
//                    if (j == 0) {
//                        continue;// 标题行
//                    }
//                    Row row = sheet.getRow(j);// 获取索引为i的行数据
//                    if(null == row){
//                        continue;
//                    }
//                    int index = sheet.getRow(0).getPhysicalNumberOfCells();//获取标题的列数用于遍历
//                    List<String> strings = new ArrayList<>();
//                    int in = 0;//用于遍历单元格判断该行是否全为空值
//                    for (int k = 0; k < index; k++) {//遍历获取每个单元格的数据
//                        String str = null;
//                        Cell cell = row.getCell(k);
//                        if(cell == null) {
//                            str = "";
//                            in++;
//                        }else {
//                            switch (cell.getCellType()) {//判断数据类型取值
//                                case NUMERIC :
//                                    if (DateUtil.isCellDateFormatted(cell)) {
//                                        Date theDate = cell.getDateCellValue();
//                                        str = String.valueOf(theDate.getTime());
//                                    }else{
//                                        String string = String.valueOf(cell.getNumericCellValue());
//                                        str = string.substring(0, string.indexOf("."));
//                                    }
//
//                                    break;
//                                case STRING :
//                                    str = cell.getStringCellValue();
//                                    if(str == null) {
//                                        str = "";
//                                        in++;
//                                    }
//                                    break;
//                                case _NONE :
//                                    System.err.println("_NONE");
//                                    break;
//                                case FORMULA :
//                                    System.err.println("FORMULA");
//                                    break;
//                                case BLANK :
//                                    str = cell.getStringCellValue();
//                                    if(str.equals("")) {
//                                        str = "";
//                                        in++;
//                                    }
//                                    break;
//                                case BOOLEAN :
//                                    System.err.println("BOOLEAN");
//                                    break;
//                                case ERROR :
//                                    System.err.println("ERROR");
//                                    break;
//                                default:
//                                    break;
//                            }
//                        }
//                        strings.add(String.valueOf(str).trim());
//                    }
//
//                    if(in != index) {//判断如果每个单元格都为null则不需要添加到集合中
//                        sheetList.add(strings);
//                    }
//                }
//                list.add(sheetList);
//            }
//
//
//        }
//
//        return list;
//    }
//
//    public List<List<List<String>>> upload(MultipartFile file){
//        InputStream inputStream = null;
//        try {
//            inputStream = file.getInputStream();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        //获取文件名
//        String fileName=file.getOriginalFilename();
//        List<List<List<String>>> list = null;
//        if(validateExcel(fileName)) {
//            // 根据版本选择创建Workbook的方式
//            Workbook wb = null;
//            // 根据文件名判断文件是2003版本还是2007版本
//            if (isExcel2007(fileName)) {
//                try {
//                    wb = new XSSFWorkbook(inputStream);
//                } catch (IOException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            } else {
//                try {
//                    wb = new HSSFWorkbook(inputStream);
//                } catch (IOException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            }
//            int num = wb.getNumberOfSheets();//获取页数
//            list = new ArrayList<>();
//            for (int i = 0; i < num; i++) {
//                List<List<String>> sheetList = new ArrayList<>();
//                //获取每一页对象
//                Sheet sheet = wb.getSheetAt(i);
//                // 得到Excel的行数
//                int totalRows = sheet.getPhysicalNumberOfRows();
//                for (int j = 0; j < totalRows; j++) {
//                    if (j == 0) {
//                        continue;// 标题行
//                    }
//                    Row row = sheet.getRow(j);// 获取索引为i的行数据
//                    if(null == row){
//                        continue;
//                    }
//                    int index = sheet.getRow(0).getPhysicalNumberOfCells();//获取标题的列数用于遍历
//                    List<String> strings = new ArrayList<>();
//                    int in = 0;//用于遍历单元格判断该行是否全为空值
//                    for (int k = 0; k < index; k++) {//遍历获取每个单元格的数据
//                        String str = null;
//                        Cell cell = row.getCell(k);
//                        if(cell == null) {
//                            str = "";
//                            in++;
//                        }else {
//                            switch (cell.getCellType()) {//判断数据类型取值
//                                case NUMERIC :
//                                    if (DateUtil.isCellDateFormatted(cell)) {
//                                        Date theDate = cell.getDateCellValue();
//                                        str = String.valueOf(theDate.getTime());
//                                    }else{
//                                        String string = String.valueOf(cell.getNumericCellValue());
//                                        str = string.substring(0, string.indexOf("."));
//                                    }
//
//                                    break;
//                                case STRING :
//                                    str = cell.getStringCellValue();
//                                    if(str == null) {
//                                        str = "";
//                                        in++;
//                                    }
//                                    break;
//                                case _NONE :
//                                    System.err.println("_NONE");
//                                    break;
//                                case FORMULA :
//                                    System.err.println("FORMULA");
//                                    break;
//                                case BLANK :
//                                    str = cell.getStringCellValue();
//                                    if(str.equals("")) {
//                                        str = "";
//                                        in++;
//                                    }
//                                    break;
//                                case BOOLEAN :
//                                    System.err.println("BOOLEAN");
//                                    break;
//                                case ERROR :
//                                    System.err.println("ERROR");
//                                    break;
//                                default:
//                                    break;
//                            }
//                        }
//                        strings.add(String.valueOf(str).trim());
//                    }
//
//                    if(in != index) {//判断如果每个单元格都为null则不需要添加到集合中
//                        sheetList.add(strings);
//                    }
//                }
//                list.add(sheetList);
//            }
//
//
//        }
//
//        return list;
//    }
//
//
//
//    // @描述:是否是2003的excel,返回true是2003
//    public static boolean isExcel2003(String filePath)  {
//        return filePath.matches("^.+\\.(?i)(xls)$");
//    }
//
//
//    //@描述:是否是2007的excel,返回true是2007
//    public static boolean isExcel2007(String filePath)  {
//        return filePath.matches("^.+\\.(?i)(xlsx)$");
//    }
//
//
//    /**
//     * 验证EXCEL文件
//     * @param filePath
//     * @return
//     */
//    public static boolean validateExcel(String filePath){
//        if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))){
//            return false;
//        }
//        return true;
//    }
//
//
//    /**
//     * 将数据写入Excel中
//     * @param titles    标题
//     * @param datas     数据
//     * @return
//     */
//    public HSSFWorkbook writeDataToExcel(List<List<String>> titles, List<List<List<String>>> datas) {
//        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//
//        for(int i = 0; i < titles.size(); i++){
//            HSSFSheet hssfSheet = hssfWorkbook.createSheet();
//            hssfSheet.setColumnWidth(0, 6 * 256);
//            hssfSheet.setDefaultRowHeightInPoints(20f);
//
//            HSSFRow hssfRow = hssfSheet.createRow(0);//设置第一行数据(标题)
//            HSSFCellStyle style = hssfWorkbook.createCellStyle();
//            HSSFFont font = hssfWorkbook.createFont();
//            font.setBold(true);
//            style.setFont(font);
//            style.setAlignment(HorizontalAlignment.CENTER);
//            for (int l = 0; l < titles.get(i).size(); l++) {
//                HSSFCell hssfCell = hssfRow.createCell(l);
//                hssfCell.setCellType(CellType.STRING);//设置表格类型
//                hssfCell.setCellValue(titles.get(i).get(l));
//                hssfCell.setCellStyle(style);
//                hssfSheet.setColumnWidth(l , 20 * 256);
//            }
//
//            //将数据添加到表格中
//            List<String> data = null;
//            for (int l = 0; l < datas.get(i).size(); l++) {
//                hssfRow = hssfSheet.createRow(l + 1);
//                data = datas.get(i).get(l);
//                for (int j = 0; j < data.size(); j++) {
//                    HSSFCell hssfCell = hssfRow.createCell(j);
//                    hssfCell.setCellType(CellType.STRING);//设置表格类型
//                    hssfCell.setCellValue(data.get(j));
//                }
//            }
//        }
//        return hssfWorkbook;
//    }
//
//    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
//        // 第一步,创建一个webbook,对应一个Excel文件
//        if (wb == null) {
//            wb = new HSSFWorkbook();
//        }
//        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
//        HSSFSheet sheet = wb.createSheet(sheetName);
//        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
//        HSSFRow row = sheet.createRow(0);
//        // 第四步,创建单元格,并设置值表头 设置表头居中
//        HSSFCellStyle style = wb.createCellStyle();
//        style.setLocked(true);
//        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
//
//        HSSFCell cell = null;
//        // 创建标题
//        for (int i = 0; i < title.length; i++) {
//            cell = row.createCell(i);
//            cell.setCellValue(title[i]);
//            cell.setCellStyle(style);
//        }
//        // 创建内容
//        for (int i = 0; i < values.length; i++) {
//            row = sheet.createRow(i + 1);
//            for (int j = 0; j < values[i].length; j++) {
//                cell = row.createCell(j);
//                cell.setCellValue(values[i][j]);
//                cell.setCellStyle(style);
//            }
//        }
//
//        return wb;
//    }
//
//}