概述
编写csv导出工具类,为了提高复用性,使用泛型和反射,传入一个任意类型的Vo的List就可以导出.
业务要求导出订单,订单中有多个商品,vo中商品是一个list
刚开始使用superCsv,不知道superCsv怎么处理这种情况,干脆自己写了一个.
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;
}
}
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;
}
}
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 CSV导出所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复