Sfoglia il codice sorgente

导出方法编写

yusm 2 giorni fa
parent
commit
38b672ed97

+ 297 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/util/ExcelUtil.java

@@ -1,5 +1,7 @@
 package com.management.platform.util;
 
+import com.management.platform.entity.vo.ReportRateTaskListVO;
+import com.management.platform.entity.vo.ReportRateTaskVO;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.RegionUtil;
@@ -12,6 +14,8 @@ import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -1478,4 +1482,297 @@ public class ExcelUtil {
         }
         return "/upload/"+fileName;
     }
+
+    public static String exportGeneralExcelWithList(Integer type, ArrayList<ReportRateTaskListVO> list, String path) {
+        String result="系统提示:Excel文件导出成功!";
+        String fileName="";
+        if (type==0){
+            fileName= "任务工时填报及时表_任务"+".xlsx";
+        }else {
+            fileName= "任务工时填报及时表_人员"+".xlsx";
+        }
+
+        try {
+            // 创建工作簿
+            SXSSFWorkbook workBook = new SXSSFWorkbook();
+            // 创建工作类
+
+            Sheet sheetOne = workBook.createSheet();
+            workBook.setSheetName(0,"任务工时填报及时表");
+            sheetOne.setDefaultColumnWidth(16);
+
+            //设置字体样式
+            Font headFont = workBook.createFont();
+            headFont.setBold(true);
+            headFont.setFontHeightInPoints((short) 10);
+            headFont.setFontName("黑体");
+
+            Font titleFont = workBook.createFont();
+            titleFont.setBold(true);
+            titleFont.setFontHeightInPoints((short) 10);
+            titleFont.setFontName("黑体");
+
+            Font font = workBook.createFont();
+            font.setFontHeightInPoints((short) 10);
+            font.setFontName("宋体");
+
+            //设置单元格样式
+            XSSFCellStyle  headStyle = (XSSFCellStyle) workBook.createCellStyle();
+            headStyle.setFont(headFont);
+            headStyle.setAlignment(HorizontalAlignment.CENTER);
+            headStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
+            headStyle.setWrapText(true);
+            headStyle.setBorderBottom(BorderStyle.THIN); //下边框
+            headStyle.setBorderLeft(BorderStyle.THIN);//左边框
+            headStyle.setBorderTop(BorderStyle.THIN);//上边框
+            headStyle.setBorderRight(BorderStyle.THIN);//右边框
+
+
+
+            //设置自定义颜色
+            XSSFColor xssfColor = new XSSFColor();
+            byte[] colorRgb = { (byte)118, (byte)147, (byte)60 };
+            xssfColor.setRGB(colorRgb);
+
+
+            CellStyle titleStyle = workBook.createCellStyle();
+            titleStyle.setFont(titleFont);
+            titleStyle.setAlignment(HorizontalAlignment.CENTER);
+            titleStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
+            titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);  //填充单元格
+            titleStyle.setFillForegroundColor((short)9);    //填色
+            titleStyle.setWrapText(true);
+            titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
+            titleStyle.setBorderLeft(BorderStyle.THIN);//左边框
+            titleStyle.setBorderTop(BorderStyle.THIN);//上边框
+            titleStyle.setBorderRight(BorderStyle.THIN);//右边框
+
+            CellStyle cellStyle = workBook.createCellStyle();
+            cellStyle.setFont(font);
+            cellStyle.setAlignment(HorizontalAlignment.CENTER);
+            cellStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
+            cellStyle.setWrapText(true);
+            cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
+            cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
+            cellStyle.setBorderTop(BorderStyle.THIN);//上边框
+            cellStyle.setBorderRight(BorderStyle.THIN);//右边框
+
+            // 设置边框颜色(重要!)
+            cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
+            cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
+            cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
+            cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
+
+            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+
+
+
+            if (type==0){
+                ArrayList<String> headers = new ArrayList<>();
+                headers.add("所属项目");
+                headers.add("任务");
+                headers.add("任务开始时间");
+                headers.add("任务截止时间");
+                headers.add("人员");
+                headers.add("工号");
+                headers.add("所属部门");
+                headers.add("填报及时率");
+                headers.add("填报及时率(含请假)");
+
+
+                // 创建表头行
+                Row headerRow = sheetOne.createRow(0);
+                for (int i = 0; i < headers.size(); i++) {
+                    Cell cell = headerRow.createCell(i);
+                    cell.setCellValue(headers.get(i));
+                    cell.setCellStyle(titleStyle);
+                }
+
+                int rowNum = 1; // 数据从第2行开始
+                for (ReportRateTaskListVO taskListVO : list) {
+                    List<ReportRateTaskVO> taskDetails = taskListVO.getList();
+                    if (taskDetails == null || taskDetails.isEmpty()) continue;
+
+                    int startRow = rowNum;
+                    int endRow = startRow + taskDetails.size() - 1;
+
+                    // 填写数据
+                    for (int i = 0; i < taskDetails.size(); i++) {
+                        ReportRateTaskVO detail = taskDetails.get(i);
+                        Row row = sheetOne.createRow(rowNum++);
+
+                        // 只在第一行填写任务信息并合并单元格
+                        if (i == 0) {
+                            // 所属项目
+                            Cell projectCell = row.createCell(0);
+                            projectCell.setCellValue(taskListVO.getProjectName());
+                            projectCell.setCellStyle(cellStyle);
+
+                            // 任务名称
+                            Cell taskCell = row.createCell(1);
+                            taskCell.setCellValue(taskListVO.getTaskName());
+                            taskCell.setCellStyle(cellStyle);
+
+                            // 开始时间
+                            Cell startDateCell = row.createCell(2);
+                            String s = taskListVO.getStartDate() == null ? "" :taskListVO.getStartDate().format(formatter);
+                            startDateCell.setCellValue(s);
+                            startDateCell.setCellStyle(cellStyle);
+
+                            // 结束时间
+                            Cell endDateCell = row.createCell(3);
+                            String s1 = taskListVO.getStartDate() == null ? "" :taskListVO.getStartDate().format(formatter);
+                            endDateCell.setCellValue(s1);
+                            endDateCell.setCellStyle(cellStyle);
+
+                            if(taskDetails.size()>1) {
+                                // 先为所有要合并的单元格设置样式
+                                for (int r = startRow; r <= endRow; r++) {
+                                    Row mergeRow = sheetOne.getRow(r) == null ? sheetOne.createRow(r) : sheetOne.getRow(r);
+
+                                    for (int c = 0; c <= 3; c++) { // 0-3列需要合并
+                                        Cell cell = mergeRow.getCell(c) == null ? mergeRow.createCell(c) : mergeRow.getCell(c);
+                                        cell.setCellStyle(cellStyle);
+                                    }
+                                }
+
+                                // 合并项目、任务、时间单元格
+                                sheetOne.addMergedRegion(new CellRangeAddress(startRow, endRow, 0, 0));
+                                sheetOne.addMergedRegion(new CellRangeAddress(startRow, endRow, 1, 1));
+                                sheetOne.addMergedRegion(new CellRangeAddress(startRow, endRow, 2, 2));
+                                sheetOne.addMergedRegion(new CellRangeAddress(startRow, endRow, 3, 3));
+                            }
+                        }
+
+                        // 填写人员信息(每行都填)
+                        // 人员
+                        Cell userCell = row.createCell(4);
+                        userCell.setCellValue(detail.getUserName());
+                        userCell.setCellStyle(cellStyle);
+
+                        // 工号
+                        Cell jobNumCell = row.createCell(5);
+                        jobNumCell.setCellValue(detail.getJobNumber() != null ? detail.getJobNumber() : "");
+                        jobNumCell.setCellStyle(cellStyle);
+
+                        // 所属部门
+                        Cell deptCell = row.createCell(6);
+                        deptCell.setCellValue(detail.getDepartmentName() != null ? detail.getDepartmentName() : "");
+                        deptCell.setCellStyle(cellStyle);
+
+                        // 填报及时率
+                        Cell rateCell = row.createCell(7);
+                        rateCell.setCellValue(detail.getTimelinessRate() != null ? detail.getTimelinessRate() : "");
+                        rateCell.setCellStyle(cellStyle);
+
+                        // 填报及时率(含请假)
+                        Cell rateLeaveCell = row.createCell(8);
+                        rateLeaveCell.setCellValue(detail.getTimelinessRateWithLeave() != null ? detail.getTimelinessRateWithLeave() : "");
+                        rateLeaveCell.setCellStyle(cellStyle);
+                    }
+                }
+            }else {
+                ArrayList<String> headers = new ArrayList<>();
+                headers.add("人员");
+                headers.add("任务");
+                headers.add("任务开始时间");
+                headers.add("任务截止时间");
+                headers.add("填报及时率");
+                headers.add("填报及时率(含请假)");
+                headers.add("总填报及时率");
+                headers.add("总填报及时率(含请假)");
+
+                // 创建表头行
+                Row headerRow = sheetOne.createRow(0);
+                for (int i = 0; i < headers.size(); i++) {
+                    Cell cell = headerRow.createCell(i);
+                    cell.setCellValue(headers.get(i));
+                    cell.setCellStyle(titleStyle);
+                }
+
+                // 定义日期格式化器
+                DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+
+                int rowNum = 1; // 数据从第2行开始
+                for (ReportRateTaskListVO taskListVO : list) {
+                    List<ReportRateTaskVO> taskDetails = taskListVO.getList();
+                    if (taskDetails == null || taskDetails.isEmpty()) continue;
+
+                    int startRow = rowNum;
+                    int endRow = startRow + taskDetails.size() - 1;
+
+                    // 填写数据
+                    for (int i = 0; i < taskDetails.size(); i++) {
+                        ReportRateTaskVO detail = taskDetails.get(i);
+                        Row row = sheetOne.createRow(rowNum++);
+
+                        // 任务名称(每行都填写)
+                        Cell taskCell = row.createCell(1);
+                        taskCell.setCellValue(detail.getTaskName() != null ? detail.getTaskName() : "");
+                        taskCell.setCellStyle(cellStyle);
+
+                        // 只在第一行填写任务信息并合并单元格
+                        if (i == 0) {
+                            // 所属人员
+                            Cell userCell = row.createCell(0);
+                            userCell.setCellValue(taskListVO.getUserName());
+                            userCell.setCellStyle(cellStyle);
+
+                            // 总填报及时率
+                            Cell rateCell = row.createCell(6);
+                            rateCell.setCellValue(taskListVO.getAveragePercentage() != null ? taskListVO.getAveragePercentage() : "");
+                            rateCell.setCellStyle(cellStyle);
+
+                            // 总填报及时率(含请假)
+                            Cell rateLeaveCell = row.createCell(7);
+                            rateLeaveCell.setCellValue(taskListVO.getAverageLeavePercentage() != null ? taskListVO.getAverageLeavePercentage() : "");
+                            rateLeaveCell.setCellStyle(cellStyle);
+
+                            // 合并人员、总填报及时率单元格
+                            if(taskDetails.size() > 1) {
+                                sheetOne.addMergedRegion(new CellRangeAddress(startRow, endRow, 0, 0));
+                                sheetOne.addMergedRegion(new CellRangeAddress(startRow, endRow, 6, 6));
+                                sheetOne.addMergedRegion(new CellRangeAddress(startRow, endRow, 7, 7));
+                            }
+                        }
+
+                        // 开始时间
+                        Cell startDateCell = row.createCell(2);
+                        String startDateStr = detail.getStartDate() == null ? "" : detail.getStartDate().format(dateFormatter);
+                        startDateCell.setCellValue(startDateStr);
+                        startDateCell.setCellStyle(cellStyle);
+
+                        // 结束时间(这里原来错误地使用了startDate,应该使用endDate)
+                        Cell endDateCell = row.createCell(3);
+                        String endDateStr = detail.getEndDate() == null ? "" : detail.getEndDate().format(dateFormatter);
+                        endDateCell.setCellValue(endDateStr);
+                        endDateCell.setCellStyle(cellStyle);
+
+                        // 填报及时率
+                        Cell timelinessRateCell = row.createCell(4);
+                        timelinessRateCell.setCellValue(detail.getTimelinessRate() != null ? detail.getTimelinessRate() : "");
+                        timelinessRateCell.setCellStyle(cellStyle);
+
+                        // 填报及时率(含请假)
+                        Cell timelinessRateWithLeaveCell = row.createCell(5);
+                        timelinessRateWithLeaveCell.setCellValue(detail.getTimelinessRateWithLeave() != null ? detail.getTimelinessRateWithLeave() : "");
+                        timelinessRateWithLeaveCell.setCellStyle(cellStyle);
+                    }
+                }
+            }
+
+            File dir = new File(path);
+            if (!dir.exists()) {
+                dir.mkdirs();
+            }
+            FileOutputStream os = new FileOutputStream(path+fileName);//保存到本地
+            workBook.write(os);
+            os.flush();
+            os.close();
+        }catch(Exception e) {
+            System.out.println(result);
+            e.printStackTrace();
+        }
+        return "/upload/"+fileName;
+    }
 }