2 天以前 809562f45f703f99f9f75618428cef59a6458799
src/main/java/com/ruoyi/staff/controller/PersonalShiftController.java
@@ -17,6 +17,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.Collection;
@@ -67,31 +68,42 @@
    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void exportToExcel(@NotNull(message = "时间不能为空!") String time, String userName, Integer sysDeptId, Boolean isMonth, HttpServletResponse response) throws Exception {
        Map<Object, Object> data;
        ServletOutputStream out = response.getOutputStream();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("requestType","excel");
        response.setHeader("Access-Control-Expose-Headers", "requestType");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + time + ".xlsx");
        if (!isMonth) {
            data = personalShiftService.exportToYearExcel(time, userName, sysDeptId);
            // 设置单元格样式
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(StyleYearUtils.getHeadStyle(), StyleYearUtils.getContentStyle());
            // 保存到第一个sheet中
            EasyExcel.write(response.getOutputStream())
            Map<Object, Object> data = personalShiftService.exportToYearExcel(time, userName, sysDeptId);
            HorizontalCellStyleStrategy style = new HorizontalCellStyleStrategy(
                    StyleYearUtils.getHeadStyle(),
                    StyleYearUtils.getContentStyle()
            );
            EasyExcel.write(out)
                    .head((List<List<String>>) data.get("header"))
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 自适应列宽
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .registerWriteHandler(style)
                    .sheet("年度")
                    .doWrite((Collection<?>) data.get("data"));
        } else {
            data = personalShiftService.exportToMonthExcel(time, userName, sysDeptId);
            // 设置单元格样式
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(StyleMonthUtils.getHeadStyle(), StyleMonthUtils.getContentStyle());
            EasyExcel.write(response.getOutputStream())
            Map<Object, Object> data = personalShiftService.exportToMonthExcel(time, userName, sysDeptId);
            HorizontalCellStyleStrategy style = new HorizontalCellStyleStrategy(
                    StyleMonthUtils.getHeadStyle(),
                    StyleMonthUtils.getContentStyle()
            );
            EasyExcel.write(out)
                    .head((List<List<String>>) data.get("header"))
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .registerWriteHandler(style)
                    .sheet("月度")
                    .doWrite((Collection<?>) data.get("data"));
        }
        out.flush();
    }