1
0
chenendian vor 1 Monat
Ursprung
Commit
3af1b266b5

+ 188 - 22
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/controller/cadastre/SupplyController.java

@@ -12,11 +12,17 @@ import com.siwei.common.core.domain.R;
 import com.siwei.common.core.utils.bean.BeanUtils;
 import com.siwei.common.core.utils.poi.ExcelUtil;
 import com.siwei.common.core.web.controller.BaseController;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.gdal.ogr.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 
 /**
@@ -120,31 +126,191 @@ public class SupplyController extends BaseController {
     }
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-    // todo 供应用途分析报表导出接口,前端传入时间范围,后端查询数据并生成Excel文件返回给前端
-    // 前端可以使用a标签的download属性来下载文件,后端需要设置响应头Content-Disposition为attachment; filename=xxx.xlsx
-    // 导出代码逻辑可用参考exportList2 方法。可用自行定义一个新的ExcelVo类来适配供应用途分析报表的数据结构,或者直接使用LandSupplyReportVO中的数据来生成Excel文件。
-    // 最终程呈现的效果见参考图片
     @PostMapping("/purpose/export")
     public void exportPurposeReport(HttpServletResponse response, @RequestParam String startYear, @RequestParam String endYear) {
         try {
-            LandSupplyReportVO res =  supplyService.getTdgyPurposeReport(startYear,endYear);
-            ExcelUtil<YdbpExcelVo> util = new ExcelUtil<>(YdbpExcelVo.class);
-            util.exportExcel(response, null, "报批数据");
+            LandSupplyReportVO res = supplyService.getTdgyPurposeReport(startYear, endYear);
+
+            if (res == null || CollectionUtils.isEmpty(res.getData())) {
+                return;
+            }
+
+            Workbook workbook = new XSSFWorkbook();
+            Sheet sheet = workbook.createSheet("供应用途分析报表");
+
+            CellStyle titleStyle = workbook.createCellStyle();
+            titleStyle.setAlignment(HorizontalAlignment.CENTER);
+            titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+            Font titleFont = workbook.createFont();
+            titleFont.setBold(true);
+            titleFont.setFontHeightInPoints((short) 14);
+            titleStyle.setFont(titleFont);
+            titleStyle.setBorderTop(BorderStyle.THIN);
+            titleStyle.setBorderBottom(BorderStyle.THIN);
+            titleStyle.setBorderLeft(BorderStyle.THIN);
+            titleStyle.setBorderRight(BorderStyle.THIN);
+
+            CellStyle headerStyle = workbook.createCellStyle();
+            headerStyle.setAlignment(HorizontalAlignment.CENTER);
+            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
+            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+            Font headerFont = workbook.createFont();
+            headerFont.setBold(true);
+            headerStyle.setFont(headerFont);
+            headerStyle.setBorderTop(BorderStyle.THIN);
+            headerStyle.setBorderBottom(BorderStyle.THIN);
+            headerStyle.setBorderLeft(BorderStyle.THIN);
+            headerStyle.setBorderRight(BorderStyle.THIN);
+
+            CellStyle dataStyle = workbook.createCellStyle();
+            dataStyle.setAlignment(HorizontalAlignment.RIGHT);
+            dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+            dataStyle.setBorderTop(BorderStyle.THIN);
+            dataStyle.setBorderBottom(BorderStyle.THIN);
+            dataStyle.setBorderLeft(BorderStyle.THIN);
+            dataStyle.setBorderRight(BorderStyle.THIN);
+
+            CellStyle totalStyle = workbook.createCellStyle();
+            totalStyle.setAlignment(HorizontalAlignment.RIGHT);
+            totalStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+            totalStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
+            totalStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+            Font totalFont = workbook.createFont();
+            totalFont.setBold(true);
+            totalStyle.setFont(totalFont);
+            totalStyle.setBorderTop(BorderStyle.THIN);
+            totalStyle.setBorderBottom(BorderStyle.THIN);
+            totalStyle.setBorderLeft(BorderStyle.THIN);
+            totalStyle.setBorderRight(BorderStyle.THIN);
+
+            List<Integer> years = res.getYears();
+            List<LandSupplyReportVO.LandTypeData> dataList = res.getData();
+
+            int rowIndex = 0;
+
+            Row titleRow = sheet.createRow(rowIndex++);
+            Cell titleCell = titleRow.createCell(0);
+            titleCell.setCellValue("供应用途分析报表(" + startYear + "-" + endYear + ")");
+            titleCell.setCellStyle(titleStyle);
+            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1 + years.size() * 3));
+            titleRow.setHeight((short) 600);
+
+            Row headerRow1 = sheet.createRow(rowIndex++);
+            Row headerRow2 = sheet.createRow(rowIndex++);
+
+            Cell typeHeader = headerRow1.createCell(0);
+            typeHeader.setCellValue("用地类型");
+            typeHeader.setCellStyle(headerStyle);
+            sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 0));
+            headerRow2.createCell(0).setCellStyle(headerStyle);
+
+            int colIndex = 1;
+            String[] categoryNames = {"总计", "出让", "划拨"};
+            for (int i = 0; i < categoryNames.length; i++) {
+                Cell categoryCell = headerRow1.createCell(colIndex);
+                categoryCell.setCellValue(categoryNames[i]);
+                categoryCell.setCellStyle(headerStyle);
+                sheet.addMergedRegion(new CellRangeAddress(1, 1, colIndex, colIndex + years.size() - 1));
+
+                for (int j = 0; j < years.size(); j++) {
+                    Cell yearCell = headerRow2.createCell(colIndex + j);
+                    yearCell.setCellValue(String.valueOf(years.get(j)));
+                    yearCell.setCellStyle(headerStyle);
+                }
+                colIndex += years.size();
+            }
+
+            for (LandSupplyReportVO.LandTypeData landTypeData : dataList) {
+                Row dataRow = sheet.createRow(rowIndex++);
+
+                Cell typeCell = dataRow.createCell(0);
+                typeCell.setCellValue(landTypeData.getLandType());
+                typeCell.setCellStyle(headerStyle);
+
+                int dataColIndex = 1;
+
+                Map<String, Double> totalYearData = landTypeData.getTotal() != null ? landTypeData.getTotal().getYearData() : new LinkedHashMap<>();
+                Map<String, Double> transferYearData = landTypeData.getTransfer() != null ? landTypeData.getTransfer().getYearData() : new LinkedHashMap<>();
+                Map<String, Double> allocationYearData = landTypeData.getAllocation() != null ? landTypeData.getAllocation().getYearData() : new LinkedHashMap<>();
+
+                for (Integer year : years) {
+                    String yearStr = String.valueOf(year);
+                    Cell cell = dataRow.createCell(dataColIndex++);
+                    cell.setCellValue(totalYearData.getOrDefault(yearStr, 0.0));
+                    cell.setCellStyle(dataStyle);
+                }
+                for (Integer year : years) {
+                    String yearStr = String.valueOf(year);
+                    Cell cell = dataRow.createCell(dataColIndex++);
+                    cell.setCellValue(transferYearData.getOrDefault(yearStr, 0.0));
+                    cell.setCellStyle(dataStyle);
+                }
+                for (Integer year : years) {
+                    String yearStr = String.valueOf(year);
+                    Cell cell = dataRow.createCell(dataColIndex++);
+                    cell.setCellValue(allocationYearData.getOrDefault(yearStr, 0.0));
+                    cell.setCellStyle(dataStyle);
+                }
+            }
+
+            Row totalRow = sheet.createRow(rowIndex++);
+            Cell totalLabelCell = totalRow.createCell(0);
+            totalLabelCell.setCellValue("合计");
+            totalLabelCell.setCellStyle(headerStyle);
+
+            int totalColIndex = 1;
+            
+            for (Integer year : years) {
+                String yearStr = String.valueOf(year);
+                double yearTotal = 0.0;
+                for (LandSupplyReportVO.LandTypeData item : dataList) {
+                    if (item.getTotal() != null && item.getTotal().getYearData() != null) {
+                        yearTotal += item.getTotal().getYearData().getOrDefault(yearStr, 0.0);
+                    }
+                }
+                Cell cell = totalRow.createCell(totalColIndex++);
+                cell.setCellValue(yearTotal);
+                cell.setCellStyle(dataStyle);
+            }
+            for (Integer year : years) {
+                String yearStr = String.valueOf(year);
+                double yearTransfer = 0.0;
+                for (LandSupplyReportVO.LandTypeData item : dataList) {
+                    if (item.getTransfer() != null && item.getTransfer().getYearData() != null) {
+                        yearTransfer += item.getTransfer().getYearData().getOrDefault(yearStr, 0.0);
+                    }
+                }
+                Cell cell = totalRow.createCell(totalColIndex++);
+                cell.setCellValue(yearTransfer);
+                cell.setCellStyle(dataStyle);
+            }
+            for (Integer year : years) {
+                String yearStr = String.valueOf(year);
+                double yearAllocation = 0.0;
+                for (LandSupplyReportVO.LandTypeData item : dataList) {
+                    if (item.getAllocation() != null && item.getAllocation().getYearData() != null) {
+                        yearAllocation += item.getAllocation().getYearData().getOrDefault(yearStr, 0.0);
+                    }
+                }
+                Cell cell = totalRow.createCell(totalColIndex++);
+                cell.setCellValue(yearAllocation);
+                cell.setCellStyle(dataStyle);
+            }
+
+            for (int i = 0; i <= 1 + years.size() * 3; i++) {
+                sheet.autoSizeColumn(i);
+            }
+
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setCharacterEncoding("utf-8");
+            String fileName = "供应用途分析报表.xlsx";
+            String encodedName = URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20");
+            response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + encodedName);
+            //response.setHeader("Content-Disposition", "attachment;filename=供应用途分析报表.xlsx");
+
+            workbook.write(response.getOutputStream());
+            workbook.close();
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 1 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/cadastre/impl/SupplyServiceImpl.java

@@ -16,6 +16,7 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDate;
 import java.util.*;
 import java.util.stream.Collectors;