• java设计模式之策略模式


    什么是策略模式?为什么要使用策略模式?

    策略模式属于行为型模式,行为型用来识别对象之间的常用交流模式并加以实现。使用策略模式的好处有三点:

    1,优化代码逻辑结构性,可以减少分支判断。

    2,增加代码程序拓展和移植。

    3,增加代码复用和业务逻辑分离和优化。

    当然也有缺点的,缺点就是得提前约定好规范,从而触发这样的业务场景。

    项目应用场景:工作流,支付,聚合支付,日志收集,当然还有很多很多,只有你需要多渠道操作业务这个时候不知道怎么使用设计模式的时候,这个时候代理模式是首先,他既能分离代码和实现代码的拓展。

    这里给大家一个我之前做的一个下载,采用统一下载,但是不同的业务模板其实是不一样的,这样就会导致会写很多冗余代码,我每创建一个导出的下载就会去更新模板和方法,这个是很早就做的导出excel的功能,代码还是有相对优化的余地。

    使用此模式注意的俩点:

    1,必须使用上下文获取对象的实现。

    2,必须指定优先注入的key  @Primary  这个注解必须加,不然会报空指针异常,找不到实现类,上下文获取对象必须有个对象加载。

    1. package com.bootdo.common.config;
    2. import org.springframework.beans.BeansException;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.ApplicationContextAware;
    5. import org.springframework.stereotype.Component;
    6. /**
    7. * @author yang
    8. */
    9. @Component
    10. public class ApplicationContextProvider implements ApplicationContextAware {
    11. /**
    12. * 上下文对象实例
    13. */
    14. private static ApplicationContext APPLICATION_CONTENT;
    15. @Override
    16. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    17. APPLICATION_CONTENT = applicationContext;
    18. }
    19. /**
    20. * 获取applicationContext
    21. *
    22. * @return ApplicationContext
    23. */
    24. public static ApplicationContext getApplicationContext() {
    25. return APPLICATION_CONTENT;
    26. }
    27. /**
    28. * 通过name获取 Bean.
    29. *
    30. * @param name name
    31. * @return Object
    32. */
    33. public static Object getBean(String name) {
    34. return getApplicationContext().getBean(name);
    35. }
    36. /**
    37. * 通过class获取Bean.
    38. *
    39. * @param clazz 类名称
    40. * @param 实例化泛型
    41. * @return T
    42. */
    43. public static T getBean(Class clazz) {
    44. return getApplicationContext().getBean(clazz);
    45. }
    46. /**
    47. * 通过name,以及Clazz返回指定的Bean
    48. *
    49. * @param name 服务别称
    50. * @param clazz 类名
    51. * @param 实例化泛型
    52. * @return T
    53. */
    54. public static T getBean(String name, Class clazz) {
    55. return getApplicationContext().getBean(name, clazz);
    56. }
    57. }

    类图:

    注解方式实现:

    定义一个抽象类:

    1. import javax.servlet.http.HttpServletResponse;
    2. import java.util.Map;
    3. public interface DownExcelService {
    4. void downexcel(HttpServletResponse response, Map params) throws Exception;
    5. }

     定义一个常量方式:

    1. package com.bootdo.common.config;
    2. /**
    3. * Excel 下载接口
    4. *
    5. * @author yangchang
    6. */
    7. public interface WorkflowConfigCodeConstants {
    8. /**
    9. * 工人总账目
    10. */
    11. String ADVANCE_SUM_DOWNXECEL = "advacnsum_downexcel";
    12. /**
    13. * 工人借支
    14. */
    15. String ADVANCE_DOWNXECEL = "advacn_downexcel";
    16. /**
    17. * 材料
    18. */
    19. String METATLE_DOWNXECEL = "metail_downexcel";
    20. /**
    21. * 公司生活费
    22. */
    23. String COMPANYREMITTANCE_DOWNXECEL = "companyremittance_downexcel";
    24. /**
    25. * 突击信息
    26. */
    27. String ASSAULT_DOWNXECEL = "assault_downexcel";
    28. }

    多个实现类:

     默认实现类:

    1. package com.bootdo.common.controller.detail;
    2. import com.bootdo.common.service.DownExcelService;
    3. import javax.servlet.http.HttpServletResponse;
    4. import java.util.Map;
    5. public abstract class AbstractDetaliCallBackServiceImpl implements DownExcelService {
    6. @Override
    7. public void downexcel(HttpServletResponse response, Map params) throws Exception {
    8. }
    9. }

    突击实现类:

    1. package com.bootdo.common.downInterface;
    2. import com.bootdo.common.config.Constant;
    3. import com.bootdo.common.config.WorkflowConfigCodeConstants;
    4. import com.bootdo.common.domain.AssaultDO;
    5. import com.bootdo.common.service.AssaultService;
    6. import com.bootdo.common.utils.CommonMethod;
    7. import org.apache.commons.lang3.ObjectUtils;
    8. import org.apache.commons.lang3.StringUtils;
    9. import org.springframework.beans.factory.annotation.Autowired;
    10. import org.springframework.context.annotation.Primary;
    11. import org.springframework.stereotype.Service;
    12. import javax.servlet.http.HttpServletResponse;
    13. import java.util.HashMap;
    14. import java.util.List;
    15. import java.util.Map;
    16. @Service(WorkflowConfigCodeConstants.ASSAULT_DOWNXECEL)
    17. public class AssaultDownExcelServiceImpl extends AbstractExcelCallBackServiceImpl {
    18. @Autowired
    19. private AssaultService assaultService;
    20. @Override
    21. public void downexcel(HttpServletResponse response, Map params) throws Exception {
    22. String date = params.get("date");
    23. String name = params.get("name");
    24. String proparentId = params.get("proparentId");
    25. Map map = new HashMap();
    26. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("date"), ""))) {
    27. String[] dateBteetn = CommonMethod.getDate(params.get("date").toString());
    28. map.put("startDate" , dateBteetn[0]);
    29. map.put("stopDate" , dateBteetn[1]);
    30. }
    31. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("name"), ""))) {
    32. map.put("name" , params.get("name").toString());
    33. }
    34. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("proparentId"), ""))) {
    35. map.put("proparentId" , params.get("proparentId").toString());
    36. }
    37. String typeNames = CommonMethod.typeNameExcel(date, name, proparentId);
    38. List list = assaultService.list(map);
    39. CommonMethod.downexcel(response, list, Constant.EXCELE_ASSAULT_STATUS, typeNames, 7);
    40. }
    41. }

    公司生活费实现类:

    1. package com.bootdo.common.downInterface;
    2. import com.bootdo.common.config.Constant;
    3. import com.bootdo.common.config.WorkflowConfigCodeConstants;
    4. import com.bootdo.common.domain.CompanyremittanceDO;
    5. import com.bootdo.common.service.CompanyremittanceService;
    6. import com.bootdo.common.utils.CommonMethod;
    7. import org.apache.commons.lang3.ObjectUtils;
    8. import org.apache.commons.lang3.StringUtils;
    9. import org.springframework.beans.factory.annotation.Autowired;
    10. import org.springframework.context.annotation.Primary;
    11. import org.springframework.stereotype.Service;
    12. import javax.servlet.http.HttpServletResponse;
    13. import java.util.HashMap;
    14. import java.util.List;
    15. import java.util.Map;
    16. @Primary
    17. @Service(WorkflowConfigCodeConstants.COMPANYREMITTANCE_DOWNXECEL)
    18. public class CompanyrittceDownExcelServiceImpl extends AbstractExcelCallBackServiceImpl {
    19. @Autowired
    20. private CompanyremittanceService companyremittanceService;
    21. @Override
    22. public void downexcel(HttpServletResponse response, Map params) throws Exception {
    23. String date = params.get("date");
    24. String name = params.get("name");
    25. String proparentId = params.get("proparentid");
    26. Map map = new HashMap<>();
    27. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("date"), ""))) {
    28. String[] dateBteetn = CommonMethod.getDate(params.get("date").toString());
    29. map.put("startDate" , dateBteetn[0]);
    30. map.put("stopDate" , dateBteetn[1]);
    31. }
    32. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("name"), ""))) {
    33. map.put("remittancethis" , params.get("name").toString());
    34. }
    35. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("proparentId"), ""))) {
    36. map.put("proparentid" , params.get("proparentId").toString());
    37. }
    38. String typeNames = CommonMethod.typeNameExcel(date, name, proparentId);
    39. List list = companyremittanceService.list(map);
    40. CommonMethod.downexcel(response, list, Constant.EXCELE_COMPANYMitter_STATUS, typeNames, 7);
    41. }
    42. }

    材料信息实现类:

    1. package com.bootdo.common.downInterface;
    2. import com.bootdo.common.config.Constant;
    3. import com.bootdo.common.config.WorkflowConfigCodeConstants;
    4. import com.bootdo.common.domain.AdvanceDO;
    5. import com.bootdo.common.domain.MaterialDO;
    6. import com.bootdo.common.service.MaterialService;
    7. import com.bootdo.common.utils.CommonMethod;
    8. import org.apache.commons.lang3.ObjectUtils;
    9. import org.apache.commons.lang3.StringUtils;
    10. import org.springframework.beans.factory.annotation.Autowired;
    11. import org.springframework.stereotype.Service;
    12. import javax.servlet.http.HttpServletResponse;
    13. import java.util.HashMap;
    14. import java.util.List;
    15. import java.util.Map;
    16. @Service(WorkflowConfigCodeConstants.METATLE_DOWNXECEL)
    17. public class MetialDownExcelServiceImpl extends AbstractExcelCallBackServiceImpl {
    18. @Autowired
    19. private MaterialService materialService;
    20. @Override
    21. public void downexcel(HttpServletResponse response, Map params) throws Exception {
    22. String date = params.get("date");
    23. String name = params.get("name");
    24. String proparentId = params.get("proparentId");
    25. Map map = new HashMap<>();
    26. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("date"), ""))) {
    27. String[] dateBteetn = CommonMethod.getDate(params.get("date").toString());
    28. map.put("startDate" , dateBteetn[0]);
    29. map.put("stopDate" , dateBteetn[1]);
    30. }
    31. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("name"), ""))) {
    32. map.put("materialname" , params.get("name").toString());
    33. }
    34. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("proparentId"), ""))) {
    35. map.put("proparentid" , params.get("proparentId").toString());
    36. }
    37. String typeNames = CommonMethod.typeNameExcel(date, name, proparentId);
    38. List list = materialService.list(map);
    39. CommonMethod.downexcel(response, list, Constant.EXCELE_TEALIERS_STATUS, typeNames, 5);
    40. }
    41. }

    贴一段很早就写的代码:

    1. /**
    2. * 下载excel
    3. *
    4. * @param response
    5. * @param list
    6. * @param status
    7. * @param typename
    8. * @throws Exception
    9. */
    10. public static void downexcel(HttpServletResponse response, List list, String status, String title, int size) throws Exception {
    11. response.setContentType("application/vnd.ms-excel");
    12. DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    13. double money = 0.0;
    14. List colist = null;
    15. String fileName = URLEncoder.encode(title + "信息表" , "utf-8");
    16. response.setHeader("Content-disposition" , "attachment;fileName=" + fileName + ".xls");
    17. // 创建一个输出流
    18. // 创建一个输出流
    19. OutputStream out = response.getOutputStream();
    20. // 创建一个excel工作簿,将输出流给我们的workbook
    21. WritableWorkbook wb = Workbook.createWorkbook(out);
    22. // 创建一个sheet(sheet名,位置)
    23. WritableSheet sheet = wb.createSheet(title, 200);
    24. // 设置样式
    25. // sheet.
    26. sheet.getSettings().setDefaultColumnWidth(15);// 设置列宽
    27. sheet.getSettings().setDefaultRowHeight(500);// 设置行高
    28. // 设置字体(字体,大小,粗细)只创建了字体,但是没有应用这个字体
    29. WritableFont font = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD);
    30. // 将字体应用到单元格
    31. WritableCellFormat format = new WritableCellFormat(font);
    32. // 设置边框
    33. format.setBorder(Border.ALL, BorderLineStyle.THIN);
    34. // 设置对齐方式
    35. format.setAlignment(Alignment.CENTRE);
    36. // 如果内容超出列宽,自动换行
    37. format.setWrap(true);
    38. // 将内容写入
    39. WritableFont font2 = new WritableFont(WritableFont.ARIAL, 30, WritableFont.BOLD);
    40. WritableCellFormat format2 = new WritableCellFormat(font2);
    41. format2.setAlignment(Alignment.CENTRE);
    42. sheet.mergeCells(0, 0, size, 0);
    43. // 创建一个标签,存放标题title(存放的同时,将title的位置和格式都存好了)
    44. Label label = new Label(0, 0, title, format2);
    45. // 将标题放入到sheet中
    46. sheet.addCell(label);
    47. switch (status) {
    48. case "1":
    49. // 小标题
    50. sheet.addCell(new Label(0, 1, "编号" , format));
    51. sheet.addCell(new Label(1, 1, "工作内容" , format));
    52. sheet.addCell(new Label(2, 1, "工作进度" , format));
    53. sheet.addCell(new Label(3, 1, "工作名称" , format));
    54. sheet.addCell(new Label(4, 1, "工作记录人" , format));
    55. sheet.addCell(new Label(5, 1, "工作计划开始时间" , format));
    56. sheet.addCell(new Label(6, 1, "工作计划结束时间" , format));
    57. sheet.addCell(new Label(7, 1, "分类" , format));
    58. List dictList = (List) list;
    59. for (int i = 0; i < dictList.size(); i++) {
    60. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    61. sheet.addCell(new Label(1, (i + 2), dictList.get(i).getWorkmessage() + "" , format));
    62. sheet.addCell(new Label(2, (i + 2), dictList.get(i).getWorkprogress(), format));
    63. sheet.addCell(new Label(3, (i + 2), dictList.get(i).getWorkname() + "" , format));
    64. sheet.addCell(new Label(4, (i + 2), dictList.get(i).getWorkthis() + "" , format));
    65. sheet.addCell(new Label(5, (i + 2), formatDate(dictList.get(i).getWorkstartdate()) + "" , format));
    66. sheet.addCell(new Label(6, (i + 2), formatDate(dictList.get(i).getWorkstopdate()) + "" , format));
    67. sheet.addCell(new Label(7, (i + 2), getProjectname(dictList.get(i).getProparentid().toString()), format));
    68. }
    69. break;
    70. case "2":
    71. // 小标题
    72. sheet.addCell(new Label(0, 1, "编号" , format));
    73. sheet.addCell(new Label(1, 1, "打款金额" , format));
    74. sheet.addCell(new Label(2, 1, "打款日期" , format));
    75. sheet.addCell(new Label(3, 1, "汇款人名称" , format));
    76. sheet.addCell(new Label(4, 1, "备注" , format));
    77. sheet.addCell(new Label(5, 1, "公司名称" , format));
    78. sheet.addCell(new Label(6, 1, "分类" , format));
    79. List companyadvanceDOS = (List) list;
    80. double companyMoney = 0.0;
    81. for (int i = 0; i < companyadvanceDOS.size(); i++) {
    82. companyMoney += companyadvanceDOS.get(i).getCompanyMoney();
    83. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    84. sheet.addCell(new Label(1, (i + 2), companyadvanceDOS.get(i).getCompanyMoney() + "" , format));
    85. sheet.addCell(new Label(2, (i + 2), companyadvanceDOS.get(i).getCompanyDate(), format));
    86. sheet.addCell(new Label(3, (i + 2), companyadvanceDOS.get(i).getCmpname(), format));
    87. sheet.addCell(new Label(4, (i + 2), companyadvanceDOS.get(i).getDecptions() + "" , format));
    88. sheet.addCell(new Label(5, (i + 2), companyadvanceDOS.get(i).getCompanyName() + "" , format));
    89. sheet.addCell(new Label(6, (i + 2), getProjectname(companyadvanceDOS.get(i).getProparentId().toString()), format));
    90. }
    91. WritableCellFormat format3 = new WritableCellFormat(font2);
    92. // 追加最后一行
    93. Label label2 = new Label(0, companyadvanceDOS.size() + 2, "合计:" + String.valueOf(companyMoney) + "\n\n\n\n\n" , format3);
    94. sheet.addCell(label2);
    95. break;
    96. case "3":
    97. sheet.addCell(new Label(0, 1, "编号" , format));
    98. sheet.addCell(new Label(1, 1, "人员名称" , format));
    99. sheet.addCell(new Label(2, 1, "身份证号码" , format));
    100. sheet.addCell(new Label(3, 1, "银行卡号" , format));
    101. sheet.addCell(new Label(4, 1, "联系电话" , format));
    102. sheet.addCell(new Label(5, 1, "入场时间" , format));
    103. sheet.addCell(new Label(6, 1, "分类" , format));
    104. List emplpueesDOList = (List) list;
    105. for (int i = 0; i < emplpueesDOList.size(); i++) {
    106. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    107. sheet.addCell(new Label(1, (i + 2), emplpueesDOList.get(i).getEmployeename(), format));
    108. sheet.addCell(new Label(2, (i + 2), emplpueesDOList.get(i).getEmployeeidcards() + "" , format));
    109. sheet.addCell(new Label(3, (i + 2), emplpueesDOList.get(i).getBankcards() + "" , format));
    110. sheet.addCell(new Label(4, (i + 2), emplpueesDOList.get(i).getPhone() + "" , format));
    111. sheet.addCell(new Label(5, (i + 2), emplpueesDOList.get(i).getEntrancedate() + "" , format));
    112. sheet.addCell(new Label(6, (i + 2), getProjectname(emplpueesDOList.get(i).getProparentId().toString()), format));
    113. }
    114. break;
    115. case "4":
    116. sheet.addCell(new Label(0, 1, "编号" , format));
    117. sheet.addCell(new Label(1, 1, "人员名称" , format));
    118. sheet.addCell(new Label(2, 1, "点工总金额" , format));
    119. sheet.addCell(new Label(3, 1, "包工总金额" , format));
    120. sheet.addCell(new Label(4, 1, "总材料" , format));
    121. sheet.addCell(new Label(5, 1, "总借支" , format));
    122. sheet.addCell(new Label(6, 1, "余额" , format));
    123. sheet.addCell(new Label(7, 1, "备注" , format));
    124. sheet.addCell(new Label(8, 1, "分类" , format));
    125. List advacesumDOList = (List) list;
    126. double advacemMney = 0.0;
    127. for (int i = 0; i < advacesumDOList.size(); i++) {
    128. if (advacesumDOList.get(i).getBalance() != null) {
    129. advacemMney += advacesumDOList.get(i).getBalance();
    130. }
    131. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    132. sheet.addCell(new Label(1, (i + 2), advacesumDOList.get(i).getName(), format));
    133. sheet.addCell(new Label(2, (i + 2), advacesumDOList.get(i).getAmountmoney() + "" , format));
    134. sheet.addCell(new Label(3, (i + 2), advacesumDOList.get(i).getContractmoney() + "" , format));
    135. sheet.addCell(new Label(4, (i + 2), advacesumDOList.get(i).getMeterialsum() + "" , format));
    136. sheet.addCell(new Label(5, (i + 2), advacesumDOList.get(i).getAdvacemoney() + "" , format));
    137. sheet.addCell(new Label(6, (i + 2), advacesumDOList.get(i).getBalance() + "" , format));
    138. sheet.addCell(new Label(7, (i + 2), advacesumDOList.get(i).getDecptions() + "" , format));
    139. sheet.addCell(new Label(8, (i + 2), getProjectname(advacesumDOList.get(i).getProparentId().toString()), format));
    140. }
    141. WritableCellFormat advacefarmat = new WritableCellFormat(font2);
    142. // 追加最后一行
    143. Label advaceSumLabel = new Label(0, advacesumDOList.size() + 2, "合计:" + String.valueOf(advacemMney) + "\n\n\n\n\n" , advacefarmat);
    144. sheet.addCell(advaceSumLabel);
    145. break;
    146. case "5":
    147. sheet.addCell(new Label(0, 1, "姓名" , format));
    148. sheet.addCell(new Label(1, 1, "分类" , format));
    149. sheet.addCell(new Label(2, 1, "年月" , format));
    150. sheet.addCell(new Label(3, 1, "日期" , format));
    151. sheet.addCell(new Label(4, 1, "借支金额" , format));
    152. sheet.addCell(new Label(5, 1, "备注" , format));
    153. List advanceDOList = (List) list;
    154. int cloumn = 0;
    155. for (int i = 0; i < advanceDOList.size(); i++) {
    156. AdvanceDO dto = advanceDOList.get(i);
    157. //1.渲染姓名
    158. sheet.addCell(new Label(0, (cloumn + 2), dto.getDavacename(), format));
    159. //2.合并姓名
    160. sheet.mergeCells(0, (cloumn + 2), 0, (cloumn + 2) + 24);
    161. //3.渲染分类
    162. sheet.setColumnView(1, 30);//设置第2列的宽度
    163. sheet.addCell(new Label(1, (cloumn + 2), dto.getTypename(), format));
    164. //4.合并分类
    165. sheet.mergeCells(1, (cloumn + 2), 1, (cloumn + 2) + 24);
    166. //5.渲染年月
    167. sheet.addCell(new Label(2, (cloumn + 2), dto.getYears() + "-01" , format));
    168. sheet.addCell(new Label(2, (cloumn + 4), dto.getYears() + "-02" , format));
    169. sheet.addCell(new Label(2, (cloumn + 6), dto.getYears() + "-03" , format));
    170. sheet.addCell(new Label(2, (cloumn + 8), dto.getYears() + "-04" , format));
    171. sheet.addCell(new Label(2, (cloumn + 10), dto.getYears() + "-05" , format));
    172. sheet.addCell(new Label(2, (cloumn + 12), dto.getYears() + "-06" , format));
    173. sheet.addCell(new Label(2, (cloumn + 14), dto.getYears() + "-07" , format));
    174. sheet.addCell(new Label(2, (cloumn + 16), dto.getYears() + "-08" , format));
    175. sheet.addCell(new Label(2, (cloumn + 18), dto.getYears() + "-09" , format));
    176. sheet.addCell(new Label(2, (cloumn + 20), dto.getYears() + "-10" , format));
    177. sheet.addCell(new Label(2, (cloumn + 22), dto.getYears() + "-11" , format));
    178. /* //6.合并年月
    179. sheet.mergeCells(2, (cloumn + 2), 1, (cloumn + 2) + 1);
    180. sheet.mergeCells(2, (cloumn + 4), 1, (cloumn + 4) + 1);
    181. sheet.mergeCells(2, (cloumn + 6), 1, (cloumn + 6) + 1);
    182. sheet.mergeCells(2, (cloumn + 8), 1, (cloumn + 8) + 1);
    183. sheet.mergeCells(2, (cloumn + 10), 1, (cloumn + 10) + 1);
    184. sheet.mergeCells(2, (cloumn + 12), 1, (cloumn + 12) + 1);
    185. sheet.mergeCells(2, (cloumn + 14), 1, (cloumn + 14) + 1);
    186. sheet.mergeCells(2, (cloumn + 16), 1, (cloumn + 16) + 1);
    187. sheet.mergeCells(2, (cloumn + 18), 1, (cloumn + 18) + 1);
    188. sheet.mergeCells(2, (cloumn + 20), 1, (cloumn + 20) + 1);
    189. sheet.mergeCells(2, (cloumn + 22), 1, (cloumn + 22) + 1);
    190. sheet.mergeCells(2, (cloumn + 24), 1, (cloumn + 24) + 1);*/
    191. sheet.addCell(new Label(2, (cloumn + 24), dto.getYears() + "-12" , format));
    192. //7.渲染统计
    193. sheet.addCell(new Label(2, (cloumn + 26), "总计:" + dto.getDavaceprice(), format));
    194. //8.合并统计
    195. sheet.mergeCells(2, (cloumn + 26), 5, (cloumn + 26));
    196. //9.渲染日期
    197. sheet.addCell(new Label(3, (cloumn + 2), dto.getD1s(), format));
    198. sheet.addCell(new Label(3, (cloumn + 3), dto.getD1t(), format));
    199. sheet.addCell(new Label(3, (cloumn + 4), dto.getD2s(), format));
    200. sheet.addCell(new Label(3, (cloumn + 5), dto.getD2t(), format));
    201. sheet.addCell(new Label(3, (cloumn + 6), dto.getD3s(), format));
    202. sheet.addCell(new Label(3, (cloumn + 7), dto.getD3t(), format));
    203. sheet.addCell(new Label(3, (cloumn + 8), dto.getD4s(), format));
    204. sheet.addCell(new Label(3, (cloumn + 9), dto.getD4t(), format));
    205. sheet.addCell(new Label(3, (cloumn + 10), dto.getD5s(), format));
    206. sheet.addCell(new Label(3, (cloumn + 11), dto.getD5t(), format));
    207. sheet.addCell(new Label(3, (cloumn + 12), dto.getD6s(), format));
    208. sheet.addCell(new Label(3, (cloumn + 13), dto.getD6t(), format));
    209. sheet.addCell(new Label(3, (cloumn + 14), dto.getD7s(), format));
    210. sheet.addCell(new Label(3, (cloumn + 15), dto.getD7t(), format));
    211. sheet.addCell(new Label(3, (cloumn + 16), dto.getD8s(), format));
    212. sheet.addCell(new Label(3, (cloumn + 17), dto.getD8t(), format));
    213. sheet.addCell(new Label(3, (cloumn + 18), dto.getD9s(), format));
    214. sheet.addCell(new Label(3, (cloumn + 19), dto.getD9t(), format));
    215. sheet.addCell(new Label(3, (cloumn + 20), dto.getD10s(), format));
    216. sheet.addCell(new Label(3, (cloumn + 21), dto.getD10t(), format));
    217. sheet.addCell(new Label(3, (cloumn + 22), dto.getD11s(), format));
    218. sheet.addCell(new Label(3, (cloumn + 23), dto.getD11t(), format));
    219. sheet.addCell(new Label(3, (cloumn + 24), dto.getD12s(), format));
    220. sheet.addCell(new Label(3, (cloumn + 25), dto.getD12t(), format));
    221. //10.渲染借支金额
    222. sheet.addCell(new Label(4, (cloumn + 2), dto.getM1smoney() + "" , format));
    223. sheet.addCell(new Label(4, (cloumn + 3), dto.getM1tmoney() + "" , format));
    224. sheet.addCell(new Label(4, (cloumn + 4), dto.getM2smoney() + "" , format));
    225. sheet.addCell(new Label(4, (cloumn + 5), dto.getM2tmoney() + "" , format));
    226. sheet.addCell(new Label(4, (cloumn + 6), dto.getM3smoney() + "" , format));
    227. sheet.addCell(new Label(4, (cloumn + 7), dto.getM3tmoney() + "" , format));
    228. sheet.addCell(new Label(4, (cloumn + 8), dto.getM4smoney() + "" , format));
    229. sheet.addCell(new Label(4, (cloumn + 9), dto.getM4tmoney() + "" , format));
    230. sheet.addCell(new Label(4, (cloumn + 10), dto.getM5smoney() + "" , format));
    231. sheet.addCell(new Label(4, (cloumn + 11), dto.getM5tmoney() + "" , format));
    232. sheet.addCell(new Label(4, (cloumn + 12), dto.getM6smoney() + "" , format));
    233. sheet.addCell(new Label(4, (cloumn + 13), dto.getM6tmoney() + "" , format));
    234. sheet.addCell(new Label(4, (cloumn + 14), dto.getM7smoney() + "" , format));
    235. sheet.addCell(new Label(4, (cloumn + 15), dto.getM7tmoney() + "" , format));
    236. sheet.addCell(new Label(4, (cloumn + 16), dto.getM8smoney() + "" , format));
    237. sheet.addCell(new Label(4, (cloumn + 17), dto.getM8tmoney() + "" , format));
    238. sheet.addCell(new Label(4, (cloumn + 18), dto.getM9smoney() + "" , format));
    239. sheet.addCell(new Label(4, (cloumn + 19), dto.getM9tmoney() + "" , format));
    240. sheet.addCell(new Label(4, (cloumn + 20), dto.getM10smoney() + "" , format));
    241. sheet.addCell(new Label(4, (cloumn + 21), dto.getM10tmoney() + "" , format));
    242. sheet.addCell(new Label(4, (cloumn + 22), dto.getM11smoney() + "" , format));
    243. sheet.addCell(new Label(4, (cloumn + 23), dto.getM11tmoney() + "" , format));
    244. sheet.addCell(new Label(4, (cloumn + 24), dto.getM12smoney() + "" , format));
    245. sheet.addCell(new Label(4, (cloumn + 25), dto.getM12tmoney() + "" , format));
    246. //11.渲染备注
    247. sheet.setColumnView(5, 50);//设置第6列的宽度
    248. sheet.addCell(new Label(5, (cloumn + 2), dto.getM1snote(), format));
    249. sheet.addCell(new Label(5, (cloumn + 3), dto.getM1tnote(), format));
    250. sheet.addCell(new Label(5, (cloumn + 4), dto.getM2snote(), format));
    251. sheet.addCell(new Label(5, (cloumn + 5), dto.getM2tnote(), format));
    252. sheet.addCell(new Label(5, (cloumn + 6), dto.getM3snote(), format));
    253. sheet.addCell(new Label(5, (cloumn + 7), dto.getM3tnote(), format));
    254. sheet.addCell(new Label(5, (cloumn + 8), dto.getM4snote(), format));
    255. sheet.addCell(new Label(5, (cloumn + 9), dto.getM4tnote(), format));
    256. sheet.addCell(new Label(5, (cloumn + 10), dto.getM5snote(), format));
    257. sheet.addCell(new Label(5, (cloumn + 11), dto.getM5tnote(), format));
    258. sheet.addCell(new Label(5, (cloumn + 12), dto.getM6snote(), format));
    259. sheet.addCell(new Label(5, (cloumn + 13), dto.getM6tnote(), format));
    260. sheet.addCell(new Label(5, (cloumn + 14), dto.getM7snote(), format));
    261. sheet.addCell(new Label(5, (cloumn + 15), dto.getM7tnote(), format));
    262. sheet.addCell(new Label(5, (cloumn + 16), dto.getM8snote(), format));
    263. sheet.addCell(new Label(5, (cloumn + 17), dto.getM8tnote(), format));
    264. sheet.addCell(new Label(5, (cloumn + 18), dto.getM9snote(), format));
    265. sheet.addCell(new Label(5, (cloumn + 19), dto.getM9tnote(), format));
    266. sheet.addCell(new Label(5, (cloumn + 20), dto.getM10snote(), format));
    267. sheet.addCell(new Label(5, (cloumn + 21), dto.getM10tnote(), format));
    268. sheet.addCell(new Label(5, (cloumn + 22), dto.getM11snote(), format));
    269. sheet.addCell(new Label(5, (cloumn + 23), dto.getM11tnote(), format));
    270. sheet.addCell(new Label(5, (cloumn + 24), dto.getM12snote(), format));
    271. sheet.addCell(new Label(5, (cloumn + 25), dto.getM12tnote(), format));
    272. cloumn += 25;
    273. }
    274. DoubleSummaryStatistics sum = advanceDOList.stream().collect(Collectors.summarizingDouble(AdvanceDO::getDavaceprice));
    275. WritableCellFormat advacesfarmat = new WritableCellFormat(font2);
    276. // 追加最后一行
    277. Label advaceLabel = new Label(0, cloumn + 2, "合计:" + sum.getSum() + "\n\n\n\n\n" , advacesfarmat);
    278. sheet.addCell(advaceLabel);
    279. break;
    280. case "6":
    281. // 小标题
    282. sheet.addCell(new Label(0, 1, "编号" , format));
    283. sheet.addCell(new Label(1, 1, "材料名称" , format));
    284. sheet.addCell(new Label(2, 1, "材料总价格" , format));
    285. sheet.addCell(new Label(3, 1, "材料描述" , format));
    286. sheet.addCell(new Label(4, 1, "材料单位" , format));
    287. sheet.addCell(new Label(5, 1, "分类" , format));
    288. List materialDOList = (List) list;
    289. double moneys = 0.0;
    290. for (int i = 0; i < materialDOList.size(); i++) {
    291. moneys += materialDOList.get(i).getSumprice();
    292. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    293. sheet.addCell(new Label(1, (i + 2), materialDOList.get(i).getMaterialname() + "" , format));
    294. sheet.addCell(new Label(2, (i + 2), materialDOList.get(i).getSumprice().toString(), format));
    295. sheet.addCell(new Label(3, (i + 2), materialDOList.get(i).getMaterialdecptions() + "" , format));
    296. sheet.addCell(new Label(4, (i + 2), materialDOList.get(i).getMaterialdanwei() + "" , format));
    297. sheet.addCell(new Label(5, (i + 2), getProjectname(materialDOList.get(i).getProparentid().toString()), format));
    298. }
    299. WritableCellFormat materialFormat = new WritableCellFormat(font2);
    300. // 追加最后一行
    301. Label materiallabel = new Label(0, materialDOList.size() + 2, "合计:" + String.valueOf(moneys) + "\n\n\n\n\n" , materialFormat);
    302. sheet.addCell(materiallabel);
    303. break;
    304. case "7":
    305. sheet.addCell(new Label(0, 1, "编号" , format));
    306. sheet.addCell(new Label(1, 1, "汇款日期" , format));
    307. sheet.addCell(new Label(2, 1, "收款人" , format));
    308. sheet.addCell(new Label(3, 1, "汇款金额" , format));
    309. sheet.addCell(new Label(4, 1, "汇款公司" , format));
    310. sheet.addCell(new Label(5, 1, "汇款备注" , format));
    311. sheet.addCell(new Label(6, 1, "汇款银行卡号" , format));
    312. sheet.addCell(new Label(7, 1, "分类" , format));
    313. colist = (List) list;
    314. for (int i = 0; i < colist.size(); i++) {
    315. money += colist.get(i).getRemittancemoney();
    316. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    317. sheet.addCell(new Label(1, (i + 2), colist.get(i).getRemittancedate(), format));
    318. sheet.addCell(new Label(2, (i + 2), colist.get(i).getRemittancethis(), format));
    319. sheet.addCell(new Label(3, (i + 2), colist.get(i).getRemittancemoney() + "" , format));
    320. sheet.addCell(new Label(4, (i + 2), colist.get(i).getRemittancename() + "" , format));
    321. sheet.addCell(new Label(5, (i + 2), colist.get(i).getRemittancemessage() + "" , format));
    322. sheet.addCell(new Label(6, (i + 2), colist.get(i).getRemittancemebankcards() + "" , format));
    323. sheet.addCell(new Label(7, (i + 2), getProjectname(colist.get(i).getProparentid().toString()), format));
    324. }
    325. break;
    326. case "8":
    327. sheet.addCell(new Label(0, 1, "编号" , format));
    328. sheet.addCell(new Label(1, 1, "工人名称" , format));
    329. sheet.addCell(new Label(2, 1, "总工天" , format));
    330. sheet.addCell(new Label(3, 1, "分类" , format));
    331. List labourdayDOList = (List) list;
    332. for (int i = 0; i < labourdayDOList.size(); i++) {
    333. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    334. sheet.addCell(new Label(1, (i + 2), labourdayDOList.get(i).getName(), format));
    335. sheet.addCell(new Label(2, (i + 2), labourdayDOList.get(i).getSumlabourday() + "" , format));
    336. sheet.addCell(new Label(3, (i + 2), getProjectname(labourdayDOList.get(i).getProparentId().toString()), format));
    337. }
    338. break;
    339. case "9":
    340. sheet.addCell(new Label(0, 1, "编号" , format));
    341. sheet.addCell(new Label(1, 1, "突击人员名称" , format));
    342. sheet.addCell(new Label(2, 1, "突击日期" , format));
    343. sheet.addCell(new Label(3, 1, "人数" , format));
    344. sheet.addCell(new Label(4, 1, "人工费" , format));
    345. sheet.addCell(new Label(5, 1, "车费" , format));
    346. sheet.addCell(new Label(6, 1, "总金额" , format));
    347. sheet.addCell(new Label(7, 1, "分类" , format));
    348. List assaultDOS = (List) list;
    349. double assaultMoney = 0.0;
    350. for (int i = 0; i < assaultDOS.size(); i++) {
    351. assaultMoney += assaultDOS.get(i).getSummoney();
    352. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    353. sheet.addCell(new Label(1, (i + 2), assaultDOS.get(i).getName() + "" , format));
    354. sheet.addCell(new Label(2, (i + 2), (assaultDOS.get(i).getAssaultdate()) + "" , format));
    355. sheet.addCell(new Label(3, (i + 2), assaultDOS.get(i).getCount() + "" , format));
    356. sheet.addCell(new Label(4, (i + 2), assaultDOS.get(i).getMenmoney() + "" , format));
    357. sheet.addCell(new Label(5, (i + 2), assaultDOS.get(i).getCarmoney() + "" , format));
    358. sheet.addCell(new Label(6, (i + 2), assaultDOS.get(i).getSummoney() + "" , format));
    359. sheet.addCell(new Label(7, (i + 2), getProjectname(assaultDOS.get(i).getProparentId().toString()) + "" , format));
    360. }
    361. WritableCellFormat assFormat = new WritableCellFormat(font2);
    362. // 追加最后一行
    363. Label asslabel = new Label(0, assaultDOS.size() + 2, "合计:" + String.valueOf(assaultMoney) + "\n\n\n\n\n" , assFormat);
    364. sheet.addCell(asslabel);
    365. break;
    366. case "10":
    367. // 小标题*/
    368. sheet.addCell(new Label(0, 1, "编号" , format));
    369. sheet.addCell(new Label(1, 1, "工天记录人" , format));
    370. sheet.addCell(new Label(2, 1, "日期" , format));
    371. sheet.addCell(new Label(3, 1, "总工天" , format));
    372. sheet.addCell(new Label(4, 1, "总金额" , format));
    373. sheet.addCell(new Label(5, 1, "备注" , format));
    374. sheet.addCell(new Label(6, 1, "分类" , format));
    375. List companydyaDOS = (List) list;
    376. double companydyaMoney = 0.0;
    377. for (int i = 0; i < companydyaDOS.size(); i++) {
    378. companydyaMoney += companydyaDOS.get(i).getComoanymoney();
    379. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    380. sheet.addCell(new Label(1, (i + 2), companydyaDOS.get(i).getCompanythis() + "" , format));
    381. sheet.addCell(new Label(2, (i + 2), (companydyaDOS.get(i).getCompanydate()) + "" , format));
    382. sheet.addCell(new Label(3, (i + 2), companydyaDOS.get(i).getCompanyday() + "" , format));
    383. sheet.addCell(new Label(4, (i + 2), companydyaDOS.get(i).getComoanymoney() + "" , format));
    384. sheet.addCell(new Label(5, (i + 2), companydyaDOS.get(i).getMessage() + "" , format));
    385. sheet.addCell(new Label(6, (i + 2), getProjectname(companydyaDOS.get(i).getProparentid().toString()) + "" , format));
    386. }
    387. WritableCellFormat companydyaFormat = new WritableCellFormat(font2);
    388. // 追加最后一行
    389. Label companydyalabel = new Label(0, companydyaDOS.size() + 2, "合计:" + String.valueOf(companydyaMoney) + "\n\n\n\n\n" , companydyaFormat);
    390. sheet.addCell(companydyalabel);
    391. break;
    392. case "11":
    393. // 小标题*/
    394. sheet.addCell(new Label(0, 1, "月份" , format));
    395. sheet.addCell(new Label(1, 1, "项目名称" , format));
    396. sheet.addCell(new Label(2, 1, "完成产值" , format));
    397. sheet.addCell(new Label(3, 1, "所用包工" , format));
    398. sheet.addCell(new Label(4, 1, "所用点工" , format));
    399. sheet.addCell(new Label(5, 1, "管理费" , format));
    400. sheet.addCell(new Label(6, 1, "亏/盈" , format));
    401. sheet.addCell(new Label(7, 1, "备注" , format));
    402. List process = (List) list;
    403. double win = 0.0;
    404. BigDecimal income = null;//总收入
    405. int node = process.size() + 2;
    406. for (int i = 0; i < process.size(); i++) {
    407. sheet.addCell(new Label(0, (i + 2), process.get(i).getYear() + "" , format));
    408. sheet.addCell(new Label(1, (i + 2), getProjectname(process.get(i).getProparentId().toString()) + "" , format));
    409. sheet.addCell(new Label(2, (i + 2), process.get(i).getProduction() + "" , format));
    410. sheet.addCell(new Label(3, (i + 2), process.get(i).getContractor() + "" , format));
    411. sheet.addCell(new Label(4, (i + 2), process.get(i).getPointwork() + "" , format));
    412. sheet.addCell(new Label(5, (i + 2), process.get(i).getMaterial() + "" , format));
    413. sheet.addCell(new Label(6, (i + 2), process.get(i).getManagement() + "" , format));
    414. sheet.addCell(new Label(7, (i + 2), process.get(i).getNote() + "" , format));
    415. win += process.get(i).getWin();
    416. ;
    417. }
    418. WritableCellFormat processFormat = new WritableCellFormat(font2);
    419. // 追加最后一行
    420. Label processLabel = new Label(0, node, "合计:" + formatNumber(win).toString(), processFormat);
    421. sheet.addCell(processLabel);
    422. break;
    423. }
    424. if (status.equals("7")) {
    425. WritableCellFormat format3 = new WritableCellFormat(font2);
    426. // 追加最后一行
    427. Label label2 = new Label(0, colist.size() + 2, "合计:" + String.valueOf(money), format3);
    428. sheet.addCell(label2);
    429. }
    430. // 关闭资源
    431. wb.write();
    432. wb.close();
    433. out.flush();
    434. out.close();
    435. }

    这段代码显然有很大的优化空间,但是很早入门写的代码,感觉不算太规范吧,但是总这上面来说,优化的空间就在excel上面优化成动态的生产列名和标题,通过循环反射的方式去优化代码即可。采用java8方式构造数据也是可以的。

     ————没有与生俱来的天赋,都是后天的努力拼搏我是小杨,谢谢你的关注和支持)

  • 相关阅读:
    在 Windows 上搭建 NTP 服务器
    Java基础面试题&知识点总结(下篇)
    Verilog零基础入门(边看边练与测试仿真)-状态机-笔记(7-10讲)
    在JavaScript中利用代码来实现ATM的效果,具有存钱,取钱,查看余额,退出功能
    一文读懂vue3和vue2的API风格对比
    Visual Studio 的安装和调试及scanf的解决方法
    多台云服务器的 Kubernetes 集群搭建
    实用 - Java后端面试经历(经验分享)适用于2~3年
    挑战杯 机器视觉人体跌倒检测系统 - opencv python
    微信小程序:js处理一段文字,根据句号或者分号进行换行
  • 原文地址:https://blog.csdn.net/qq_39751120/article/details/127759272