EasyExcelUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package com.ydtech.modules.inv.utils;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.util.ListUtils;
  4. import com.alibaba.excel.write.builder.ExcelWriterBuilder;
  5. import com.ydtech.constants.enums.inv.InvPayMethodEnum;
  6. import com.ydtech.modules.inv.model.*;
  7. import com.ydtech.modules.inv.model.excel.InvSettlementSourceExcel;
  8. import com.ydtech.modules.inv.model.excel.InvSourceExcel;
  9. import com.ydtech.modules.inv.service.InvAccountExcelService;
  10. import com.ydtech.modules.inv.utils.excel.InvSettlementSourceDataListener;
  11. import com.ydtech.modules.inv.utils.excel.InvSourceDataListener;
  12. import lombok.SneakyThrows;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Component;
  15. import java.io.File;
  16. import java.io.InputStream;
  17. import java.math.BigDecimal;
  18. import java.text.SimpleDateFormat;
  19. import java.time.LocalDate;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.stream.Collectors;
  25. import static com.ydtech.modules.inv.utils.ZipMultiFile.zipFiles;
  26. /**
  27. * @author jianlong
  28. * @date 2023-12-29 16:46
  29. */
  30. @Component
  31. public class EasyExcelUtils {
  32. @Autowired
  33. private InvAccountExcelService insAccountExcelService;
  34. //通用写入
  35. public static String downExcel(String uploadFilePath, Class clz, List<?> list) {
  36. String file = System.currentTimeMillis() + ".xlsx";
  37. String fileName = uploadFilePath + file;
  38. EasyExcel.write(fileName, clz).sheet("1").doWrite(list);
  39. return file;
  40. }
  41. /**
  42. * 不创建对象的写
  43. */
  44. public static String noModelWrite(String uploadFilePath, List<InvAccountProfit> invAccountProfitList, List<InvAccount> invAccounts, List<InvAccountOp> invAccountOpList) {
  45. // 写法1
  46. String file = System.currentTimeMillis() + ".xlsx";
  47. String fileName = uploadFilePath + file;
  48. // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
  49. EasyExcel.write(fileName).head(head(invAccountProfitList)).sheet("1").doWrite(dataList(invAccounts, invAccountOpList, invAccountProfitList));
  50. return file;
  51. }
  52. private static List<List<String>> head(List<InvAccountProfit> invAccountProfitList) {
  53. List<List<String>> list = new ArrayList<>(4 + invAccountProfitList.size());
  54. List<String> head = ListUtils.newArrayList();
  55. head.add("账户性质");
  56. List<String> head1 = ListUtils.newArrayList();
  57. head1.add("户名");
  58. List<String> head2 = ListUtils.newArrayList();
  59. head2.add("开户银行");
  60. List<String> head3 = ListUtils.newArrayList();
  61. head3.add("银行卡余额");
  62. list.add(0, head);
  63. list.add(1, head1);
  64. list.add(2, head2);
  65. list.add(3, head3);
  66. invAccountProfitList.forEach(invAccountProfit -> {
  67. List<String> head0 = ListUtils.newArrayList();
  68. head0.add(invAccountProfit.getProfitName());
  69. list.add(head0);
  70. });
  71. return list;
  72. }
  73. private static List<List<Object>> dataList(List<InvAccount> invAccounts, List<InvAccountOp> invAccountOpList, List<InvAccountProfit> invAccountProfitList) {
  74. List<List<Object>> list = ListUtils.newArrayList();
  75. invAccounts.forEach(invAccount -> {
  76. invAccount.getInvAccountCardList().forEach(invAccountCard -> {
  77. List<Object> data = ListUtils.newArrayList();
  78. if (invAccountCard.getBankCardNum() != null) {
  79. data.add(invAccount.getOuterFlag().equals("0") ? "内部账户" : "外部账户");
  80. data.add(invAccount.getAccountName());
  81. data.add(invAccountCard.getBankName());
  82. data.add(invAccountCard.getBalance());
  83. invAccountProfitList.forEach(invAccountProfitList1 -> {
  84. String a = "0";
  85. for (InvAccountOp invAccountProfit : invAccountOpList) {
  86. if (invAccountProfitList1.getProfitEng().equals(invAccountProfit.getProfit()) && invAccountCard.getBankCardNum().equals(invAccountProfit.getCardNum())) {
  87. a = new BigDecimal(a).add(new BigDecimal(invAccountProfit.getMoney())).toString();
  88. }
  89. }
  90. data.add(a);
  91. });
  92. list.add(data);
  93. }
  94. });
  95. });
  96. return list;
  97. }
  98. /**
  99. * 不创建对象的写
  100. */
  101. public static String noModelOperateWrite(String uploadFilePath, List<InvAccountProfit> invAccountProfitList, List<InvAccountOperate> invAccountOperates) {
  102. // 写法1
  103. String file = System.currentTimeMillis() + ".xlsx";
  104. String fileName = uploadFilePath + file;
  105. // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
  106. EasyExcel.write(fileName).head(operateHead(invAccountProfitList)).sheet("1").doWrite(operatedataList(invAccountOperates, invAccountProfitList));
  107. return file;
  108. }
  109. private static List<List<String>> operateHead(List<InvAccountProfit> invAccountProfitList) {
  110. List<List<String>> list = new ArrayList<>(7 + invAccountProfitList.size());
  111. List<String> head = ListUtils.newArrayList();
  112. head.add("支出时间");
  113. List<String> head1 = ListUtils.newArrayList();
  114. head1.add("部门");
  115. List<String> head2 = ListUtils.newArrayList();
  116. head2.add("支出方");
  117. List<String> head3 = ListUtils.newArrayList();
  118. head3.add("收入方");
  119. List<String> head4 = ListUtils.newArrayList();
  120. head4.add("支出项目");
  121. List<String> head5 = ListUtils.newArrayList();
  122. head5.add("分项");
  123. List<String> head6 = ListUtils.newArrayList();
  124. head6.add("支出金额");
  125. List<String> head7 = ListUtils.newArrayList();
  126. head7.add("转账类型");
  127. list.add(0, head);
  128. list.add(1, head1);
  129. list.add(2, head2);
  130. list.add(3, head3);
  131. list.add(4, head4);
  132. list.add(5, head5);
  133. list.add(6, head6);
  134. list.add(7, head7);
  135. invAccountProfitList.forEach(invAccountProfit -> {
  136. List<String> head0 = ListUtils.newArrayList();
  137. head0.add(invAccountProfit.getProfitName());
  138. list.add(head0);
  139. });
  140. return list;
  141. }
  142. private static List<List<Object>> operatedataList(List<InvAccountOperate> invAccountOperates, List<InvAccountProfit> invAccountProfitList) {
  143. List<List<Object>> list = ListUtils.newArrayList();
  144. invAccountOperates.forEach(invAccountOperate -> {
  145. List<Object> data = ListUtils.newArrayList();
  146. data.add(invAccountOperate.getCreateTime());
  147. data.add(invAccountOperate.getApplySector());
  148. data.add(invAccountOperate.getFundFlow().substring(0, invAccountOperate.getFundFlow().indexOf("→")));
  149. data.add(invAccountOperate.getFundFlow().substring(invAccountOperate.getFundFlow().indexOf("→") + 1));
  150. data.add(invAccountOperate.getFundUse());
  151. data.add(invAccountOperate.getFundFen());
  152. data.add(invAccountOperate.getApplyAmount());
  153. data.add(invAccountOperate.getRevenueOutlay().equals("0") ? "收入" : "支出");
  154. invAccountProfitList.forEach(invAccountProfit -> {
  155. if (invAccountOperate.getOutProfit() != null) {
  156. for (Map.Entry<String, Object> entry : invAccountOperate.getOutProfit().entrySet()) {
  157. if (entry.getKey().equals(invAccountProfit.getProfitEng())) {
  158. data.add(entry.getValue());
  159. }
  160. }
  161. }
  162. if (invAccountOperate.getEntProfit() != null) {
  163. for (Map.Entry<String, Object> entry : invAccountOperate.getEntProfit().entrySet()) {
  164. if (entry.getKey().equals(invAccountProfit.getProfitEng())) {
  165. data.add(entry.getValue());
  166. }
  167. }
  168. }
  169. });
  170. list.add(data);
  171. });
  172. return list;
  173. }
  174. public static String downExcel(String uploadFilePath, String fileName, Class clz, List<?> list) {
  175. String file = fileName + System.currentTimeMillis() + ".xlsx";
  176. String filePath = uploadFilePath + file;
  177. EasyExcel.write(filePath, clz).sheet("1").doWrite(list);
  178. return file;
  179. }
  180. public static List<InvSourceExcel> readInvSourceExcel(String uploadFilePath, Class clz) {
  181. // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
  182. // List<InvSourceExcel> list = new ArrayList<>();
  183. List<InvSourceExcel> list = EasyExcel.read(uploadFilePath, clz, new InvSourceDataListener())
  184. .sheet()
  185. .sheetName("来源表")
  186. .doReadSync();
  187. return list;
  188. }
  189. public static List<InvSettlementSourceExcel> readInvSettlementSourceExcelExcel(String uploadFilePath, Class clz) {
  190. // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
  191. List<InvSettlementSourceExcel> list = EasyExcel.read(uploadFilePath, clz, new InvSettlementSourceDataListener())
  192. .sheet()
  193. .sheetName("结算来源表")
  194. .doReadSync();
  195. return list;
  196. }
  197. /**
  198. * 导出资金申请模板
  199. */
  200. @SneakyThrows
  201. public String downAccountExcel(String uploadFilePath, List<InvAccountExcel> invAccountExcel, List<InvAccountProfit> invAccountProfit) {
  202. // 加载模板
  203. InputStream inputStream = getClass().getResourceAsStream("/template/资金申请单模板.xlsx");
  204. List<InvAccountExcel> list = new ArrayList<>();
  205. // 获取工作表并填充
  206. for (InvAccountExcel excel : invAccountExcel) {
  207. // 写入文件
  208. Date date = new Date();
  209. String file = date.getTime() + ".xlsx";
  210. String fileName = uploadFilePath + file;
  211. excel.setExcelApplyTime(new SimpleDateFormat("yyyy-MM-dd").format(excel.getApplyTime()));
  212. String payMethod = excel.getPayMethod();
  213. String[] split = payMethod.split(",");
  214. StringBuilder a = new StringBuilder();
  215. for (String i : split) {
  216. a.append(InvPayMethodEnum.getNameByCode(i)).append(",");
  217. }
  218. StringBuilder stringBuffer = new StringBuilder();
  219. Map<String, List<InvAccountProfit>> collect = invAccountProfit.stream().collect(
  220. Collectors.groupingBy(
  221. InvAccountProfit::getProfitEng
  222. ));
  223. for (Map.Entry<String, Object> entry : excel.getOutProfit().entrySet()) {
  224. if (Integer.parseInt(entry.getValue().toString()) != 0) {
  225. stringBuffer.append(collect.get(entry.getKey()).get(0).getProfitName()).append(" ").append(entry.getValue()).append("%");
  226. }
  227. }
  228. stringBuffer.append("→");
  229. for (Map.Entry<String, Object> entry : excel.getEntProfit().entrySet()) {
  230. if (Integer.parseInt(entry.getValue().toString()) != 0) {
  231. stringBuffer.append(collect.get(entry.getKey()).get(0).getProfitName()).append(" ").append(entry.getValue()).append("%");
  232. }
  233. }
  234. excel.setPlate(stringBuffer.toString());
  235. excel.setPayMethod(a.toString());
  236. // 生成工作簿对象
  237. ExcelWriterBuilder workBookWriter = EasyExcel.write(fileName, InvAccountExcel.class)
  238. .withTemplate(inputStream);
  239. workBookWriter.sheet().doFill(excel);
  240. excel.setUplodePath(file);
  241. excel.setPayMethod(payMethod);
  242. list.add(excel);
  243. }
  244. insAccountExcelService.updateBatchById(list);
  245. File[] srcFiles = new File[list.size()];
  246. for (int i = 0; i < list.size(); i++) {
  247. srcFiles[i] = new File(uploadFilePath + list.get(i).getUplodePath());
  248. }
  249. String a = LocalDate.now() + ".zip";
  250. File zipFile = new File(uploadFilePath + a);
  251. // 调用压缩方法
  252. zipFiles(srcFiles, zipFile);
  253. return a;
  254. }
  255. }