* 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();
}
}