我是靠谱客的博主 优秀老虎,这篇文章主要介绍JAVA CSV导出,现在分享给大家,希望可以做个参考。

编写csv导出工具类,为了提高复用性,使用泛型和反射,传入一个任意类型的Vo的List就可以导出.

业务要求导出订单,订单中有多个商品,vo中商品是一个list

刚开始使用superCsv,不知道superCsv怎么处理这种情况,干脆自己写了一个.

复制代码
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
import java.math.BigDecimal; import java.util.Date; import com.tunynet.groupmall.base.utils.DateUtil; import com.tunynet.groupmall.base.utils.csvexportutil.CellList; /** * @author wanrq * @version 0.5 * @date Created in 2019/12/5 15:04 * @description 导出订单表(有快递的订单) * @modified By */ public class CourieredOrderVo { /** 接龙号 **/ private Long id; /** 微信昵称 **/ private String weChatName; /** 每个商品类型的个数 **/ private CellList goodsNumberList; /** 订单金额 **/ private BigDecimal price; /** 在线支付金额 **/ private BigDecimal onlinePrice; /** 在线退款金额 **/ private BigDecimal refundPrice; /** 用户备注 **/ private String userRemarks; /** 管理备注 **/ private String adminRemarks; /** 下单时间 **/ private Date createDate; /** 订单状态 **/ private String status; /** 联系人 **/ private String Contact; /** 联系电话 **/ private String phone; /** 省 **/ private String province; /** 市 **/ private String city; /** 区(县) **/ private String district; /** 详细地址 **/ private String address; /** 物流公司 **/ private String LogisticsCompany; /** 物流单号 **/ private String LogisticsNumber; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public CellList getGoodsNumberList() { return goodsNumberList; } public void setGoodsNumberList(CellList goodsNumberList) { this.goodsNumberList = goodsNumberList; } public String getWeChatName() { return weChatName; } public void setWeChatName(String weChatName) { this.weChatName = weChatName; } public String getCreateDate() { return " "+DateUtil.parseDateToStr(createDate,DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI); } public void setCreateDate(Date createDate) { this.createDate = createDate; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public BigDecimal getOnlinePrice() { return onlinePrice; } public void setOnlinePrice(BigDecimal onlinePrice) { this.onlinePrice = onlinePrice; } public BigDecimal getRefundPrice() { return refundPrice; } public void setRefundPrice(BigDecimal refundPrice) { this.refundPrice = refundPrice; } public String getUserRemarks() { return userRemarks; } public void setUserRemarks(String userRemarks) { this.userRemarks = userRemarks; } public String getAdminRemarks() { return adminRemarks; } public void setAdminRemarks(String adminRemarks) { this.adminRemarks = adminRemarks; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getContact() { return Contact; } public void setContact(String contact) { Contact = contact; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getLogisticsCompany() { return LogisticsCompany; } public void setLogisticsCompany(String logisticsCompany) { LogisticsCompany = logisticsCompany; } public String getLogisticsNumber() { return LogisticsNumber; } public void setLogisticsNumber(String logisticsNumber) { LogisticsNumber = logisticsNumber; } }
复制代码
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
import java.util.Map; /** * @author wanrq * @version 0.5 * @date Created in 2019/12/6 16:34 * @description 泛型导出excel,vo里有list,需要确定list里有多少个元素, * 每个元素对应位置,这样有null也不会显示错位 * @modified By */ public class CellList { /** 每个值对应的单元格位置 **/ private Map<Integer,Object> map; /** 一共有多少單元格 **/ private Integer size; public CellList() { } public CellList(Integer size) { this.size = size; } public CellList(Map<Integer, Object> map, Integer size) { this.map = map; this.size = size; } public Map<Integer, Object> getMap() { return map; } public void setMap(Map<Integer, Object> map) { this.map = map; } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } }
复制代码
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
import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * @author wanrq * @version 0.5 * @date Created in 2019/12/5 17:47 * @description 传入vo导出csv文件,通过getter自定义显示效果, list为空显示有bug处理待加强 * @modified By */ public class CsvWrite { private PrintWriter printWriter; public CsvWrite(PrintWriter printWriter) { this.printWriter = printWriter; } /** * @author wanrq@tunynet.com * @date Created in 2019/12/9 11:04 * @description csv文件编码 * @version 0.5 * @modified By * @param o 要编码的一个单元格的数据 * @return String */ private String encode(Object o) { if (null == o) { return ""; } String cell = o.toString(); if("".equals(cell)){ return ""; } StringBuilder sb = new StringBuilder(); // 避免公式 if ('=' == cell.charAt(0)) { sb.append(" "); } // 有,和n的单元格需要引号引起来 boolean needQMarks = false; char[] cellChars = cell.toCharArray(); for (char cellChar : cellChars) { switch (cellChar) { case ',': case 'n': needQMarks = true; sb.append(cellChar); break; case '"': needQMarks = true; sb.append(""""); break; default: sb.append(cellChar); break; } } String encodeCell = sb.toString(); if (needQMarks) { encodeCell = """ + encodeCell + """; } return encodeCell; } /** * @author wanrq * @date Created in 2019/12/9 11:05 * @description 包装成一行csv文件数据 * @version 0.5 * @modified By * @param vo 文件传送类对象 * @return String */ private <T> String rowContent(T vo) { String[] fieldNames = Reflection.getFieldNames(vo); List<String> cellList = new LinkedList<>(); for (String fieldName : fieldNames) { String cell = null; Object valueByName = null; try { // 通过getter获取单元格数据,用户要自定义单元格显示可以修改vo的getter返回 valueByName = Reflection.getFieldValueByName(fieldName, vo); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { // 部分私有变量没有getter,获取不到不进行任何操作 continue; } if (valueByName instanceof CellList) { // 如果是单元格列表,导出多个单元格 CellList cells = (CellList) valueByName; Map<Integer, Object> map = cells.getMap(); Integer size = cells.getSize(); for (int i = 0; i < size; i++) { Object o; if(null == map){ o = ""; }else{ o=map.get(i); } cell = encode(o); cellList.add(cell); } } else { // 普通值调用toString后编码 cell = encode(valueByName); cellList.add(cell); } } String row = String.join(",", cellList); return row; } /** * @author wanrq * @date Created in 2019/12/9 11:07 * @description 写一行 * @version 0.5 * @modified By * @param vo VO */ public <T> void writeRow(T vo) { printWriter.println(rowContent(vo)); } /** * @author wanrq * @date Created in 2019/12/9 11:07 * @description 写多行 * @version 0.5 * @modified By * @param voList VO */ public <T> void writeAll(List<T> voList) { for (T vo : voList) { writeRow(vo); } } /** * @author wanrq * @date Created in 2019/12/9 11:07 * @description 写表头 * @version 0.5 * @modified By * @param titles 表头 */ public <T> void writeTitle(T[] titles) { List<String> encodeList = new LinkedList<>(); for (T title : titles) { encodeList.add(encode(title.toString())); } String headerContent = String.join(",", encodeList); printWriter.println(headerContent); } }

 

最后

以上就是优秀老虎最近收集整理的关于JAVA CSV导出的全部内容,更多相关JAVA内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部