package com.engine.salary.biz; import com.engine.salary.entity.datacollection.dto.AddUpDeductionListDTO; import com.engine.salary.entity.datacollection.param.AddUpDeductionQueryParam; import com.engine.salary.mapper.datacollection.AddUpDeductionMapper; import org.apache.ibatis.session.SqlSession; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import weaver.conn.mybatis.MyBatisFactory; import weaver.general.BaseBean; import weaver.general.Util; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; public class AddUpDeductionBiz extends BaseBean { /** * 查询列表 * * @param param * @return */ public List list(AddUpDeductionQueryParam param) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { AddUpDeductionMapper mapper = sqlSession.getMapper(AddUpDeductionMapper.class); List list = mapper.list(param); return list; } finally { sqlSession.close(); } } /** * excel标题 */ private final static List title = Arrays.asList("姓名", "个税扣缴义务人", "部门", "手机号", "工号", "证件号码", "入职日期", "累计子女教育", "累计继续教育", "累计住房贷款利息", "累计住房租金", "累计赡养老人"); /** * 导出 * * @param param * @return */ public HSSFWorkbook export(AddUpDeductionQueryParam param) { //获取操作按钮资源 List> rowList = getExcelRowList(param); //获取excel return getWorkbook(rowList); } /** * 获取excel * * @param rowList 行列表 * @return workbook */ private HSSFWorkbook getWorkbook(List> rowList) { HSSFWorkbook workbook = new HSSFWorkbook(); // 设置title样式 HSSFCellStyle titleCellStyle = workbook.createCellStyle(); HSSFFont titleFont = workbook.createFont(); titleFont.setFontName("仿宋"); titleFont.setFontHeightInPoints((short) 15); titleCellStyle.setFont(titleFont); titleCellStyle.setAlignment(HorizontalAlignment.CENTER); titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色 titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置主体样式 HSSFCellStyle cellStyle = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 10);// 设置字体大小 cellStyle.setFont(font);// 选择需要用到的字体格式 cellStyle.setWrapText(true); HSSFSheet sheet = workbook.createSheet("0"); for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); HSSFRow row = sheet.createRow(rowIndex); for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { HSSFCell cell = row.createCell(cellIndex); cell.setCellType(CellType.STRING); if (rowIndex == 0) { cell.setCellStyle(titleCellStyle); } else { cell.setCellStyle(cellStyle); } cell.setCellValue(infoList.get(cellIndex)); sheet.setColumnWidth(cellIndex, 35 * 256); } } return workbook; } /** * 获取excel数据行 * * @return 导出数据行集合 */ private List> getExcelRowList(AddUpDeductionQueryParam param) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); List list = list(param); final List> dataRowList = Optional.ofNullable(list) .map(List::stream) .map(operatorStream -> operatorStream.map(dto -> { List cellList = new ArrayList<>(); cellList.add(Util.null2String(dto.getUsername())); cellList.add(Util.null2String(dto.getTaxAgentName())); cellList.add(Util.null2String(dto.getDepartmentName())); cellList.add(Util.null2String(dto.getMobile())); cellList.add(Util.null2String(dto.getJobNum())); cellList.add(Util.null2String(dto.getIdNo())); cellList.add(dto.getHiredate()==null?"":formatter.format(dto.getHiredate())); cellList.add(String.valueOf(dto.getAddUpChildEducation())); cellList.add(String.valueOf(dto.getAddUpContinuingEducation())); cellList.add(String.valueOf(dto.getAddUpHousingLoanInterest())); cellList.add(String.valueOf(dto.getAddUpHousingRent())); cellList.add(String.valueOf(dto.getAddUpSupportElderly())); return cellList; }).collect(Collectors.toList())) .orElse(Collections.emptyList()); List> rowList = new ArrayList<>(); rowList.add(title); rowList.addAll(dataRowList); return rowList; } }