什么是策略模式?为什么要使用策略模式?
策略模式属于行为型模式,行为型用来识别对象之间的常用交流模式并加以实现。使用策略模式的好处有三点:
1,优化代码逻辑结构性,可以减少分支判断。
2,增加代码程序拓展和移植。
3,增加代码复用和业务逻辑分离和优化。
当然也有缺点的,缺点就是得提前约定好规范,从而触发这样的业务场景。
项目应用场景:工作流,支付,聚合支付,日志收集,当然还有很多很多,只有你需要多渠道操作业务这个时候不知道怎么使用设计模式的时候,这个时候代理模式是首先,他既能分离代码和实现代码的拓展。
这里给大家一个我之前做的一个下载,采用统一下载,但是不同的业务模板其实是不一样的,这样就会导致会写很多冗余代码,我每创建一个导出的下载就会去更新模板和方法,这个是很早就做的导出excel的功能,代码还是有相对优化的余地。
使用此模式注意的俩点:
1,必须使用上下文获取对象的实现。
2,必须指定优先注入的key @Primary 这个注解必须加,不然会报空指针异常,找不到实现类,上下文获取对象必须有个对象加载。
- package com.bootdo.common.config;
-
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.stereotype.Component;
-
- /**
- * @author yang
- */
- @Component
- public class ApplicationContextProvider implements ApplicationContextAware {
-
- /**
- * 上下文对象实例
- */
- private static ApplicationContext APPLICATION_CONTENT;
-
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- APPLICATION_CONTENT = applicationContext;
- }
-
- /**
- * 获取applicationContext
- *
- * @return ApplicationContext
- */
- public static ApplicationContext getApplicationContext() {
- return APPLICATION_CONTENT;
- }
-
- /**
- * 通过name获取 Bean.
- *
- * @param name name
- * @return Object
- */
- public static Object getBean(String name) {
- return getApplicationContext().getBean(name);
- }
-
- /**
- * 通过class获取Bean.
- *
- * @param clazz 类名称
- * @param
实例化泛型 - * @return T
- */
- public static
T getBean(Class clazz) { - return getApplicationContext().getBean(clazz);
- }
-
- /**
- * 通过name,以及Clazz返回指定的Bean
- *
- * @param name 服务别称
- * @param clazz 类名
- * @param
实例化泛型 - * @return T
- */
- public static
T getBean(String name, Class clazz) { - return getApplicationContext().getBean(name, clazz);
- }
- }
类图:
注解方式实现:
定义一个抽象类:
-
- import javax.servlet.http.HttpServletResponse;
- import java.util.Map;
-
- public interface DownExcelService {
- void downexcel(HttpServletResponse response, Map
params) throws Exception; - }
定义一个常量方式:
- package com.bootdo.common.config;
-
- /**
- * Excel 下载接口
- *
- * @author yangchang
- */
- public interface WorkflowConfigCodeConstants {
- /**
- * 工人总账目
- */
- String ADVANCE_SUM_DOWNXECEL = "advacnsum_downexcel";
-
- /**
- * 工人借支
- */
- String ADVANCE_DOWNXECEL = "advacn_downexcel";
-
- /**
- * 材料
- */
-
- String METATLE_DOWNXECEL = "metail_downexcel";
-
-
- /**
- * 公司生活费
- */
- String COMPANYREMITTANCE_DOWNXECEL = "companyremittance_downexcel";
-
-
- /**
- * 突击信息
- */
- String ASSAULT_DOWNXECEL = "assault_downexcel";
-
- }
多个实现类:
默认实现类:
- package com.bootdo.common.controller.detail;
-
- import com.bootdo.common.service.DownExcelService;
-
- import javax.servlet.http.HttpServletResponse;
- import java.util.Map;
-
- public abstract class AbstractDetaliCallBackServiceImpl implements DownExcelService {
- @Override
- public void downexcel(HttpServletResponse response, Map
params) throws Exception { - }
- }
突击实现类:
- package com.bootdo.common.downInterface;
-
- import com.bootdo.common.config.Constant;
- import com.bootdo.common.config.WorkflowConfigCodeConstants;
- import com.bootdo.common.domain.AssaultDO;
- import com.bootdo.common.service.AssaultService;
- import com.bootdo.common.utils.CommonMethod;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Primary;
- import org.springframework.stereotype.Service;
-
- import javax.servlet.http.HttpServletResponse;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- @Service(WorkflowConfigCodeConstants.ASSAULT_DOWNXECEL)
- public class AssaultDownExcelServiceImpl extends AbstractExcelCallBackServiceImpl {
- @Autowired
- private AssaultService assaultService;
-
- @Override
- public void downexcel(HttpServletResponse response, Map
params) throws Exception { - String date = params.get("date");
- String name = params.get("name");
- String proparentId = params.get("proparentId");
- Map
map = new HashMap(); - if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("date"), ""))) {
- String[] dateBteetn = CommonMethod.getDate(params.get("date").toString());
- map.put("startDate" , dateBteetn[0]);
- map.put("stopDate" , dateBteetn[1]);
- }
- if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("name"), ""))) {
- map.put("name" , params.get("name").toString());
- }
- if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("proparentId"), ""))) {
- map.put("proparentId" , params.get("proparentId").toString());
- }
- String typeNames = CommonMethod.typeNameExcel(date, name, proparentId);
- List
list = assaultService.list(map); - CommonMethod.downexcel(response, list, Constant.EXCELE_ASSAULT_STATUS, typeNames, 7);
- }
- }
公司生活费实现类:
- package com.bootdo.common.downInterface;
-
- import com.bootdo.common.config.Constant;
- import com.bootdo.common.config.WorkflowConfigCodeConstants;
- import com.bootdo.common.domain.CompanyremittanceDO;
- import com.bootdo.common.service.CompanyremittanceService;
- import com.bootdo.common.utils.CommonMethod;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Primary;
- import org.springframework.stereotype.Service;
-
- import javax.servlet.http.HttpServletResponse;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- @Primary
- @Service(WorkflowConfigCodeConstants.COMPANYREMITTANCE_DOWNXECEL)
- public class CompanyrittceDownExcelServiceImpl extends AbstractExcelCallBackServiceImpl {
- @Autowired
- private CompanyremittanceService companyremittanceService;
-
- @Override
- public void downexcel(HttpServletResponse response, Map
params) throws Exception { - String date = params.get("date");
- String name = params.get("name");
- String proparentId = params.get("proparentid");
- Map
map = new HashMap<>(); - if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("date"), ""))) {
- String[] dateBteetn = CommonMethod.getDate(params.get("date").toString());
- map.put("startDate" , dateBteetn[0]);
- map.put("stopDate" , dateBteetn[1]);
- }
- if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("name"), ""))) {
- map.put("remittancethis" , params.get("name").toString());
- }
- if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("proparentId"), ""))) {
- map.put("proparentid" , params.get("proparentId").toString());
- }
- String typeNames = CommonMethod.typeNameExcel(date, name, proparentId);
- List
list = companyremittanceService.list(map); - CommonMethod.downexcel(response, list, Constant.EXCELE_COMPANYMitter_STATUS, typeNames, 7);
- }
- }
材料信息实现类:
- package com.bootdo.common.downInterface;
-
- import com.bootdo.common.config.Constant;
- import com.bootdo.common.config.WorkflowConfigCodeConstants;
- import com.bootdo.common.domain.AdvanceDO;
- import com.bootdo.common.domain.MaterialDO;
- import com.bootdo.common.service.MaterialService;
- import com.bootdo.common.utils.CommonMethod;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import javax.servlet.http.HttpServletResponse;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- @Service(WorkflowConfigCodeConstants.METATLE_DOWNXECEL)
- public class MetialDownExcelServiceImpl extends AbstractExcelCallBackServiceImpl {
- @Autowired
- private MaterialService materialService;
-
- @Override
- public void downexcel(HttpServletResponse response, Map
params) throws Exception { - String date = params.get("date");
- String name = params.get("name");
- String proparentId = params.get("proparentId");
- Map
map = new HashMap<>(); -
- if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("date"), ""))) {
- String[] dateBteetn = CommonMethod.getDate(params.get("date").toString());
- map.put("startDate" , dateBteetn[0]);
- map.put("stopDate" , dateBteetn[1]);
- }
- if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("name"), ""))) {
- map.put("materialname" , params.get("name").toString());
- }
- if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("proparentId"), ""))) {
- map.put("proparentid" , params.get("proparentId").toString());
- }
- String typeNames = CommonMethod.typeNameExcel(date, name, proparentId);
- List
list = materialService.list(map); - CommonMethod.downexcel(response, list, Constant.EXCELE_TEALIERS_STATUS, typeNames, 5);
- }
- }
贴一段很早就写的代码:
- /**
- * 下载excel
- *
- * @param response
- * @param list
- * @param status
- * @param typename
- * @throws Exception
- */
- public static void downexcel(HttpServletResponse response, List> list, String status, String title, int size) throws Exception {
- response.setContentType("application/vnd.ms-excel");
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- double money = 0.0;
- List
colist = null; - String fileName = URLEncoder.encode(title + "信息表" , "utf-8");
- response.setHeader("Content-disposition" , "attachment;fileName=" + fileName + ".xls");
- // 创建一个输出流
-
-
- // 创建一个输出流
- OutputStream out = response.getOutputStream();
- // 创建一个excel工作簿,将输出流给我们的workbook
- WritableWorkbook wb = Workbook.createWorkbook(out);
- // 创建一个sheet(sheet名,位置)
- WritableSheet sheet = wb.createSheet(title, 200);
- // 设置样式
- // sheet.
- sheet.getSettings().setDefaultColumnWidth(15);// 设置列宽
- sheet.getSettings().setDefaultRowHeight(500);// 设置行高
- // 设置字体(字体,大小,粗细)只创建了字体,但是没有应用这个字体
- WritableFont font = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD);
- // 将字体应用到单元格
- WritableCellFormat format = new WritableCellFormat(font);
- // 设置边框
- format.setBorder(Border.ALL, BorderLineStyle.THIN);
- // 设置对齐方式
- format.setAlignment(Alignment.CENTRE);
- // 如果内容超出列宽,自动换行
- format.setWrap(true);
- // 将内容写入
- WritableFont font2 = new WritableFont(WritableFont.ARIAL, 30, WritableFont.BOLD);
- WritableCellFormat format2 = new WritableCellFormat(font2);
- format2.setAlignment(Alignment.CENTRE);
- sheet.mergeCells(0, 0, size, 0);
- // 创建一个标签,存放标题title(存放的同时,将title的位置和格式都存好了)
- Label label = new Label(0, 0, title, format2);
- // 将标题放入到sheet中
- sheet.addCell(label);
- switch (status) {
- case "1":
-
- // 小标题
-
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "工作内容" , format));
- sheet.addCell(new Label(2, 1, "工作进度" , format));
- sheet.addCell(new Label(3, 1, "工作名称" , format));
- sheet.addCell(new Label(4, 1, "工作记录人" , format));
- sheet.addCell(new Label(5, 1, "工作计划开始时间" , format));
- sheet.addCell(new Label(6, 1, "工作计划结束时间" , format));
- sheet.addCell(new Label(7, 1, "分类" , format));
-
- List
dictList = (List) list; - for (int i = 0; i < dictList.size(); i++) {
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), dictList.get(i).getWorkmessage() + "" , format));
- sheet.addCell(new Label(2, (i + 2), dictList.get(i).getWorkprogress(), format));
- sheet.addCell(new Label(3, (i + 2), dictList.get(i).getWorkname() + "" , format));
- sheet.addCell(new Label(4, (i + 2), dictList.get(i).getWorkthis() + "" , format));
- sheet.addCell(new Label(5, (i + 2), formatDate(dictList.get(i).getWorkstartdate()) + "" , format));
- sheet.addCell(new Label(6, (i + 2), formatDate(dictList.get(i).getWorkstopdate()) + "" , format));
- sheet.addCell(new Label(7, (i + 2), getProjectname(dictList.get(i).getProparentid().toString()), format));
- }
- break;
- case "2":
- // 小标题
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "打款金额" , format));
- sheet.addCell(new Label(2, 1, "打款日期" , format));
- sheet.addCell(new Label(3, 1, "汇款人名称" , format));
- sheet.addCell(new Label(4, 1, "备注" , format));
- sheet.addCell(new Label(5, 1, "公司名称" , format));
- sheet.addCell(new Label(6, 1, "分类" , format));
- List
companyadvanceDOS = (List) list; - double companyMoney = 0.0;
- for (int i = 0; i < companyadvanceDOS.size(); i++) {
- companyMoney += companyadvanceDOS.get(i).getCompanyMoney();
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), companyadvanceDOS.get(i).getCompanyMoney() + "" , format));
- sheet.addCell(new Label(2, (i + 2), companyadvanceDOS.get(i).getCompanyDate(), format));
- sheet.addCell(new Label(3, (i + 2), companyadvanceDOS.get(i).getCmpname(), format));
- sheet.addCell(new Label(4, (i + 2), companyadvanceDOS.get(i).getDecptions() + "" , format));
- sheet.addCell(new Label(5, (i + 2), companyadvanceDOS.get(i).getCompanyName() + "" , format));
- sheet.addCell(new Label(6, (i + 2), getProjectname(companyadvanceDOS.get(i).getProparentId().toString()), format));
-
- }
- WritableCellFormat format3 = new WritableCellFormat(font2);
- // 追加最后一行
- Label label2 = new Label(0, companyadvanceDOS.size() + 2, "合计:" + String.valueOf(companyMoney) + "\n\n\n\n\n" , format3);
- sheet.addCell(label2);
- break;
- case "3":
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "人员名称" , format));
- sheet.addCell(new Label(2, 1, "身份证号码" , format));
- sheet.addCell(new Label(3, 1, "银行卡号" , format));
- sheet.addCell(new Label(4, 1, "联系电话" , format));
- sheet.addCell(new Label(5, 1, "入场时间" , format));
- sheet.addCell(new Label(6, 1, "分类" , format));
- List
emplpueesDOList = (List) list; - for (int i = 0; i < emplpueesDOList.size(); i++) {
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), emplpueesDOList.get(i).getEmployeename(), format));
- sheet.addCell(new Label(2, (i + 2), emplpueesDOList.get(i).getEmployeeidcards() + "" , format));
- sheet.addCell(new Label(3, (i + 2), emplpueesDOList.get(i).getBankcards() + "" , format));
- sheet.addCell(new Label(4, (i + 2), emplpueesDOList.get(i).getPhone() + "" , format));
- sheet.addCell(new Label(5, (i + 2), emplpueesDOList.get(i).getEntrancedate() + "" , format));
- sheet.addCell(new Label(6, (i + 2), getProjectname(emplpueesDOList.get(i).getProparentId().toString()), format));
-
- }
- break;
-
- case "4":
-
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "人员名称" , format));
- sheet.addCell(new Label(2, 1, "点工总金额" , format));
- sheet.addCell(new Label(3, 1, "包工总金额" , format));
- sheet.addCell(new Label(4, 1, "总材料" , format));
- sheet.addCell(new Label(5, 1, "总借支" , format));
- sheet.addCell(new Label(6, 1, "余额" , format));
- sheet.addCell(new Label(7, 1, "备注" , format));
- sheet.addCell(new Label(8, 1, "分类" , format));
-
- List
advacesumDOList = (List) list; - double advacemMney = 0.0;
- for (int i = 0; i < advacesumDOList.size(); i++) {
- if (advacesumDOList.get(i).getBalance() != null) {
- advacemMney += advacesumDOList.get(i).getBalance();
-
- }
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), advacesumDOList.get(i).getName(), format));
- sheet.addCell(new Label(2, (i + 2), advacesumDOList.get(i).getAmountmoney() + "" , format));
- sheet.addCell(new Label(3, (i + 2), advacesumDOList.get(i).getContractmoney() + "" , format));
- sheet.addCell(new Label(4, (i + 2), advacesumDOList.get(i).getMeterialsum() + "" , format));
- sheet.addCell(new Label(5, (i + 2), advacesumDOList.get(i).getAdvacemoney() + "" , format));
- sheet.addCell(new Label(6, (i + 2), advacesumDOList.get(i).getBalance() + "" , format));
- sheet.addCell(new Label(7, (i + 2), advacesumDOList.get(i).getDecptions() + "" , format));
- sheet.addCell(new Label(8, (i + 2), getProjectname(advacesumDOList.get(i).getProparentId().toString()), format));
-
- }
- WritableCellFormat advacefarmat = new WritableCellFormat(font2);
- // 追加最后一行
- Label advaceSumLabel = new Label(0, advacesumDOList.size() + 2, "合计:" + String.valueOf(advacemMney) + "\n\n\n\n\n" , advacefarmat);
- sheet.addCell(advaceSumLabel);
- break;
-
- case "5":
- sheet.addCell(new Label(0, 1, "姓名" , format));
- sheet.addCell(new Label(1, 1, "分类" , format));
- sheet.addCell(new Label(2, 1, "年月" , format));
- sheet.addCell(new Label(3, 1, "日期" , format));
- sheet.addCell(new Label(4, 1, "借支金额" , format));
- sheet.addCell(new Label(5, 1, "备注" , format));
- List
advanceDOList = (List) list; - int cloumn = 0;
- for (int i = 0; i < advanceDOList.size(); i++) {
- AdvanceDO dto = advanceDOList.get(i);
- //1.渲染姓名
- sheet.addCell(new Label(0, (cloumn + 2), dto.getDavacename(), format));
- //2.合并姓名
- sheet.mergeCells(0, (cloumn + 2), 0, (cloumn + 2) + 24);
- //3.渲染分类
- sheet.setColumnView(1, 30);//设置第2列的宽度
- sheet.addCell(new Label(1, (cloumn + 2), dto.getTypename(), format));
- //4.合并分类
- sheet.mergeCells(1, (cloumn + 2), 1, (cloumn + 2) + 24);
- //5.渲染年月
- sheet.addCell(new Label(2, (cloumn + 2), dto.getYears() + "-01" , format));
- sheet.addCell(new Label(2, (cloumn + 4), dto.getYears() + "-02" , format));
- sheet.addCell(new Label(2, (cloumn + 6), dto.getYears() + "-03" , format));
- sheet.addCell(new Label(2, (cloumn + 8), dto.getYears() + "-04" , format));
- sheet.addCell(new Label(2, (cloumn + 10), dto.getYears() + "-05" , format));
- sheet.addCell(new Label(2, (cloumn + 12), dto.getYears() + "-06" , format));
- sheet.addCell(new Label(2, (cloumn + 14), dto.getYears() + "-07" , format));
- sheet.addCell(new Label(2, (cloumn + 16), dto.getYears() + "-08" , format));
- sheet.addCell(new Label(2, (cloumn + 18), dto.getYears() + "-09" , format));
- sheet.addCell(new Label(2, (cloumn + 20), dto.getYears() + "-10" , format));
- sheet.addCell(new Label(2, (cloumn + 22), dto.getYears() + "-11" , format));
- /* //6.合并年月
- sheet.mergeCells(2, (cloumn + 2), 1, (cloumn + 2) + 1);
- sheet.mergeCells(2, (cloumn + 4), 1, (cloumn + 4) + 1);
- sheet.mergeCells(2, (cloumn + 6), 1, (cloumn + 6) + 1);
- sheet.mergeCells(2, (cloumn + 8), 1, (cloumn + 8) + 1);
- sheet.mergeCells(2, (cloumn + 10), 1, (cloumn + 10) + 1);
- sheet.mergeCells(2, (cloumn + 12), 1, (cloumn + 12) + 1);
- sheet.mergeCells(2, (cloumn + 14), 1, (cloumn + 14) + 1);
- sheet.mergeCells(2, (cloumn + 16), 1, (cloumn + 16) + 1);
- sheet.mergeCells(2, (cloumn + 18), 1, (cloumn + 18) + 1);
- sheet.mergeCells(2, (cloumn + 20), 1, (cloumn + 20) + 1);
- sheet.mergeCells(2, (cloumn + 22), 1, (cloumn + 22) + 1);
- sheet.mergeCells(2, (cloumn + 24), 1, (cloumn + 24) + 1);*/
- sheet.addCell(new Label(2, (cloumn + 24), dto.getYears() + "-12" , format));
- //7.渲染统计
- sheet.addCell(new Label(2, (cloumn + 26), "总计:" + dto.getDavaceprice(), format));
- //8.合并统计
- sheet.mergeCells(2, (cloumn + 26), 5, (cloumn + 26));
- //9.渲染日期
- sheet.addCell(new Label(3, (cloumn + 2), dto.getD1s(), format));
- sheet.addCell(new Label(3, (cloumn + 3), dto.getD1t(), format));
- sheet.addCell(new Label(3, (cloumn + 4), dto.getD2s(), format));
- sheet.addCell(new Label(3, (cloumn + 5), dto.getD2t(), format));
- sheet.addCell(new Label(3, (cloumn + 6), dto.getD3s(), format));
- sheet.addCell(new Label(3, (cloumn + 7), dto.getD3t(), format));
- sheet.addCell(new Label(3, (cloumn + 8), dto.getD4s(), format));
- sheet.addCell(new Label(3, (cloumn + 9), dto.getD4t(), format));
- sheet.addCell(new Label(3, (cloumn + 10), dto.getD5s(), format));
- sheet.addCell(new Label(3, (cloumn + 11), dto.getD5t(), format));
- sheet.addCell(new Label(3, (cloumn + 12), dto.getD6s(), format));
- sheet.addCell(new Label(3, (cloumn + 13), dto.getD6t(), format));
- sheet.addCell(new Label(3, (cloumn + 14), dto.getD7s(), format));
- sheet.addCell(new Label(3, (cloumn + 15), dto.getD7t(), format));
- sheet.addCell(new Label(3, (cloumn + 16), dto.getD8s(), format));
- sheet.addCell(new Label(3, (cloumn + 17), dto.getD8t(), format));
- sheet.addCell(new Label(3, (cloumn + 18), dto.getD9s(), format));
- sheet.addCell(new Label(3, (cloumn + 19), dto.getD9t(), format));
- sheet.addCell(new Label(3, (cloumn + 20), dto.getD10s(), format));
- sheet.addCell(new Label(3, (cloumn + 21), dto.getD10t(), format));
- sheet.addCell(new Label(3, (cloumn + 22), dto.getD11s(), format));
- sheet.addCell(new Label(3, (cloumn + 23), dto.getD11t(), format));
- sheet.addCell(new Label(3, (cloumn + 24), dto.getD12s(), format));
- sheet.addCell(new Label(3, (cloumn + 25), dto.getD12t(), format));
- //10.渲染借支金额
- sheet.addCell(new Label(4, (cloumn + 2), dto.getM1smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 3), dto.getM1tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 4), dto.getM2smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 5), dto.getM2tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 6), dto.getM3smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 7), dto.getM3tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 8), dto.getM4smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 9), dto.getM4tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 10), dto.getM5smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 11), dto.getM5tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 12), dto.getM6smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 13), dto.getM6tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 14), dto.getM7smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 15), dto.getM7tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 16), dto.getM8smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 17), dto.getM8tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 18), dto.getM9smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 19), dto.getM9tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 20), dto.getM10smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 21), dto.getM10tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 22), dto.getM11smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 23), dto.getM11tmoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 24), dto.getM12smoney() + "" , format));
- sheet.addCell(new Label(4, (cloumn + 25), dto.getM12tmoney() + "" , format));
- //11.渲染备注
- sheet.setColumnView(5, 50);//设置第6列的宽度
- sheet.addCell(new Label(5, (cloumn + 2), dto.getM1snote(), format));
- sheet.addCell(new Label(5, (cloumn + 3), dto.getM1tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 4), dto.getM2snote(), format));
- sheet.addCell(new Label(5, (cloumn + 5), dto.getM2tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 6), dto.getM3snote(), format));
- sheet.addCell(new Label(5, (cloumn + 7), dto.getM3tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 8), dto.getM4snote(), format));
- sheet.addCell(new Label(5, (cloumn + 9), dto.getM4tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 10), dto.getM5snote(), format));
- sheet.addCell(new Label(5, (cloumn + 11), dto.getM5tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 12), dto.getM6snote(), format));
- sheet.addCell(new Label(5, (cloumn + 13), dto.getM6tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 14), dto.getM7snote(), format));
- sheet.addCell(new Label(5, (cloumn + 15), dto.getM7tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 16), dto.getM8snote(), format));
- sheet.addCell(new Label(5, (cloumn + 17), dto.getM8tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 18), dto.getM9snote(), format));
- sheet.addCell(new Label(5, (cloumn + 19), dto.getM9tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 20), dto.getM10snote(), format));
- sheet.addCell(new Label(5, (cloumn + 21), dto.getM10tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 22), dto.getM11snote(), format));
- sheet.addCell(new Label(5, (cloumn + 23), dto.getM11tnote(), format));
- sheet.addCell(new Label(5, (cloumn + 24), dto.getM12snote(), format));
- sheet.addCell(new Label(5, (cloumn + 25), dto.getM12tnote(), format));
- cloumn += 25;
- }
- DoubleSummaryStatistics sum = advanceDOList.stream().collect(Collectors.summarizingDouble(AdvanceDO::getDavaceprice));
- WritableCellFormat advacesfarmat = new WritableCellFormat(font2);
- // 追加最后一行
- Label advaceLabel = new Label(0, cloumn + 2, "合计:" + sum.getSum() + "\n\n\n\n\n" , advacesfarmat);
- sheet.addCell(advaceLabel);
- break;
- case "6":
- // 小标题
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "材料名称" , format));
- sheet.addCell(new Label(2, 1, "材料总价格" , format));
- sheet.addCell(new Label(3, 1, "材料描述" , format));
- sheet.addCell(new Label(4, 1, "材料单位" , format));
- sheet.addCell(new Label(5, 1, "分类" , format));
-
- List
materialDOList = (List) list; - double moneys = 0.0;
- for (int i = 0; i < materialDOList.size(); i++) {
- moneys += materialDOList.get(i).getSumprice();
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), materialDOList.get(i).getMaterialname() + "" , format));
- sheet.addCell(new Label(2, (i + 2), materialDOList.get(i).getSumprice().toString(), format));
- sheet.addCell(new Label(3, (i + 2), materialDOList.get(i).getMaterialdecptions() + "" , format));
- sheet.addCell(new Label(4, (i + 2), materialDOList.get(i).getMaterialdanwei() + "" , format));
- sheet.addCell(new Label(5, (i + 2), getProjectname(materialDOList.get(i).getProparentid().toString()), format));
-
- }
- WritableCellFormat materialFormat = new WritableCellFormat(font2);
- // 追加最后一行
- Label materiallabel = new Label(0, materialDOList.size() + 2, "合计:" + String.valueOf(moneys) + "\n\n\n\n\n" , materialFormat);
- sheet.addCell(materiallabel);
- break;
- case "7":
-
-
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "汇款日期" , format));
- sheet.addCell(new Label(2, 1, "收款人" , format));
- sheet.addCell(new Label(3, 1, "汇款金额" , format));
- sheet.addCell(new Label(4, 1, "汇款公司" , format));
- sheet.addCell(new Label(5, 1, "汇款备注" , format));
- sheet.addCell(new Label(6, 1, "汇款银行卡号" , format));
- sheet.addCell(new Label(7, 1, "分类" , format));
- colist = (List
) list; - for (int i = 0; i < colist.size(); i++) {
- money += colist.get(i).getRemittancemoney();
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), colist.get(i).getRemittancedate(), format));
- sheet.addCell(new Label(2, (i + 2), colist.get(i).getRemittancethis(), format));
- sheet.addCell(new Label(3, (i + 2), colist.get(i).getRemittancemoney() + "" , format));
- sheet.addCell(new Label(4, (i + 2), colist.get(i).getRemittancename() + "" , format));
- sheet.addCell(new Label(5, (i + 2), colist.get(i).getRemittancemessage() + "" , format));
- sheet.addCell(new Label(6, (i + 2), colist.get(i).getRemittancemebankcards() + "" , format));
- sheet.addCell(new Label(7, (i + 2), getProjectname(colist.get(i).getProparentid().toString()), format));
- }
- break;
- case "8":
-
-
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "工人名称" , format));
- sheet.addCell(new Label(2, 1, "总工天" , format));
- sheet.addCell(new Label(3, 1, "分类" , format));
- List
labourdayDOList = (List) list; - for (int i = 0; i < labourdayDOList.size(); i++) {
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), labourdayDOList.get(i).getName(), format));
- sheet.addCell(new Label(2, (i + 2), labourdayDOList.get(i).getSumlabourday() + "" , format));
- sheet.addCell(new Label(3, (i + 2), getProjectname(labourdayDOList.get(i).getProparentId().toString()), format));
- }
- break;
-
- case "9":
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "突击人员名称" , format));
- sheet.addCell(new Label(2, 1, "突击日期" , format));
- sheet.addCell(new Label(3, 1, "人数" , format));
- sheet.addCell(new Label(4, 1, "人工费" , format));
- sheet.addCell(new Label(5, 1, "车费" , format));
- sheet.addCell(new Label(6, 1, "总金额" , format));
- sheet.addCell(new Label(7, 1, "分类" , format));
-
-
- List
assaultDOS = (List) list; - double assaultMoney = 0.0;
- for (int i = 0; i < assaultDOS.size(); i++) {
- assaultMoney += assaultDOS.get(i).getSummoney();
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), assaultDOS.get(i).getName() + "" , format));
- sheet.addCell(new Label(2, (i + 2), (assaultDOS.get(i).getAssaultdate()) + "" , format));
- sheet.addCell(new Label(3, (i + 2), assaultDOS.get(i).getCount() + "" , format));
- sheet.addCell(new Label(4, (i + 2), assaultDOS.get(i).getMenmoney() + "" , format));
- sheet.addCell(new Label(5, (i + 2), assaultDOS.get(i).getCarmoney() + "" , format));
- sheet.addCell(new Label(6, (i + 2), assaultDOS.get(i).getSummoney() + "" , format));
- sheet.addCell(new Label(7, (i + 2), getProjectname(assaultDOS.get(i).getProparentId().toString()) + "" , format));
-
- }
- WritableCellFormat assFormat = new WritableCellFormat(font2);
- // 追加最后一行
- Label asslabel = new Label(0, assaultDOS.size() + 2, "合计:" + String.valueOf(assaultMoney) + "\n\n\n\n\n" , assFormat);
- sheet.addCell(asslabel);
- break;
- case "10":
- // 小标题*/
- sheet.addCell(new Label(0, 1, "编号" , format));
- sheet.addCell(new Label(1, 1, "工天记录人" , format));
- sheet.addCell(new Label(2, 1, "日期" , format));
- sheet.addCell(new Label(3, 1, "总工天" , format));
- sheet.addCell(new Label(4, 1, "总金额" , format));
-
- sheet.addCell(new Label(5, 1, "备注" , format));
- sheet.addCell(new Label(6, 1, "分类" , format));
-
-
- List
companydyaDOS = (List) list; - double companydyaMoney = 0.0;
- for (int i = 0; i < companydyaDOS.size(); i++) {
- companydyaMoney += companydyaDOS.get(i).getComoanymoney();
- sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
- sheet.addCell(new Label(1, (i + 2), companydyaDOS.get(i).getCompanythis() + "" , format));
- sheet.addCell(new Label(2, (i + 2), (companydyaDOS.get(i).getCompanydate()) + "" , format));
- sheet.addCell(new Label(3, (i + 2), companydyaDOS.get(i).getCompanyday() + "" , format));
- sheet.addCell(new Label(4, (i + 2), companydyaDOS.get(i).getComoanymoney() + "" , format));
- sheet.addCell(new Label(5, (i + 2), companydyaDOS.get(i).getMessage() + "" , format));
- sheet.addCell(new Label(6, (i + 2), getProjectname(companydyaDOS.get(i).getProparentid().toString()) + "" , format));
-
- }
- WritableCellFormat companydyaFormat = new WritableCellFormat(font2);
- // 追加最后一行
- Label companydyalabel = new Label(0, companydyaDOS.size() + 2, "合计:" + String.valueOf(companydyaMoney) + "\n\n\n\n\n" , companydyaFormat);
- sheet.addCell(companydyalabel);
- break;
- case "11":
-
-
- // 小标题*/
- sheet.addCell(new Label(0, 1, "月份" , format));
- sheet.addCell(new Label(1, 1, "项目名称" , format));
- sheet.addCell(new Label(2, 1, "完成产值" , format));
- sheet.addCell(new Label(3, 1, "所用包工" , format));
- sheet.addCell(new Label(4, 1, "所用点工" , format));
- sheet.addCell(new Label(5, 1, "管理费" , format));
- sheet.addCell(new Label(6, 1, "亏/盈" , format));
- sheet.addCell(new Label(7, 1, "备注" , format));
- List
process = (List) list; - double win = 0.0;
- BigDecimal income = null;//总收入
- int node = process.size() + 2;
- for (int i = 0; i < process.size(); i++) {
- sheet.addCell(new Label(0, (i + 2), process.get(i).getYear() + "" , format));
- sheet.addCell(new Label(1, (i + 2), getProjectname(process.get(i).getProparentId().toString()) + "" , format));
- sheet.addCell(new Label(2, (i + 2), process.get(i).getProduction() + "" , format));
- sheet.addCell(new Label(3, (i + 2), process.get(i).getContractor() + "" , format));
- sheet.addCell(new Label(4, (i + 2), process.get(i).getPointwork() + "" , format));
- sheet.addCell(new Label(5, (i + 2), process.get(i).getMaterial() + "" , format));
- sheet.addCell(new Label(6, (i + 2), process.get(i).getManagement() + "" , format));
- sheet.addCell(new Label(7, (i + 2), process.get(i).getNote() + "" , format));
- win += process.get(i).getWin();
- ;
- }
-
- WritableCellFormat processFormat = new WritableCellFormat(font2);
- // 追加最后一行
-
- Label processLabel = new Label(0, node, "合计:" + formatNumber(win).toString(), processFormat);
- sheet.addCell(processLabel);
- break;
- }
- if (status.equals("7")) {
- WritableCellFormat format3 = new WritableCellFormat(font2);
- // 追加最后一行
- Label label2 = new Label(0, colist.size() + 2, "合计:" + String.valueOf(money), format3);
- sheet.addCell(label2);
- }
-
- // 关闭资源
- wb.write();
- wb.close();
- out.flush();
- out.close();
- }
这段代码显然有很大的优化空间,但是很早入门写的代码,感觉不算太规范吧,但是总这上面来说,优化的空间就在excel上面优化成动态的生产列名和标题,通过循环反射的方式去优化代码即可。采用java8方式构造数据也是可以的。
————没有与生俱来的天赋,都是后天的努力拼搏(我是小杨,谢谢你的关注和支持)