-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWriteTest.java
96 lines (87 loc) · 4.35 KB
/
WriteTest.java
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
import bean.Payment;
import bean.TradeOrder;
import com.github.liuhuagui.gridexcel.GridExcel;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import util.MockData;
import util.PaymentWays;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.function.Function;
/**
* 使用MockData从类路径下的序列化对象文件获取模拟数据List<String>
* @author KaiKang
*/
public class WriteTest {
private void putPaymentsFunction(LinkedHashMap<String, Function<TradeOrder, Object>> writeFunctionMap, final int index) {
writeFunctionMap.put("第" + (1 + index) + "次支付订单号", to -> {
Payment payment = to.getPayments().get(index);
return payment.getPaymentId();
});
writeFunctionMap.put("第" + (1 + index) + "次支付金额", to -> {
Payment payment = to.getPayments().get(index);
return payment.getPaymentAmount();
});
writeFunctionMap.put("第" + (1 + index) + "次支付方式", to -> {
Payment payment = to.getPayments().get(index);
return PaymentWays.bankInstName(payment.getPaymentWay());
});
writeFunctionMap.put("第" + (1 + index) + "次支付状态", to -> {
Payment payment = to.getPayments().get(index);
return payment.getPaymentStatus() == 1 ? "支付完成" : "支付中";
});
}
public LinkedHashMap<String,Function<TradeOrder,Object>> writeFunctionMap(){
LinkedHashMap<String, Function<TradeOrder, Object>> writeFunctionMap = new LinkedHashMap<>();
writeFunctionMap.put("订单号", to -> to.getTradeOrderId());
writeFunctionMap.put("顾问编号", to -> to.getConsultantId());
writeFunctionMap.put("顾问姓名", to -> to.getConsultant().getConsultantName());
writeFunctionMap.put("顾问手机号", to -> to.getConsultant().getConsultantPhone());
writeFunctionMap.put("客户编号", to -> to.getCustomerId());
writeFunctionMap.put("客户名称", to -> to.getCustomer().getCustomerName());
writeFunctionMap.put("客户手机号", to -> to.getCustomer().getCustomerPhone());
writeFunctionMap.put("服务商编号", to -> to.getProviderId());
writeFunctionMap.put("服务商名称", to -> to.getProvider().getProviderName());
writeFunctionMap.put("服务商注册手机号", to -> to.getProvider().getLegalPersonPhone());
writeFunctionMap.put("服务编号", to -> to.getServiceId());
writeFunctionMap.put("服务名称", to -> to.getService().getServiceName());
writeFunctionMap.put("单价", to -> to.getActualUnitPrice());
writeFunctionMap.put("数量", to -> to.getServiceCount());
writeFunctionMap.put("总额", to -> to.getActualSumPrice());
writeFunctionMap.put("支付比例", to -> to.getPaymentRatio());
writeFunctionMap.put("支付次数", to -> to.getPaymentTimes());
writeFunctionMap.put("支付状态", to -> {
Byte status = to.getPaymentStatus();
return status == null ? "未支付" : ("第" + Math.abs(status) + "次支付" + (status > 0 ? "完成" : "中"));
});
// 为支付订单添加处理函数
putPaymentsFunction(writeFunctionMap, 0);
putPaymentsFunction(writeFunctionMap, 1);
putPaymentsFunction(writeFunctionMap, 2);
return writeFunctionMap;
}
/**
* 使用UserModel写出数据到Excel
* @throws Exception
*/
@Test
public void writeExcelByUserModel() throws Exception {
GridExcel.writeByUserModel(TradeOrder.class)
.head(writeFunctionMap())//对象字段到Excel列的映射
.createSheet()
.process(MockData.data())//模拟数据。在这里设置业务数据集合。
.write(FileUtils.openOutputStream(new File("/excel/test.xlsx")));
}
/**
* 使用Streaming UserModel写出数据到Excel
* @throws Exception
*/
@Test
public void writeExcelByStreaming() throws Exception {
GridExcel.writeByStreaming(TradeOrder.class)
.head(writeFunctionMap())//对象字段到Excel列的映射
.createSheet()
.process(MockData.data())//模拟数据。在这里设置业务数据集合。
.write(FileUtils.openOutputStream(new File("/excel/test.xlsx")));
}
}