java doCsvExport

/**
* Export records to csv file

* @param response  Http response
* @param items     List of item that implements on CsvExportable interface
* @throws IOException
*/
public static void doCsvExport(HttpServletResponse response, List<CsvExportable> items) throws IOException {
// reset response buffer and header information
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=export.csv");


PrintWriter writer = null;
try {
writer = new PrintWriter(response.getOutputStream());


for (int i = 0; i < items.size(); i++) {
CsvExportable item =  items.get(i);
if (i == 0) {
writer.println(item.getCsvTitle());
}
writer.println(item.toCsvString());

if ((i+1) % 100 == 0) {
writer.flush();
}
}
} catch (IOException e) {
DevLog.error("Exception happens when export results to csv", e);
throw e;
} finally {
if (writer != null) {
writer.close();
}
}

}



public interface CsvExportable {


public String getCsvTitle();

public String toCsvString();
}



public class RealEstateCustomerDTO  implements CsvExportable {

@Override
public String toString() {
return "RealEstateCustomerDTO [realEstateCustomerId="
+ realEstateCustomerId + ", clientNo=" + clientNo + ", name="
+ name + ", sex=" + sex + ", phone=" + phone
+ ", email=" + email + ", telephone=" + telephone
+ ", address=" + address + ", birthday=" + birthday
+ ", profession=" + profession + ", wechatNo=" + wechatNo
+ ", bankInfo=" + bankInfo + ", peopleId=" + peopleId
+ ", status=" + status + ", buyAmount=" + buyAmount
+ ", userBought=" + userBought + "]";
}


@Override
public int compareTo(RealEstateCustomerDTO that) {
if (that == null) {
return 1;
}

int result = getStatus().compareTo(that.getStatus());
if (result != 0) {
return result;
}

result = getClientNo().compareTo(that.getClientNo());
return result;
}

@Override
public int hashCode() {
int hashcode = super.hashCode();
if (StringUtils.isNotEmpty(getStatus())) {
hashcode += 31 * getStatus().hashCode();
}

if (StringUtils.isNotEmpty(getClientNo())) {
hashcode += 31 * getClientNo().hashCode();
}

return hashcode;
}


@Override
public String getCsvTitle() {
return "一账通ID,客户姓名,性别,手机号,邮箱,座机号,地址,生日,职业,微信号,银行信息,身份证号码,平台,备注,状态,创建者,创建时间,修改者,修改时间";
}


@Override
public String toCsvString() {
StringBuilder builder = new StringBuilder();
builder.append(getClientNo()).append(",")
.append(getName()).append(",")
.append("0".equals(getSex()) ? "女" : "1".equals(getSex()) ? "男" : "" ).append(",")
.append(getPhone()).append(",")
.append(getEmail()).append(",")
.append(getTelephone()).append(",")
.append(getAddress()).append(",")
.append(getBirthday()).append(",")
.append(getProfession()).append(",")
.append(getWechatNo()).append(",")
.append(getBankInfo()).append(",")
.append(getPeopleId()).append(",")
.append(getPlatForm()).append(",")
.append(getRemark()).append(",")
.append("01".equals(getStatus()) ? "正常" : "已删除").append(",")
.append(getCreatedBy()).append(",")
.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(getCreatedDate())).append(",")
.append(getUpdatedBy()).append(",")
.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(getUpdatedDate()));
return builder.toString();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值