TempExcel.java
7.45 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.taover.util.UtilExcel;
public class TempExcel {
public static void main(String[] args){
//dealExcel();
readExcel();
}
private static void readExcel() {
File file = new File("C:\\Users\\Administrator\\Desktop\\异常Excel\\1_2020-03-08-14h55m00s-面包仓-订单回传数据(2)(1).xlsx");
try {
Map<String, List<List<Object>>> readExcelAllSheetMap = UtilExcel.readExcelAllSheetMap(file.getAbsolutePath());
System.out.println("12");
} catch (Exception e) {
e.printStackTrace();
}
}
public static final String SEPARATE_CONSIGNEE_MOBILE = "__";
public static final int EXCEL_TIANMAO_CONSIGNEE_INDEX = 14;
public static final int EXCEL_TIANMAO_MOBILE_INDEX = 18;
public static final int EXCEL_TIANMAO_ORDERDATE_INDEX = 20;
public static final int EXCEL_TIANMAO_GOODSNUMBER_INDEX = 26;
public static final int EXCEL_JD_CONSIGNEE_INDEX = 14;
public static final int EXCEL_JD_MOBILE_INDEX = 16;
public static final int EXCEL_JD_ORDERDATE_INDEX = 24;
public static final int EXCEL_JD_GOODSNUMBER_INDEX = 3;
public static void dealExcel(){
try {
List<List<Object>> tianmaoData = UtilExcel.readExcel("D:\\tempexcel\\tianmao.xlsx");
List<List<Object>> jdData = UtilExcel.readExcel("D:\\tempexcel\\jd.xls");
tianmaoData.remove(0);
jdData.remove(0);
//按下单人及下单手机号分类
Map<String, List<List<Object>>> separateData = new HashMap<String, List<List<Object>>>();
for(int i=0; i<tianmaoData.size(); ++i){
List<Object> tempData = tianmaoData.get(i);
injectMap(separateData, getNameAndMobileByTianmao(tempData), tempData);
}
for(int i=0; i<jdData.size(); ++i){
List<Object> tempData = jdData.get(i);
injectMap(separateData, getNameAndMobileByJd(tempData), tempData);
}
//分析下单分类
List<SeparateAnalysisInfo> analysisData = new ArrayList<SeparateAnalysisInfo>();
Iterator<String> keyIter = separateData.keySet().iterator();
while(keyIter.hasNext()){
String keyName = keyIter.next();
List<List<Object>> keyData = separateData.get(keyName);
if(keyData.size() <= 1){
continue;
}
String[] keyNameSep = keyName.split(TempExcel.SEPARATE_CONSIGNEE_MOBILE);
if(keyNameSep.length == 4){
SeparateAnalysisInfo anaInfo = new SeparateAnalysisInfo(keyNameSep[0], keyNameSep[1], keyData, Integer.valueOf(keyNameSep[2]), Integer.valueOf(keyNameSep[3]));
if(anaInfo.getDescList().size() > 1){
analysisData.add(anaInfo);
}
}
}
//打印信息,保存信息
List<List<Object>> resultInfo = new ArrayList<List<Object>>();
List<Object> blankRow = new ArrayList<Object>();
for(int i=0; i<analysisData.size(); ++i){
SeparateAnalysisInfo item = analysisData.get(i);
//添加空白间距行
resultInfo.add(blankRow);
resultInfo.add(blankRow);
//用户信息列
List<Object> userInfo = new ArrayList<Object>();
userInfo.add("收货人:"+item.getConsignee());
userInfo.add("电话号:"+item.getMobile());
resultInfo.add(userInfo);
//下单信息列
List<Object> descInfo = new ArrayList<Object>();
descInfo.add("下单简述");
for(int j=0; j<item.getDescList().size(); ++j){
descInfo.add(item.getDescList().get(j));
}
resultInfo.add(descInfo);
//订单详情
resultInfo.addAll(item.getOrderData());
}
UtilExcel.saveExcel("处理结果", resultInfo, "D:\\tempexcel\\resultinfo.xlsx");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void injectMap(Map<String, List<List<Object>>> separateData, String keyName, List<Object> keyValue){
if(separateData.containsKey(keyName)){
separateData.get(keyName).add(keyValue);
}else{
List<List<Object>> tempData = new ArrayList<List<Object>>();
tempData.add(keyValue);
separateData.put(keyName, tempData);
}
}
public static String getNameAndMobileByTianmao(List<Object> data){
String name = data.get(EXCEL_TIANMAO_CONSIGNEE_INDEX).toString();
String mobile = data.get(EXCEL_TIANMAO_MOBILE_INDEX).toString();
return name+SEPARATE_CONSIGNEE_MOBILE+mobile+SEPARATE_CONSIGNEE_MOBILE+EXCEL_TIANMAO_ORDERDATE_INDEX+SEPARATE_CONSIGNEE_MOBILE+EXCEL_TIANMAO_GOODSNUMBER_INDEX;
}
public static String getNameAndMobileByJd(List<Object> data){
String name = data.get(EXCEL_JD_CONSIGNEE_INDEX).toString();
String mobile = data.get(EXCEL_JD_MOBILE_INDEX).toString();
return name+SEPARATE_CONSIGNEE_MOBILE+mobile+SEPARATE_CONSIGNEE_MOBILE+EXCEL_TIANMAO_ORDERDATE_INDEX+SEPARATE_CONSIGNEE_MOBILE+EXCEL_TIANMAO_GOODSNUMBER_INDEX;
}
}
class SeparateAnalysisInfo{
private String consignee;
private String mobile;
private List<String> descList;
private List<List<Object>> orderData;
public SeparateAnalysisInfo(String consignee, String mobile, List<List<Object>> data, int excelOrderDateIndex, int excelGoodsNumberIndex){
this.consignee = consignee;
this.mobile = mobile;
this.orderData = data;
for(int i=0; i<data.size(); ++i){
List<Object> dataItem = data.get(i);
if(dataItem.get(excelOrderDateIndex) == null){
System.out.println("没有下单日期信息:"+this.consignee+"__"+this.mobile);
continue;
}
if(dataItem.get(excelGoodsNumberIndex) == null){
System.out.println("没有商品数量信息:"+this.consignee+"__"+this.mobile);
continue;
}
String orderDate = dataItem.get(excelOrderDateIndex).toString();
if(orderDate.contains("-")){
orderDate = orderDate.substring(0, 10);
}else{
String[] dateInfo = orderDate.split(" ");
if(dateInfo.length > 0){
orderDate = dateInfo[0].replace("/", "-");
}
}
String goodsNumber = dataItem.get(excelGoodsNumberIndex).toString();
this.addDescListItem(orderDate, goodsNumber);
}
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public List<String> getDescList() {
return descList;
}
public void setDescList(List<String> descList) {
this.descList = descList;
}
public void addDescListItem(String orderDate, String goodsNumber){
if(this.descList == null){
this.descList = new ArrayList<String>();
}
String separateStr = "__";
//检查是否有重复日期的情况
boolean existsDate = false;
for(int i=0; i<this.descList.size(); ++i){
String tempDesc = this.descList.get(i);
if(tempDesc.startsWith(orderDate)){
existsDate = true;
String[] tempData = tempDesc.split(separateStr);
if(tempData.length > 1){
String[] tempDataGoods = tempData[1].split("件");
if(tempDataGoods.length > 0){
try{
int totalNumber = Integer.valueOf(tempDataGoods[0]).intValue()+Integer.valueOf(goodsNumber).intValue();
this.descList.set(i, orderDate+separateStr+totalNumber+"件");
}catch(Exception e){
e.printStackTrace();
}
}
}
break;
}
}
if(!existsDate){
this.descList.add(orderDate+separateStr+goodsNumber+"件");
}
}
public List<List<Object>> getOrderData() {
return orderData;
}
public void setOrderData(List<List<Object>> orderData) {
this.orderData = orderData;
}
public void addOrderDataItem(List<Object> orderData){
if(this.orderData == null){
this.orderData = new ArrayList<List<Object>>();
}
this.orderData.add(orderData);
}
}