复制代码
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
149package com.platform.utils.excel; import com.platform.utils.RRException; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.List; import java.util.Map; /** * Excel文件导入的基本功能类 * 可导入EXCEL2003 和 EXCEL2007格式。 * * @date 2017年10月28日 13:11:27 */ public class ExcelImport { /** * excel2003扩展名 */ public static final String EXCEL03_EXTENSION = ".xls"; /** * excel2007扩展名 */ public static final String EXCEL07_EXTENSION = ".xlsx"; private ExcelImport() { } /** * 解析EXCEL数据为 List<String[]> * * @param excelFile 要解析的上传EXCEL文件 * @return List<String[]) 行(列) */ public static List<String[]> getExcelData07(MultipartFile excelFile) { List<String[]> resultList = null; if (null == excelFile || excelFile.isEmpty()) { throw new RRException("文件内容为空!"); } Excel2007Reader excel07 = new Excel2007Reader(); try { excel07.process(excelFile.getInputStream(), false); } catch (Exception e) { throw new RRException("excel解析失败!"); } resultList = excel07.getSheetData(0); return resultList; } /** * 解析EXCEL数据为 List<String[]> * * @param excelFile 要解析的上传EXCEL文件 * @return List<String[]) 行(列) */ public static List<String[]> getExcelData03(MultipartFile excelFile) { List<String[]> resultList = null; if (null == excelFile || excelFile.isEmpty()) { throw new RRException("文件内容为空!"); } Excel2003Reader excel03 = new Excel2003Reader();// 实例化excel处理对象 try { excel03.process(excelFile.getInputStream()); } catch (IOException e) { throw new RRException("excel解析失败!"); } resultList = excel03.getSheetData(0); return resultList; } /** * 通过解析MultipartFile对象获取excel内容,并且将其拼装为List<String[]>对象返回 * * @param excelFile * @return * @throws Exception */ public static List<String[]> getExcelData(MultipartFile excelFile) throws RRException { List<String[]> resultList = null; if (!excelFile.isEmpty()) {// 上传的文件不能为空 String excelFileName = excelFile.getOriginalFilename();// 文件名(带后缀) if (excelFileName.toLowerCase().endsWith(EXCEL03_EXTENSION)) {// 如果文件是以.xls为后缀 Excel2003Reader excel03 = new Excel2003Reader();// 实例化excel处理对象 try { excel03.process(excelFile.getInputStream()); } catch (IOException e) { throw new RRException("excel解析失败!"); } resultList = excel03.getSheetData(0); } else if (excelFileName.toLowerCase().endsWith(EXCEL07_EXTENSION)) {// 如果文件是以.xlsx为后缀 Excel2007Reader excel07 = new Excel2007Reader(); try { excel07.process(excelFile.getInputStream(), false); } catch (Exception e) { throw new RRException("excel解析失败!"); } resultList = excel07.getSheetData(0); } } return resultList; } /** * 通过解析MultipartFile对象获取excel内容,并且将其拼装为Map<Integer, List<String[]>>对象返回 * * @param excelFile * @return * @throws Exception */ public static Map<Integer, List<String[]>> getExcelDataAll(MultipartFile excelFile) throws RRException { Map<Integer, List<String[]>> result = null; if (!excelFile.isEmpty()) {// 上传的文件不能为空 String excelFileName = excelFile.getOriginalFilename();// 文件名(带后缀) if (excelFileName.toLowerCase().endsWith(EXCEL03_EXTENSION)) {// 如果文件是以.xls为后缀 Excel2003Reader excel03 = new Excel2003Reader();// 实例化excel处理对象 try { excel03.process(excelFile.getInputStream()); } catch (IOException e) { throw new RRException("excel解析失败!"); } result = excel03.getSheetData(); } else if (excelFileName.toLowerCase().endsWith(EXCEL07_EXTENSION)) {// 如果文件是以.xlsx为后缀 Excel2007Reader excel07 = new Excel2007Reader(); try { excel07.process(excelFile.getInputStream(), true); } catch (Exception e) { throw new RRException("excel解析失败!"); } result = excel07.getSheetData(); } } return result; } }
复制代码
1RRException
复制代码
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
54package com.platform.utils; /** * 自定义异常 * * * @date 2017年11月18日 下午13:13:23 */ public class RRException extends RuntimeException { private static final long serialVersionUID = 1L; private String msg; private int code = 500; public RRException(String msg) { super(msg); this.msg = msg; } public RRException(String msg, Throwable e) { super(msg, e); this.msg = msg; } public RRException(String msg, int code) { super(msg); this.msg = msg; this.code = code; } public RRException(String msg, int code, Throwable e) { super(msg, e); this.msg = msg; this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } }
利用POI上传Excel 出现如果这一行前几个是空的 传到后台的list里面第一个数据是从有数据的列开始的 这样会造成取数据的时候报错
解决办法:
遍历接受的list,获取头的长度,如果两个长度不同,新定义一个长度和头信息相同的数组,遍历这个数组 将没有数据的在数组里面加上一个空的字符串,再讲数组放到一个list里面
调用set方法
set方法是将原来位置上的那个给取代了,并将原来位置上对象的返回。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12for(int i = 1;i<list.size();i++){ if(list.get(i-1).length!=list.get(i).length){ Integer count = list.get(0).length-list.get(i).length; String[] a = new String[count]; for(int j=0;j<a.length;j++){ a[j]=""; } List l = new ArrayList(Arrays.asList(list.get(i))); l.addAll(Arrays.asList(a)); String[] arr = (String[]) l.toArray(new String[list.get(0).length]); list.set(i,arr); }
转载于:https://www.cnblogs.com/NCL--/p/9754390.html
最后
以上就是健康雪糕最近收集整理的关于POI上传Excel的小问题处理的全部内容,更多相关POI上传Excel内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复