EasyExcelUtils.java 14 KB

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