package com.ruoyi.web.controller.api; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.deepoove.poi.XWPFTemplate; import com.ruoyi.common.core.domain.R; import com.ruoyi.system.model.TPartyBuilding; import com.ruoyi.system.query.PartyBuildingListQuery; import com.ruoyi.system.service.TPartyBuildingService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.*; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ClassPathResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.*; import static com.ruoyi.web.controller.common.ExcelExportExample.example1; @Slf4j @RestController @RequestMapping("/partyBuilding") @Api(tags = "党建引领") public class PartyBuildingController { private final TPartyBuildingService partyBuildingService; public PartyBuildingController(TPartyBuildingService partyBuildingService) { this.partyBuildingService = partyBuildingService; } @ApiOperation(value = "党建引领--列表",response = TPartyBuilding.class) @PostMapping(value = "/getPartyBuildingList") public R> getPartyBuildingList(@RequestBody PartyBuildingListQuery query) { Page page = new Page<>(query.getPageNum(), query.getPageSize()); Page pageTPartyBuilding = partyBuildingService.page(page, Wrappers.lambdaQuery() .eq(TPartyBuilding::getPartyType, query.getPartyType()) .orderByDesc(TPartyBuilding::getCreateTime)); return R.ok(pageTPartyBuilding); } @ApiOperation(value = "党建引领--详情",response = TPartyBuilding.class) @PostMapping(value = "/getPartyBuildingDetail/{id}") public R getPartyBuildingDetail(@PathVariable String id) { TPartyBuilding partyBuilding = partyBuildingService.getById(id); partyBuilding.setReadCount(partyBuilding.getReadCount() + 1); partyBuildingService.updateById(partyBuilding); return R.ok(partyBuilding); } // 默认模板路径 - 根据实际情况修改 private static final String DEFAULT_TEMPLATE_PATH = "F:\\code\\mobile_community\\ruoyi-applet\\src\\main\\resources\\11.xlsx"; /** * 导出Excel文件 */ @PostMapping("/excel") public ResponseEntity exportExcel() { try { // 使用提供的模板路径或默认路径 String templatePath = DEFAULT_TEMPLATE_PATH; // 生成Excel文件字节数据 byte[] excelData = createExcelFromTemplate(templatePath, generateTestData()); // 创建资源对象 ByteArrayResource resource = new ByteArrayResource(excelData); // 设置文件名 String filename = "export_data.xlsx"; HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); return ResponseEntity.ok() .headers(headers) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); } catch (Exception e) { throw new RuntimeException("导出Excel文件失败: " + e.getMessage(), e); } } /** * 基于模板创建Excel文件 */ private byte[] createExcelFromTemplate(String templatePath, List> dataList) throws IOException { try (FileInputStream templateStream = new FileInputStream(templatePath); Workbook workbook = WorkbookFactory.create(templateStream); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 // 从第四行开始填充数据(行索引从0开始,第四行索引为3) fillDataToSheet(sheet, dataList, 3); // 写入输出流 workbook.write(outputStream); return outputStream.toByteArray(); } } /** * 填充数据到工作表 */ private void fillDataToSheet(Sheet sheet, List> dataList, int startRowIndex) { if (dataList == null || dataList.isEmpty()) { return; } for (int i = 0; i < dataList.size(); i++) { Row row = sheet.createRow(startRowIndex + i); List rowData = dataList.get(i); // 确保每行有22列数据 for (int j = 0; j < 22; j++) { Cell cell = row.createCell(j); Object value = j < rowData.size() ? rowData.get(j) : ""; setCellValue(cell, value); } } } /** * 设置单元格值 */ private void setCellValue(Cell cell, Object value) { if (value == null) { cell.setCellValue(""); } else if (value instanceof String) { cell.setCellValue((String) value); } else if (value instanceof Integer) { cell.setCellValue((Integer) value); } else if (value instanceof Double) { cell.setCellValue((Double) value); } else if (value instanceof Boolean) { cell.setCellValue((Boolean) value); } else if (value instanceof Long) { cell.setCellValue((Long) value); } else { cell.setCellValue(value.toString()); } } /** * 生成测试数据 */ private List> generateTestData() { List> dataList = new java.util.ArrayList<>(); // 生成5行测试数据 for (int i = 1; i <= 5; i++) { List row = new java.util.ArrayList<>(); // 22列测试数据 row.add("数据" + i + "-1"); row.add("数据" + i + "-2"); row.add(i * 100); row.add(i * 50.5); row.add(i % 2 == 0); // 填充剩余列 for (int j = 5; j < 22; j++) { row.add("值" + i + "-" + (j + 1)); } dataList.add(row); } return dataList; } }