新创元报表
parent
f4a31f20d9
commit
81a177301b
@ -0,0 +1,14 @@
|
|||||||
|
package com.api.thinktrans.web;
|
||||||
|
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author liang.cheng
|
||||||
|
* @Date 2023/11/17 1:57 PM
|
||||||
|
* @Description: TODO
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Path("/thinktrans/common")
|
||||||
|
public class ReportExportController extends com.engine.thinktrans.web.ReportExportController {
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.engine.thinktrans.service;
|
||||||
|
|
||||||
|
import com.engine.thinktrans.entity.param.TendencyReportParam;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author liang.cheng
|
||||||
|
* @Date 2023/11/17 2:03 PM
|
||||||
|
* @Description: TODO
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public interface ReportExportService {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 年度人力趋势报表
|
||||||
|
* @Author: liang.cheng
|
||||||
|
* @Date: 2023/11/17 2:43 PM
|
||||||
|
* @param: [tendencyReportParam]
|
||||||
|
* @return: org.apache.poi.xssf.usermodel.XSSFWorkbook
|
||||||
|
*/
|
||||||
|
XSSFWorkbook personTendencyExport(TendencyReportParam tendencyReportParam);
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package com.engine.thinktrans.service.impl;
|
||||||
|
|
||||||
|
import com.engine.common.util.ServiceUtil;
|
||||||
|
import com.engine.core.impl.Service;
|
||||||
|
import com.engine.thinktrans.entity.ReportChildrenData;
|
||||||
|
import com.engine.thinktrans.entity.ReportColumns;
|
||||||
|
import com.engine.thinktrans.entity.param.TendencyReportParam;
|
||||||
|
import com.engine.thinktrans.entity.vo.TendencyReportVO;
|
||||||
|
import com.engine.thinktrans.service.ReportCollectService;
|
||||||
|
import com.engine.thinktrans.service.ReportExportService;
|
||||||
|
import com.engine.thinktrans.util.ExcelUtil;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
import weaver.hrm.User;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author liang.cheng
|
||||||
|
* @Date 2023/11/17 2:04 PM
|
||||||
|
* @Description: TODO
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public class ReportExportServiceImpl extends Service implements ReportExportService {
|
||||||
|
|
||||||
|
private ReportCollectService getReportCollectService(User user) {
|
||||||
|
return ServiceUtil.getService(ReportCollectServiceImpl.class,user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public XSSFWorkbook personTendencyExport(TendencyReportParam tendencyReportParam) {
|
||||||
|
|
||||||
|
TendencyReportVO personTendency = getReportCollectService(user).getPersonTendency(tendencyReportParam);
|
||||||
|
|
||||||
|
// 1.工作簿名称
|
||||||
|
String sheetName = "年度人力趋势报表("+tendencyReportParam.getYear()+"年)";
|
||||||
|
// 2.表头(后面动态获取)
|
||||||
|
List<List<Object>> excelSheetData = new ArrayList<>();
|
||||||
|
|
||||||
|
List<ReportColumns> columns = personTendency.getColumns();
|
||||||
|
String[] header = columns.stream()
|
||||||
|
.map(ReportColumns::getTitle)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
|
||||||
|
excelSheetData.add(Arrays.asList(header));
|
||||||
|
|
||||||
|
//数据
|
||||||
|
List<ReportChildrenData> datas = personTendency.getDatas();
|
||||||
|
List<List<Object>> rows = datas.stream()
|
||||||
|
.map(data -> {
|
||||||
|
List<Object> convertedData = new ArrayList<>(data.getData());
|
||||||
|
return convertedData;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
|
||||||
|
excelSheetData.addAll(rows);
|
||||||
|
return ExcelUtil.genWorkbook(excelSheetData, sheetName);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,102 @@
|
|||||||
|
package com.engine.thinktrans.util;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.usermodel.*;
|
||||||
|
import org.apache.poi.ss.util.CellRangeAddress;
|
||||||
|
import org.apache.poi.xssf.usermodel.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description:
|
||||||
|
* @Author: liang.cheng
|
||||||
|
* @Date: 2023/11/17 2:59 PM
|
||||||
|
*/
|
||||||
|
public class ExcelUtil {
|
||||||
|
|
||||||
|
public static XSSFWorkbook genWorkbook(List<List<Object>> rowList, String sheetName) {
|
||||||
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
|
|
||||||
|
// 设置主体样式
|
||||||
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
||||||
|
XSSFFont font = workbook.createFont();
|
||||||
|
font.setFontName("宋体");
|
||||||
|
font.setFontHeightInPoints((short) 14);
|
||||||
|
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
cellStyle.setFont(font);
|
||||||
|
cellStyle.setWrapText(true);
|
||||||
|
cellStyle.setBorderTop(BorderStyle.THIN);
|
||||||
|
cellStyle.setBorderBottom(BorderStyle.THIN);
|
||||||
|
cellStyle.setBorderLeft(BorderStyle.THIN);
|
||||||
|
cellStyle.setBorderRight(BorderStyle.THIN);
|
||||||
|
|
||||||
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
||||||
|
|
||||||
|
//设置标题
|
||||||
|
Row titleRow = sheet.createRow(0);
|
||||||
|
Cell titleCell = titleRow.createCell(0);
|
||||||
|
titleCell.setCellValue(sheetName);
|
||||||
|
// 设置标题样式
|
||||||
|
CellStyle titleStyle = workbook.createCellStyle();
|
||||||
|
Font titleFont1 = workbook.createFont();
|
||||||
|
titleFont1.setFontHeightInPoints((short) 18);
|
||||||
|
titleFont1.setBold(true);
|
||||||
|
titleFont1.setFontName("宋体");
|
||||||
|
titleStyle.setFont(titleFont1);
|
||||||
|
titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||||
|
titleCell.setCellStyle(titleStyle);
|
||||||
|
|
||||||
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 13));
|
||||||
|
|
||||||
|
|
||||||
|
// 设置表头样式
|
||||||
|
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
||||||
|
XSSFFont titleFont = workbook.createFont();
|
||||||
|
titleFont.setFontName("宋体");
|
||||||
|
titleFont.setBold(true);
|
||||||
|
titleFont.setFontHeightInPoints((short) 14);
|
||||||
|
titleCellStyle.setFont(titleFont);
|
||||||
|
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
titleCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
|
||||||
|
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||||
|
titleCellStyle.setBorderTop(BorderStyle.THIN);
|
||||||
|
titleCellStyle.setBorderBottom(BorderStyle.THIN);
|
||||||
|
titleCellStyle.setBorderLeft(BorderStyle.THIN);
|
||||||
|
titleCellStyle.setBorderRight(BorderStyle.THIN);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 自适应宽度
|
||||||
|
//sheet.autoSizeColumn(0, true);
|
||||||
|
//默认列宽
|
||||||
|
sheet.setDefaultColumnWidth(20);
|
||||||
|
//默认行高
|
||||||
|
sheet.setDefaultRowHeightInPoints(18);
|
||||||
|
|
||||||
|
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
||||||
|
List<Object> infoList = rowList.get(rowIndex);
|
||||||
|
XSSFRow row = sheet.createRow(rowIndex+2);
|
||||||
|
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
||||||
|
XSSFCell cell = row.createCell(cellIndex);
|
||||||
|
if (rowIndex == 0) {
|
||||||
|
cell.setCellStyle(titleCellStyle);
|
||||||
|
} else {
|
||||||
|
cell.setCellStyle(cellStyle);
|
||||||
|
}
|
||||||
|
Object o = infoList.get(cellIndex);
|
||||||
|
if (o instanceof String) {
|
||||||
|
cell.setCellType(CellType.STRING);
|
||||||
|
cell.setCellValue(String.valueOf(o));
|
||||||
|
} else if (o instanceof Boolean) {
|
||||||
|
cell.setCellType(CellType.BOOLEAN);
|
||||||
|
cell.setCellValue(String.valueOf(o));
|
||||||
|
} else {
|
||||||
|
cell.setCellType(CellType.STRING);
|
||||||
|
cell.setCellValue(o == null ? "" : o.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return workbook;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.engine.thinktrans.web;
|
||||||
|
|
||||||
|
import com.engine.common.util.ServiceUtil;
|
||||||
|
import com.engine.thinktrans.entity.param.TendencyReportParam;
|
||||||
|
import com.engine.thinktrans.service.ReportExportService;
|
||||||
|
import com.engine.thinktrans.service.impl.ReportExportServiceImpl;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
import weaver.hrm.HrmUserVarify;
|
||||||
|
import weaver.hrm.User;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.QueryParam;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.StreamingOutput;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author liang.cheng
|
||||||
|
* @Date 2023/11/17 1:57 PM
|
||||||
|
* @Description: TODO
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public class ReportExportController {
|
||||||
|
|
||||||
|
private ReportExportService getReportExportService(User user) {
|
||||||
|
return ServiceUtil.getService(ReportExportServiceImpl.class,user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("/personTendency/export")
|
||||||
|
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
|
public Response personTendencyExport(@Context HttpServletRequest request, @Context HttpServletResponse response,
|
||||||
|
@QueryParam("deptIds") String deptIds, @QueryParam("year") Integer year, @QueryParam("useKind") String useKind) {
|
||||||
|
User user = HrmUserVarify.getUser(request, response);
|
||||||
|
TendencyReportParam tendencyReportParam = TendencyReportParam.builder().deptIds(deptIds).year(year).useKind(useKind).build();
|
||||||
|
XSSFWorkbook workbook = getReportExportService(user).personTendencyExport(tendencyReportParam);
|
||||||
|
String time = LocalDate.now().toString();
|
||||||
|
String fileName = "年度人力趋势报表" + time;
|
||||||
|
try {
|
||||||
|
fileName = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
StreamingOutput output = outputStream -> {
|
||||||
|
workbook.write(outputStream);
|
||||||
|
outputStream.flush();
|
||||||
|
};
|
||||||
|
response.setContentType("application/octet-stream");
|
||||||
|
return Response.ok(output).header("Content-disposition", "attachment;filename=" + fileName).header("Cache-Control", "no-cache").build();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue