Commit f71045df94aee0f43db02153906310ad31a4389b
1 parent
15acd796
Exists in
master
upgrade version
Showing
3 changed files
with
1 additions
and
838 deletions
Show diff stats
build.gradle
... | ... | @@ -20,8 +20,6 @@ mainClassName = 'com.taover.util.UtilString' |
20 | 20 | |
21 | 21 | dependencies { |
22 | 22 | compile( |
23 | - "org.apache.poi:poi:4.1.2", | |
24 | - "org.apache.poi:poi-excelant:4.1.2", | |
25 | 23 | "ch.ethz.ganymed:ganymed-ssh2:build210", |
26 | 24 | "org.apache.velocity:velocity:1.6.4", |
27 | 25 | "com.squareup.okhttp3:okhttp:3.14.1", |
... | ... | @@ -62,7 +60,7 @@ uploadArchives { |
62 | 60 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
63 | 61 | } |
64 | 62 | pom.project { |
65 | - version '1.1.124' | |
63 | + version '1.2.1' | |
66 | 64 | artifactId ARTIFACT_Id |
67 | 65 | groupId GROUP_ID |
68 | 66 | packaging TYPE | ... | ... |
src/main/java/com/taover/util/UtilExcel.java
... | ... | @@ -1,576 +0,0 @@ |
1 | -package com.taover.util; | |
2 | - | |
3 | -import java.io.File; | |
4 | -import java.io.FileOutputStream; | |
5 | -import java.text.DecimalFormat; | |
6 | -import java.text.SimpleDateFormat; | |
7 | -import java.util.ArrayList; | |
8 | -import java.util.Date; | |
9 | -import java.util.HashMap; | |
10 | -import java.util.List; | |
11 | -import java.util.Map; | |
12 | - | |
13 | -import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
14 | -import org.apache.poi.openxml4j.util.ZipSecureFile; | |
15 | -import org.apache.poi.ss.usermodel.Cell; | |
16 | -import org.apache.poi.ss.usermodel.CellStyle; | |
17 | -import org.apache.poi.ss.usermodel.CellType; | |
18 | -import org.apache.poi.ss.usermodel.DateUtil; | |
19 | -import org.apache.poi.ss.usermodel.FillPatternType; | |
20 | -import org.apache.poi.ss.usermodel.Row; | |
21 | -import org.apache.poi.ss.usermodel.Sheet; | |
22 | -import org.apache.poi.ss.usermodel.Workbook; | |
23 | -import org.apache.poi.ss.usermodel.WorkbookFactory; | |
24 | -import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
25 | - | |
26 | -@Deprecated | |
27 | -public class UtilExcel { | |
28 | - private final static String excel2003L =".xls"; //2003- 版本的excel | |
29 | - private final static String excel2007U =".xlsx"; //2007+ 版本的excel | |
30 | - private final static String CSV =".csv"; //csv | |
31 | - | |
32 | - private final static int maxExcelRowNum = 10000; | |
33 | - private final static int maxExcelColumnNum = 100; | |
34 | - | |
35 | - static { | |
36 | - ZipSecureFile.setMinInflateRatio(-1.0d); | |
37 | - } | |
38 | - | |
39 | - /** | |
40 | - * 描述:根据文件后缀,自适应上传文件的版本 | |
41 | - * @param inStr,fileName | |
42 | - * @return | |
43 | - * @throws Exception | |
44 | - */ | |
45 | - private static Workbook getWorkbook(String filePath, boolean isRead) throws Exception{ | |
46 | - Workbook wb = null; | |
47 | - File tempFile = null; | |
48 | - if(isRead){ | |
49 | - tempFile = new File(filePath); | |
50 | - if(!tempFile.exists()){ | |
51 | - throw new Exception("需要读取的文件不存在!"); | |
52 | - } | |
53 | - } | |
54 | - if(isRead) { | |
55 | - wb = WorkbookFactory.create(tempFile); | |
56 | - }else { | |
57 | - int dotIndex = filePath.lastIndexOf("."); | |
58 | - if(dotIndex < 0) { | |
59 | - throw new Exception("传入的文件没有指定扩展名"); | |
60 | - } | |
61 | - String filteTypeLower = filePath.substring(dotIndex).trim().toLowerCase(); | |
62 | - if(excel2003L.equals(filteTypeLower) || CSV.equals(filteTypeLower)){ | |
63 | - wb = new HSSFWorkbook(); //2003- | |
64 | - } else if (excel2007U.equals(filteTypeLower)){ | |
65 | - wb = new XSSFWorkbook(); //2007+ | |
66 | - } else { | |
67 | - throw new Exception("解析的文件格式有误!"); | |
68 | - } | |
69 | - } | |
70 | - return wb; | |
71 | - } | |
72 | - | |
73 | - /** | |
74 | - * 创建并保存excel表 | |
75 | - * @param sheetName | |
76 | - * @param data | |
77 | - * @param path | |
78 | - */ | |
79 | - public static File saveExcelFromListString(String sheetName, List<String[]> data, String path) throws Exception{ | |
80 | - Workbook wb = UtilExcel.getWorkbook(path, false); | |
81 | - //创建Excel工作簿对象 | |
82 | - Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 | |
83 | - for(int i=0; i<data.size(); ++i){ | |
84 | - Row row = sheet.createRow(i); //创建Excel工作表的行 | |
85 | - String[] dataRow = data.get(i); | |
86 | - if(dataRow != null){ | |
87 | - for(int j=0; j<dataRow.length; ++j){ | |
88 | - Cell cell = row.createCell(j); | |
89 | - String dataCell = dataRow[j]; | |
90 | - if(dataCell != null){ | |
91 | - cell.setCellValue(dataCell); | |
92 | - } | |
93 | - } | |
94 | - } | |
95 | - } | |
96 | - try { | |
97 | - FileOutputStream fileOut; | |
98 | - File tempFile = new File(path); | |
99 | - if(!tempFile.exists()){ | |
100 | - File parentFile = tempFile.getParentFile(); | |
101 | - if(!parentFile.exists()){ | |
102 | - parentFile.mkdirs(); | |
103 | - } | |
104 | - if(!tempFile.createNewFile()){ | |
105 | - return null; | |
106 | - } | |
107 | - } | |
108 | - fileOut = new FileOutputStream(tempFile); | |
109 | - wb.write(fileOut); | |
110 | - wb.close(); | |
111 | - fileOut.close(); | |
112 | - | |
113 | - return tempFile; | |
114 | - } catch (Exception e) { | |
115 | - // TODO Auto-generated catch block | |
116 | - e.printStackTrace(); | |
117 | - } | |
118 | - return null; | |
119 | - } | |
120 | - | |
121 | - /** | |
122 | - * 创建并保存excel表 | |
123 | - * @param sheetName | |
124 | - * @param data | |
125 | - * @param path | |
126 | - */ | |
127 | - public static void saveExcel(String sheetName, List<List<Object>> data, String path) throws Exception{ | |
128 | - Workbook wb = UtilExcel.getWorkbook(path, false); | |
129 | - //创建Excel工作簿对象 | |
130 | - Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 | |
131 | - for(int i=0; i<data.size(); ++i){ | |
132 | - Row row = sheet.createRow(i); //创建Excel工作表的行 | |
133 | - List<Object> dataRow = data.get(i); | |
134 | - if(dataRow != null){ | |
135 | - for(int j=0; j<dataRow.size(); ++j){ | |
136 | - Cell cell = row.createCell(j); | |
137 | - Object dataCell = dataRow.get(j); | |
138 | - setCellValue(cell, dataCell); | |
139 | - } | |
140 | - } | |
141 | - } | |
142 | - try { | |
143 | - FileOutputStream fileOut; | |
144 | - File tempFile = new File(path); | |
145 | - if(!tempFile.exists()){ | |
146 | - File parentFile = tempFile.getParentFile(); | |
147 | - if(!parentFile.exists()){ | |
148 | - parentFile.mkdirs(); | |
149 | - } | |
150 | - if(!tempFile.createNewFile()){ | |
151 | - return; | |
152 | - } | |
153 | - } | |
154 | - fileOut = new FileOutputStream(tempFile); | |
155 | - wb.write(fileOut); | |
156 | - wb.close(); | |
157 | - fileOut.close(); | |
158 | - } catch (Exception e) { | |
159 | - // TODO Auto-generated catch block | |
160 | - e.printStackTrace(); | |
161 | - } | |
162 | - } | |
163 | - | |
164 | - private static void setCellValue(Cell cell, Object data) { | |
165 | - if(data != null){ | |
166 | - if(ClassUtil.isBasicType(data.getClass())){ | |
167 | - cell.setCellValue(Double.valueOf(data.toString())); | |
168 | - }else if(data.getClass().getSimpleName().equals("Date")){ | |
169 | - cell.setCellValue((Date)data); | |
170 | - }else{ | |
171 | - cell.setCellValue(data.toString()); | |
172 | - } | |
173 | - } | |
174 | - } | |
175 | - | |
176 | - /** | |
177 | - * 创建并保存excel表 sheet | |
178 | - * @param sheetName | |
179 | - * @param data | |
180 | - * @param path | |
181 | - */ | |
182 | - public static void saveExcelContailSheet(List<String> sheetList, List<List<List<Object>>> dataList, String path) throws Exception{ | |
183 | - if(sheetList.size() != dataList.size()){ | |
184 | - throw new Exception("sheet size != excel size"); | |
185 | - } | |
186 | - Workbook wb = UtilExcel.getWorkbook(path, false); | |
187 | - //创建Excel工作簿对象 | |
188 | - | |
189 | - for (int j = 0;j< dataList.size(); j++) { | |
190 | - List<List<Object>> data = dataList.get(j); | |
191 | - Sheet sheet = wb.createSheet(sheetList.get(j));//创建Excel工作表对象 | |
192 | - for(int i=0; i<data.size(); ++i){ | |
193 | - Row row = sheet.createRow(i); //创建Excel工作表的行 | |
194 | - List<Object> dataRow = data.get(i); | |
195 | - if(dataRow != null){ | |
196 | - for(int k=0; k<dataRow.size(); ++k){ | |
197 | - Cell cell = row.createCell(k); | |
198 | - Object dataCell = dataRow.get(k); | |
199 | - setCellValue(cell, dataCell); | |
200 | - } | |
201 | - } | |
202 | - } | |
203 | - } | |
204 | - | |
205 | - try { | |
206 | - FileOutputStream fileOut; | |
207 | - File tempFile = new File(path); | |
208 | - if(!tempFile.exists()){ | |
209 | - File parentFile = tempFile.getParentFile(); | |
210 | - if(!parentFile.exists()){ | |
211 | - parentFile.mkdirs(); | |
212 | - } | |
213 | - if(!tempFile.createNewFile()){ | |
214 | - return; | |
215 | - } | |
216 | - } | |
217 | - fileOut = new FileOutputStream(tempFile); | |
218 | - wb.write(fileOut); | |
219 | - wb.close(); | |
220 | - fileOut.close(); | |
221 | - } catch (Exception e) { | |
222 | - // TODO Auto-generated catch block | |
223 | - e.printStackTrace(); | |
224 | - } | |
225 | - } | |
226 | - | |
227 | - /** | |
228 | - * 创建并保存excel表 | |
229 | - * @param sheetName | |
230 | - * @param data | |
231 | - * @param path | |
232 | - */ | |
233 | - public static void saveExcel(String sheetName, List<List<Object>> data, String path, List<Short> backColorList) throws Exception{ | |
234 | - Workbook wb = UtilExcel.getWorkbook(path, false); | |
235 | - Map<Short, CellStyle> cellStyleMap = new HashMap<Short, CellStyle>(); | |
236 | - Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 | |
237 | - for(int i=0; i<data.size(); ++i){ | |
238 | - Row row = sheet.createRow(i); //创建Excel工作表的行 | |
239 | - List<Object> dataRow = data.get(i); | |
240 | - Short backColor = backColorList.get(i); | |
241 | - CellStyle style = cellStyleMap.get(backColor); | |
242 | - if(style == null){ | |
243 | - style = wb.createCellStyle(); | |
244 | - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); | |
245 | - cellStyleMap.put(backColor, style); | |
246 | - } | |
247 | - if(backColor != null){ | |
248 | - style.setFillForegroundColor(backColor.shortValue()); | |
249 | - } | |
250 | - if(dataRow != null){ | |
251 | - for(int j=0; j<dataRow.size(); ++j){ | |
252 | - Cell cell = row.createCell(j); | |
253 | - Object dataCell = dataRow.get(j); | |
254 | - setCellValue(cell, dataCell); | |
255 | - if(backColor != null){ | |
256 | - cell.setCellStyle(style); | |
257 | - } | |
258 | - } | |
259 | - } | |
260 | - } | |
261 | - try { | |
262 | - FileOutputStream fileOut; | |
263 | - File tempFile = new File(path); | |
264 | - if(!tempFile.exists()){ | |
265 | - File parentFile = tempFile.getParentFile(); | |
266 | - if(!parentFile.exists()){ | |
267 | - parentFile.mkdirs(); | |
268 | - } | |
269 | - if(!tempFile.createNewFile()){ | |
270 | - return; | |
271 | - } | |
272 | - } | |
273 | - fileOut = new FileOutputStream(tempFile); | |
274 | - wb.write(fileOut); | |
275 | - wb.close(); | |
276 | - fileOut.close(); | |
277 | - } catch (Exception e) { | |
278 | - // TODO Auto-generated catch block | |
279 | - e.printStackTrace(); | |
280 | - } | |
281 | - } | |
282 | - | |
283 | - /** | |
284 | - * 读取excel表--当前激活的工作表 | |
285 | - * @param path | |
286 | - */ | |
287 | - public static List<List<Object>> readExcel(String filepath) throws Exception{ | |
288 | - File file = new File(filepath); | |
289 | - if(!file.exists()){ | |
290 | - throw new Exception("Excel["+filepath+"]文件不存在"); | |
291 | - } | |
292 | - Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
293 | - return readExcelBySheetIndex(wb, wb.getActiveSheetIndex()); | |
294 | - } | |
295 | - | |
296 | - /** | |
297 | - * 读取excel表--当前激活的工作表 | |
298 | - * @param path | |
299 | - */ | |
300 | - public static List<List<Object>> readExcel(String filepath, boolean hasLimit) throws Exception{ | |
301 | - File file = new File(filepath); | |
302 | - if(!file.exists()){ | |
303 | - throw new Exception("Excel["+filepath+"]文件不存在"); | |
304 | - } | |
305 | - Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
306 | - return readExcelBySheetIndex(wb, wb.getActiveSheetIndex(), hasLimit); | |
307 | - } | |
308 | - | |
309 | - /** | |
310 | - * 读取 | |
311 | - * @param filepath | |
312 | - * @return | |
313 | - * @throws Exception | |
314 | - */ | |
315 | - public static List<List<Object>> readExcelExcludeHideLine(String filepath) throws Exception{ | |
316 | - File file = new File(filepath); | |
317 | - if(!file.exists()){ | |
318 | - throw new Exception("Excel["+filepath+"]文件不存在"); | |
319 | - } | |
320 | - Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
321 | - return readExcelBySheetIndexExcludeHideLine(filepath, wb.getActiveSheetIndex()); | |
322 | - } | |
323 | - | |
324 | - /** | |
325 | - * 读取excel表--所有工作表 | |
326 | - * @param path | |
327 | - */ | |
328 | - public static List<List<Object>> readExcelAllSheet(String filepath) throws Exception{ | |
329 | - File file = new File(filepath); | |
330 | - List<List<Object>> result = new ArrayList<List<Object>>(); | |
331 | - if(!file.exists()){ | |
332 | - throw new Exception("Excel["+filepath+"]文件不存在"); | |
333 | - } | |
334 | - Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
335 | - int sheetNumber = wb.getNumberOfSheets(); | |
336 | - | |
337 | - for(int sheetIndex=0; sheetIndex<sheetNumber; ++sheetIndex){ | |
338 | - result.addAll(readExcelBySheetIndex(filepath, sheetIndex)); | |
339 | - } | |
340 | - | |
341 | - return result; | |
342 | - } | |
343 | - | |
344 | - | |
345 | - /** | |
346 | - * 读取excel表--所有工作表 Map封装 | |
347 | - * @param path | |
348 | - */ | |
349 | - public static Map<String,List<List<Object>>> readExcelAllSheetMap(String filepath) throws Exception{ | |
350 | - File file = new File(filepath); | |
351 | - HashMap<String, List<List<Object>>> map = new HashMap<String,List<List<Object>>>(); | |
352 | - if(!file.exists()){ | |
353 | - throw new Exception("Excel["+filepath+"]文件不存在"); | |
354 | - } | |
355 | - Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
356 | - int sheetNumber = wb.getNumberOfSheets(); | |
357 | - | |
358 | - for(int sheetIndex=0; sheetIndex<sheetNumber; ++sheetIndex){ | |
359 | - map.put(sheetIndex+"",(readExcelBySheetIndexExcludeHideLine(filepath, sheetIndex))); | |
360 | - } | |
361 | - | |
362 | - return map; | |
363 | - } | |
364 | - | |
365 | - | |
366 | - | |
367 | - /** | |
368 | - * 读取excel表--当前激活的工作表 | |
369 | - * @param path | |
370 | - */ | |
371 | - public static List<List<Object>> readExcelBySheetIndex(String filepath, int sheetIndex) throws Exception{ | |
372 | - File file = new File(filepath); | |
373 | - if(!file.exists()){ | |
374 | - throw new Exception("Excel["+filepath+"]文件不存在"); | |
375 | - } | |
376 | - Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
377 | - Sheet sheet = wb.getSheetAt(sheetIndex); | |
378 | - return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false); | |
379 | - } | |
380 | - | |
381 | - private static List<List<Object>> readExcelBySheetIndex(Workbook wb, int sheetIndex) throws Exception{ | |
382 | - return readExcelBySheetIndex(wb, sheetIndex, true); | |
383 | - } | |
384 | - | |
385 | - private static List<List<Object>> readExcelBySheetIndex(Workbook wb, int sheetIndex, boolean hasRowLimit) throws Exception{ | |
386 | - Sheet sheet = wb.getSheetAt(sheetIndex); | |
387 | - if(hasRowLimit){ | |
388 | - return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false); | |
389 | - }else { | |
390 | - return readExcelBySheet(sheet, Integer.MAX_VALUE, false); | |
391 | - } | |
392 | - } | |
393 | - | |
394 | - /** | |
395 | - * 读取excel表--当前激活的工作表 | |
396 | - * @param path | |
397 | - */ | |
398 | - private static List<List<Object>> readExcelBySheet(Sheet sheet, int rowLimit, boolean excludeRowZeroHeight) throws Exception{ | |
399 | - List<List<Object>> result = new ArrayList<List<Object>>(); | |
400 | - int start = sheet.getFirstRowNum(); | |
401 | - int end = sheet.getLastRowNum(); | |
402 | - if(end > rowLimit){ | |
403 | - end = rowLimit; | |
404 | - } | |
405 | - DecimalFormat df = new DecimalFormat("0.####"); | |
406 | - for(int i=start; i<end+1; ++i){ | |
407 | - Row row = sheet.getRow(i); | |
408 | - if(row == null){ | |
409 | - continue; | |
410 | - } | |
411 | - //去除隐藏行 | |
412 | - if(excludeRowZeroHeight && row.getZeroHeight()){ | |
413 | - continue; | |
414 | - } | |
415 | - List<Object> dataRow = new ArrayList<Object>(); | |
416 | - int lastCellNum = row.getLastCellNum(); | |
417 | - if(lastCellNum > UtilExcel.maxExcelColumnNum){ | |
418 | - lastCellNum = UtilExcel.maxExcelColumnNum; | |
419 | - } | |
420 | - for(int j=0; j<lastCellNum; ++j){ | |
421 | - try { | |
422 | - dataRow.add(getCellValue(row.getCell(j), df)); | |
423 | - }catch(Exception e) { | |
424 | - dataRow.add(""); | |
425 | - e.printStackTrace(); | |
426 | - } | |
427 | - } | |
428 | - result.add(dataRow); | |
429 | - } | |
430 | - return result; | |
431 | - } | |
432 | - | |
433 | - private static Object getCellValue(Cell cell, DecimalFormat df) { | |
434 | - if(cell == null) { | |
435 | - return ""; | |
436 | - } | |
437 | - CellType currCellType = cell.getCellTypeEnum(); | |
438 | - | |
439 | - if(currCellType.compareTo(CellType.STRING) == 0){ | |
440 | - return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); | |
441 | - }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ | |
442 | - | |
443 | - /** | |
444 | - * yyyy-MM-dd----- 14 | |
445 | - yyyy年m月d日--- 31 | |
446 | - yyyy年m月------- 57 | |
447 | - m月d日 ---------- 58 | |
448 | - HH:mm----------- 20 | |
449 | - h时mm分 ------- 32 | |
450 | - yyyy-MM-dd HH:hh:ss 22 | |
451 | - */ | |
452 | - short format = cell.getCellStyle().getDataFormat(); | |
453 | - if (DateUtil.isCellDateFormatted(cell)) { | |
454 | - SimpleDateFormat sdf = null; | |
455 | - if(format == 14 || format == 31 || format == 57 || format == 58 ){ | |
456 | - //日期 | |
457 | - sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
458 | - }else if (format == 20 || format == 32) { | |
459 | - //时间 | |
460 | - sdf = new SimpleDateFormat("HH:mm"); | |
461 | - }else { | |
462 | - sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
463 | - } | |
464 | - double value = cell.getNumericCellValue(); | |
465 | - Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value); | |
466 | - return sdf.format(date); | |
467 | - } else { | |
468 | - return df.format(cell.getNumericCellValue()); | |
469 | - } | |
470 | - | |
471 | - }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ | |
472 | - return cell.getBooleanCellValue(); | |
473 | - }else if(currCellType.compareTo(CellType.FORMULA) == 0){ | |
474 | - return getFormulatValue(cell, df); | |
475 | - } | |
476 | - return ""; | |
477 | - } | |
478 | - | |
479 | - private static Object getFormulatValue(Cell cell, DecimalFormat df) { | |
480 | - CellType formulatResultType = cell.getCachedFormulaResultTypeEnum(); | |
481 | - | |
482 | - if(formulatResultType.compareTo(CellType.STRING) == 0){ | |
483 | - return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); | |
484 | - }else if(formulatResultType.compareTo(CellType.NUMERIC) == 0){ | |
485 | - return df.format(cell.getNumericCellValue()); | |
486 | - }else if(formulatResultType.compareTo(CellType.BOOLEAN) == 0){ | |
487 | - return cell.getBooleanCellValue(); | |
488 | - }else if(formulatResultType.compareTo(CellType.FORMULA) == 0){ | |
489 | - return ""; | |
490 | - } | |
491 | - return ""; | |
492 | - } | |
493 | - | |
494 | - public static List<List<Object>> readExcelBySheetIndexExcludeHideLine(String filepath, int sheetIndex) throws Exception{ | |
495 | - File file = new File(filepath); | |
496 | - if(!file.exists()){ | |
497 | - throw new Exception("Excel文件["+filepath+"]不存在"); | |
498 | - } | |
499 | - Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
500 | - Sheet sheet = wb.getSheetAt(sheetIndex); | |
501 | - return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, true); | |
502 | - } | |
503 | - | |
504 | - public static void main(String args[]){ | |
505 | - //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; | |
506 | - //String filepath = "C:\\Users\\gaoming\\Desktop\\cccc.xls"; | |
507 | - DecimalFormat df = new DecimalFormat("0.####"); | |
508 | - | |
509 | - System.out.println(df.format(3.333113F)); | |
510 | - System.out.println(df.format(3.3)); | |
511 | - System.out.println(df.format(3.3300)); | |
512 | - | |
513 | - | |
514 | - String filepath = "C:\\Users\\EDZ\\Desktop\\詹姆士0623(3)(1).xlsx"; | |
515 | - List<List<Object>> data = null; | |
516 | - | |
517 | - try { | |
518 | - long start = System.currentTimeMillis(); | |
519 | - System.out.println(start); | |
520 | - Map<String, List<List<Object>>> map = UtilExcel.readExcelAllSheetMap(filepath); | |
521 | - long end = System.currentTimeMillis(); | |
522 | - System.out.println(end); | |
523 | - System.out.println((end-start)/1000); | |
524 | - | |
525 | - | |
526 | - //System.out.println(map); | |
527 | - data = map.get("0"); | |
528 | - System.out.println(data); | |
529 | - for (int i = 0; i < data.size(); i++) { | |
530 | - System.out.println(data.get(i).get(1)); | |
531 | - } | |
532 | -// System.out.println(data.size()); | |
533 | -// System.out.println(UtilExcel.readExcelAllSheetMap(filepath)); | |
534 | - } catch (Exception e) { | |
535 | - // TODO Auto-generated catch block | |
536 | - e.printStackTrace(); | |
537 | - } | |
538 | -// List<Short> styleList = new ArrayList<Short>(); | |
539 | -// for(int i=0; i<data.size(); ++i){ | |
540 | -// if(i == 1)styleList.add(Short.valueOf(HSSFColor.RED.index)); | |
541 | -// else styleList.add(null); | |
542 | -// for(int j=0; j<data.get(i).size(); ++j){ | |
543 | -// System.out.print(data.get(i).get(j).toString()+" : "); | |
544 | -// } | |
545 | -// System.out.println(""); | |
546 | -// } | |
547 | -// try { | |
548 | -// List<String> headerList = new ArrayList<String>(); | |
549 | -// headerList.add("shhe1"); | |
550 | -// headerList.add("shhe2"); | |
551 | -// List<List<List<Object>>> dataList = new ArrayList<List<List<Object>>>(); | |
552 | -// List<List<Object>> list1 = new ArrayList<List<Object>>(); | |
553 | -// List<Object> list11 = new ArrayList<Object>(); | |
554 | -// list11.add("hahaha"); | |
555 | -// list11.add("hahaha"); | |
556 | -// list11.add("hahaha"); | |
557 | -// list1.add(list11); | |
558 | -// | |
559 | -// List<List<Object>> list2 = new ArrayList<List<Object>>(); | |
560 | -// List<Object> list22 = new ArrayList<Object>(); | |
561 | -// list22.add("hahaha2"); | |
562 | -// list22.add("hahaha2"); | |
563 | -// list22.add("hahaha2"); | |
564 | -// list2.add(list22); | |
565 | -// list2.add(list22); | |
566 | -// | |
567 | -// dataList.add(list1); | |
568 | -// dataList.add(list2); | |
569 | -// | |
570 | -// UtilExcel.saveExcelContailSheet(headerList, dataList, "C:\\Users\\gaoming\\Desktop\\qwer.xls"); | |
571 | -// } catch (Exception e) { | |
572 | -// // TODO Auto-generated catch block | |
573 | -// e.printStackTrace(); | |
574 | -// } | |
575 | - } | |
576 | -} |
src/test/java/TempExcel.java
... | ... | @@ -1,259 +0,0 @@ |
1 | -import java.io.File; | |
2 | -import java.util.ArrayList; | |
3 | -import java.util.Collections; | |
4 | -import java.util.Date; | |
5 | -import java.util.HashMap; | |
6 | -import java.util.Iterator; | |
7 | -import java.util.List; | |
8 | -import java.util.Map; | |
9 | - | |
10 | -import com.taover.util.UtilExcel; | |
11 | - | |
12 | -public class TempExcel { | |
13 | - public static void main(String[] args){ | |
14 | - //checkExcelError(); | |
15 | - saveExcel(); | |
16 | - } | |
17 | - | |
18 | - public static void saveExcel() { | |
19 | - List<List<Object>> data = new ArrayList<List<Object>>(); | |
20 | - data.add(Collections.singletonList(123)); | |
21 | - data.add(Collections.singletonList(123.222)); | |
22 | - data.add(Collections.singletonList(1232223234234234L)); | |
23 | - data.add(Collections.singletonList("123")); | |
24 | - data.add(Collections.singletonList("1232223234234234")); | |
25 | - data.add(Collections.singletonList(new Date())); | |
26 | - try { | |
27 | - UtilExcel.saveExcel("data", data, "C:\\Users\\Administrator\\Desktop\\1234.xlsx"); | |
28 | - } catch (Exception e) { | |
29 | - e.printStackTrace(); | |
30 | - } | |
31 | - } | |
32 | - | |
33 | - public static void checkExcelError() { | |
34 | - List<List<Object>> data = null; | |
35 | - try { | |
36 | - data = UtilExcel.readExcelAllSheet("C:\\Users\\Administrator\\Desktop\\1234.xls"); | |
37 | - } catch (Exception e) { | |
38 | - e.printStackTrace(); | |
39 | - } | |
40 | - System.out.println("end"+data.get(1).get(10)); | |
41 | - //dealExcel(); | |
42 | - //readExcel(); | |
43 | - } | |
44 | - | |
45 | - private static void readExcel() { | |
46 | - File file = new File("C:\\Users\\Administrator\\Desktop\\异常Excel\\1_2020-03-08-14h55m00s-面包仓-订单回传数据(2)(1).xlsx"); | |
47 | - try { | |
48 | - Map<String, List<List<Object>>> readExcelAllSheetMap = UtilExcel.readExcelAllSheetMap(file.getAbsolutePath()); | |
49 | - System.out.println("12"); | |
50 | - } catch (Exception e) { | |
51 | - e.printStackTrace(); | |
52 | - } | |
53 | - } | |
54 | - | |
55 | - public static final String SEPARATE_CONSIGNEE_MOBILE = "__"; | |
56 | - public static final int EXCEL_TIANMAO_CONSIGNEE_INDEX = 14; | |
57 | - public static final int EXCEL_TIANMAO_MOBILE_INDEX = 18; | |
58 | - public static final int EXCEL_TIANMAO_ORDERDATE_INDEX = 20; | |
59 | - public static final int EXCEL_TIANMAO_GOODSNUMBER_INDEX = 26; | |
60 | - public static final int EXCEL_JD_CONSIGNEE_INDEX = 14; | |
61 | - public static final int EXCEL_JD_MOBILE_INDEX = 16; | |
62 | - public static final int EXCEL_JD_ORDERDATE_INDEX = 24; | |
63 | - public static final int EXCEL_JD_GOODSNUMBER_INDEX = 3; | |
64 | - | |
65 | - public static void dealExcel(){ | |
66 | - try { | |
67 | - List<List<Object>> tianmaoData = UtilExcel.readExcel("D:\\tempexcel\\tianmao.xlsx"); | |
68 | - List<List<Object>> jdData = UtilExcel.readExcel("D:\\tempexcel\\jd.xls"); | |
69 | - tianmaoData.remove(0); | |
70 | - jdData.remove(0); | |
71 | - | |
72 | - //按下单人及下单手机号分类 | |
73 | - Map<String, List<List<Object>>> separateData = new HashMap<String, List<List<Object>>>(); | |
74 | - for(int i=0; i<tianmaoData.size(); ++i){ | |
75 | - List<Object> tempData = tianmaoData.get(i); | |
76 | - injectMap(separateData, getNameAndMobileByTianmao(tempData), tempData); | |
77 | - } | |
78 | - for(int i=0; i<jdData.size(); ++i){ | |
79 | - List<Object> tempData = jdData.get(i); | |
80 | - injectMap(separateData, getNameAndMobileByJd(tempData), tempData); | |
81 | - } | |
82 | - | |
83 | - //分析下单分类 | |
84 | - List<SeparateAnalysisInfo> analysisData = new ArrayList<SeparateAnalysisInfo>(); | |
85 | - Iterator<String> keyIter = separateData.keySet().iterator(); | |
86 | - while(keyIter.hasNext()){ | |
87 | - String keyName = keyIter.next(); | |
88 | - List<List<Object>> keyData = separateData.get(keyName); | |
89 | - if(keyData.size() <= 1){ | |
90 | - continue; | |
91 | - } | |
92 | - | |
93 | - String[] keyNameSep = keyName.split(TempExcel.SEPARATE_CONSIGNEE_MOBILE); | |
94 | - if(keyNameSep.length == 4){ | |
95 | - SeparateAnalysisInfo anaInfo = new SeparateAnalysisInfo(keyNameSep[0], keyNameSep[1], keyData, Integer.valueOf(keyNameSep[2]), Integer.valueOf(keyNameSep[3])); | |
96 | - if(anaInfo.getDescList().size() > 1){ | |
97 | - analysisData.add(anaInfo); | |
98 | - } | |
99 | - } | |
100 | - } | |
101 | - | |
102 | - //打印信息,保存信息 | |
103 | - List<List<Object>> resultInfo = new ArrayList<List<Object>>(); | |
104 | - List<Object> blankRow = new ArrayList<Object>(); | |
105 | - for(int i=0; i<analysisData.size(); ++i){ | |
106 | - SeparateAnalysisInfo item = analysisData.get(i); | |
107 | - | |
108 | - //添加空白间距行 | |
109 | - resultInfo.add(blankRow); | |
110 | - resultInfo.add(blankRow); | |
111 | - | |
112 | - //用户信息列 | |
113 | - List<Object> userInfo = new ArrayList<Object>(); | |
114 | - userInfo.add("收货人:"+item.getConsignee()); | |
115 | - userInfo.add("电话号:"+item.getMobile()); | |
116 | - resultInfo.add(userInfo); | |
117 | - | |
118 | - //下单信息列 | |
119 | - List<Object> descInfo = new ArrayList<Object>(); | |
120 | - descInfo.add("下单简述"); | |
121 | - for(int j=0; j<item.getDescList().size(); ++j){ | |
122 | - descInfo.add(item.getDescList().get(j)); | |
123 | - } | |
124 | - resultInfo.add(descInfo); | |
125 | - | |
126 | - //订单详情 | |
127 | - resultInfo.addAll(item.getOrderData()); | |
128 | - } | |
129 | - UtilExcel.saveExcel("处理结果", resultInfo, "D:\\tempexcel\\resultinfo.xlsx"); | |
130 | - } catch (Exception e) { | |
131 | - e.printStackTrace(); | |
132 | - } | |
133 | - } | |
134 | - | |
135 | - public static void injectMap(Map<String, List<List<Object>>> separateData, String keyName, List<Object> keyValue){ | |
136 | - if(separateData.containsKey(keyName)){ | |
137 | - separateData.get(keyName).add(keyValue); | |
138 | - }else{ | |
139 | - List<List<Object>> tempData = new ArrayList<List<Object>>(); | |
140 | - tempData.add(keyValue); | |
141 | - separateData.put(keyName, tempData); | |
142 | - } | |
143 | - } | |
144 | - | |
145 | - public static String getNameAndMobileByTianmao(List<Object> data){ | |
146 | - String name = data.get(EXCEL_TIANMAO_CONSIGNEE_INDEX).toString(); | |
147 | - String mobile = data.get(EXCEL_TIANMAO_MOBILE_INDEX).toString(); | |
148 | - return name+SEPARATE_CONSIGNEE_MOBILE+mobile+SEPARATE_CONSIGNEE_MOBILE+EXCEL_TIANMAO_ORDERDATE_INDEX+SEPARATE_CONSIGNEE_MOBILE+EXCEL_TIANMAO_GOODSNUMBER_INDEX; | |
149 | - } | |
150 | - | |
151 | - public static String getNameAndMobileByJd(List<Object> data){ | |
152 | - String name = data.get(EXCEL_JD_CONSIGNEE_INDEX).toString(); | |
153 | - String mobile = data.get(EXCEL_JD_MOBILE_INDEX).toString(); | |
154 | - return name+SEPARATE_CONSIGNEE_MOBILE+mobile+SEPARATE_CONSIGNEE_MOBILE+EXCEL_TIANMAO_ORDERDATE_INDEX+SEPARATE_CONSIGNEE_MOBILE+EXCEL_TIANMAO_GOODSNUMBER_INDEX; | |
155 | - } | |
156 | -} | |
157 | - | |
158 | -class SeparateAnalysisInfo{ | |
159 | - private String consignee; | |
160 | - private String mobile; | |
161 | - private List<String> descList; | |
162 | - private List<List<Object>> orderData; | |
163 | - | |
164 | - public SeparateAnalysisInfo(String consignee, String mobile, List<List<Object>> data, int excelOrderDateIndex, int excelGoodsNumberIndex){ | |
165 | - this.consignee = consignee; | |
166 | - this.mobile = mobile; | |
167 | - this.orderData = data; | |
168 | - | |
169 | - for(int i=0; i<data.size(); ++i){ | |
170 | - List<Object> dataItem = data.get(i); | |
171 | - if(dataItem.get(excelOrderDateIndex) == null){ | |
172 | - System.out.println("没有下单日期信息:"+this.consignee+"__"+this.mobile); | |
173 | - continue; | |
174 | - } | |
175 | - if(dataItem.get(excelGoodsNumberIndex) == null){ | |
176 | - System.out.println("没有商品数量信息:"+this.consignee+"__"+this.mobile); | |
177 | - continue; | |
178 | - } | |
179 | - | |
180 | - String orderDate = dataItem.get(excelOrderDateIndex).toString(); | |
181 | - if(orderDate.contains("-")){ | |
182 | - orderDate = orderDate.substring(0, 10); | |
183 | - }else{ | |
184 | - String[] dateInfo = orderDate.split(" "); | |
185 | - if(dateInfo.length > 0){ | |
186 | - orderDate = dateInfo[0].replace("/", "-"); | |
187 | - } | |
188 | - } | |
189 | - String goodsNumber = dataItem.get(excelGoodsNumberIndex).toString(); | |
190 | - this.addDescListItem(orderDate, goodsNumber); | |
191 | - } | |
192 | - } | |
193 | - | |
194 | - public String getConsignee() { | |
195 | - return consignee; | |
196 | - } | |
197 | - public void setConsignee(String consignee) { | |
198 | - this.consignee = consignee; | |
199 | - } | |
200 | - public String getMobile() { | |
201 | - return mobile; | |
202 | - } | |
203 | - public void setMobile(String mobile) { | |
204 | - this.mobile = mobile; | |
205 | - } | |
206 | - public List<String> getDescList() { | |
207 | - return descList; | |
208 | - } | |
209 | - public void setDescList(List<String> descList) { | |
210 | - this.descList = descList; | |
211 | - } | |
212 | - | |
213 | - public void addDescListItem(String orderDate, String goodsNumber){ | |
214 | - if(this.descList == null){ | |
215 | - this.descList = new ArrayList<String>(); | |
216 | - } | |
217 | - String separateStr = "__"; | |
218 | - | |
219 | - //检查是否有重复日期的情况 | |
220 | - boolean existsDate = false; | |
221 | - for(int i=0; i<this.descList.size(); ++i){ | |
222 | - String tempDesc = this.descList.get(i); | |
223 | - if(tempDesc.startsWith(orderDate)){ | |
224 | - existsDate = true; | |
225 | - String[] tempData = tempDesc.split(separateStr); | |
226 | - if(tempData.length > 1){ | |
227 | - String[] tempDataGoods = tempData[1].split("件"); | |
228 | - if(tempDataGoods.length > 0){ | |
229 | - try{ | |
230 | - int totalNumber = Integer.valueOf(tempDataGoods[0]).intValue()+Integer.valueOf(goodsNumber).intValue(); | |
231 | - this.descList.set(i, orderDate+separateStr+totalNumber+"件"); | |
232 | - }catch(Exception e){ | |
233 | - e.printStackTrace(); | |
234 | - } | |
235 | - } | |
236 | - } | |
237 | - | |
238 | - break; | |
239 | - } | |
240 | - } | |
241 | - if(!existsDate){ | |
242 | - this.descList.add(orderDate+separateStr+goodsNumber+"件"); | |
243 | - } | |
244 | - } | |
245 | - | |
246 | - public List<List<Object>> getOrderData() { | |
247 | - return orderData; | |
248 | - } | |
249 | - public void setOrderData(List<List<Object>> orderData) { | |
250 | - this.orderData = orderData; | |
251 | - } | |
252 | - | |
253 | - public void addOrderDataItem(List<Object> orderData){ | |
254 | - if(this.orderData == null){ | |
255 | - this.orderData = new ArrayList<List<Object>>(); | |
256 | - } | |
257 | - this.orderData.add(orderData); | |
258 | - } | |
259 | -} |