|
|
@@ -3,8 +3,6 @@ package com.xunmei.deploy.service.impl;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
-import com.alibaba.excel.EasyExcel;
|
|
|
-import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
@@ -16,15 +14,19 @@ import com.xunmei.common.core.web.page.TableDataInfo;
|
|
|
import com.xunmei.deploy.dao.IotDailyCheckDataMapper;
|
|
|
import com.xunmei.deploy.dto.dailycheck.DailyCheckDataPageDto;
|
|
|
import com.xunmei.deploy.service.IotDailyCheckDataService;
|
|
|
-import com.xunmei.deploy.util.ValueCellWriteHandler;
|
|
|
import com.xunmei.deploy.vo.DailyCheckDataPageVo;
|
|
|
import com.xunmei.deploy.vo.DailyCheckItemExportVo;
|
|
|
import com.xunmei.system.api.RemoteOrgService;
|
|
|
import com.xunmei.system.api.domain.SysOrg;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
@@ -72,17 +74,16 @@ public class IotDailyCheckDataServiceImpl extends ServiceImpl<IotDailyCheckDataM
|
|
|
throw new RuntimeException("未查询到数据");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
List<DailyCheckItemExportVo> exportDataList = new ArrayList<>();
|
|
|
String data = checkData.getData();
|
|
|
DailyCheckReq checkReq = JSON.parseObject(data, DailyCheckReq.class);
|
|
|
- exportDataList.add(new DailyCheckItemExportVo(checkReq.getFsuElectric(),"电器火灾"));
|
|
|
- exportDataList.add(new DailyCheckItemExportVo(checkReq.getVideoRecordComplete(),"昨日录像丢失"));
|
|
|
- exportDataList.add(new DailyCheckItemExportVo(checkReq.getFsuFireFight(),"消防报警"));
|
|
|
- exportDataList.add(new DailyCheckItemExportVo(checkReq.getDisk(),"硬盘状态"));
|
|
|
- exportDataList.add(new DailyCheckItemExportVo(checkReq.getDiagnose(),"视频质量"));
|
|
|
- exportDataList.add(new DailyCheckItemExportVo(checkReq.getFsuOther(),"技防环境"));
|
|
|
- exportDataList.add(new DailyCheckItemExportVo(checkReq.getNet(),"网络状态"));
|
|
|
+ exportDataList.add(new DailyCheckItemExportVo(checkReq.getFsuElectric(), "电器火灾"));
|
|
|
+ exportDataList.add(new DailyCheckItemExportVo(checkReq.getVideoRecordComplete(), "昨日录像丢失"));
|
|
|
+ exportDataList.add(new DailyCheckItemExportVo(checkReq.getFsuFireFight(), "消防报警"));
|
|
|
+ exportDataList.add(new DailyCheckItemExportVo(checkReq.getDisk(), "硬盘状态"));
|
|
|
+ exportDataList.add(new DailyCheckItemExportVo(checkReq.getDiagnose(), "视频质量"));
|
|
|
+ exportDataList.add(new DailyCheckItemExportVo(checkReq.getFsuOther(), "技防环境"));
|
|
|
+ exportDataList.add(new DailyCheckItemExportVo(checkReq.getNet(), "网络状态"));
|
|
|
for (DailyCheckItemExportVo exportVo : exportDataList) {
|
|
|
exportVo.setTitle("智能巡查每日工作登记簿");
|
|
|
exportVo.setTime(LocalDateTimeUtil.format(checkData.getRecordDate(), Constants.HMS_FORMAT));
|
|
|
@@ -90,25 +91,116 @@ public class IotDailyCheckDataServiceImpl extends ServiceImpl<IotDailyCheckDataM
|
|
|
exportVo.setUserName(checkData.getUserName());
|
|
|
}
|
|
|
try {
|
|
|
- String baseHeaderName = checkData.getFileName();
|
|
|
- // 设置响应头
|
|
|
- response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(baseHeaderName, "utf-8"));
|
|
|
- response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
- response.setCharacterEncoding("utf-8");
|
|
|
- // String header = baseHeaderName;
|
|
|
- // 数据导出
|
|
|
- EasyExcel.write(response.getOutputStream(), DailyCheckItemExportVo.class)
|
|
|
- .registerWriteHandler(new ValueCellWriteHandler(baseHeaderName))
|
|
|
- .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
|
- .sheet(baseHeaderName)
|
|
|
- .doWrite(exportDataList);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- // 重置response
|
|
|
- response.reset();
|
|
|
- response.setContentType("application/json");
|
|
|
- response.setCharacterEncoding("utf-8");
|
|
|
+ exportToExcel(exportDataList, response);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void exportToExcel(List<DailyCheckItemExportVo> exportDataList, HttpServletResponse response) throws IOException {
|
|
|
+ // 创建一个新的Excel 工作簿
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
+ // 创建一个工作表sheet
|
|
|
+ Sheet sheet = workbook.createSheet("智能巡查每日工作登记簿");
|
|
|
+
|
|
|
+ // 设置列宽
|
|
|
+ for (int i = 0; i < 5; i++) { // 假设我们有5列
|
|
|
+ sheet.setColumnWidth(i, 256 * 40); // 列宽设为20个字符宽度
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 创建表头
|
|
|
+ createHeader(sheet,exportDataList.get(0));
|
|
|
+
|
|
|
+ // 填充数据
|
|
|
+ int rowNum = 3; // 数据从第四行开始
|
|
|
+ for (DailyCheckItemExportVo exportVo : exportDataList) {
|
|
|
+ Row row = sheet.createRow(rowNum++);
|
|
|
+ row.setHeight((short) 700);
|
|
|
+ int colNum = 0;
|
|
|
+ // 填充行数据,根据你的 exportVo 属性进行调整
|
|
|
+ row.createCell(colNum++).setCellValue(exportVo.getType());
|
|
|
+ row.createCell(colNum++).setCellValue(exportVo.getTotalCount());
|
|
|
+ row.createCell(colNum++).setCellValue(exportVo.getExceptionCount());
|
|
|
+ row.createCell(colNum++).setCellValue(exportVo.getResult());
|
|
|
+ row.createCell(colNum++).setCellValue(exportVo.getResolution());
|
|
|
}
|
|
|
|
|
|
+ // 设置下载响应头
|
|
|
+ String fileName = "智能巡查每日工作登记簿.xlsx";
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
|
|
|
+
|
|
|
+ // 写入数据到输出流
|
|
|
+ try (OutputStream out = response.getOutputStream()) {
|
|
|
+ workbook.write(out);
|
|
|
+ } finally {
|
|
|
+ // 关闭工作簿
|
|
|
+ workbook.close();
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ private static void createHeader(Sheet sheet, DailyCheckItemExportVo exportVo) {
|
|
|
+
|
|
|
+
|
|
|
+ // 第一行表头
|
|
|
+ Row firstRow = sheet.createRow(0);
|
|
|
+ Cell firstRowCell = firstRow.createCell(0);
|
|
|
+ firstRowCell.setCellValue("智能巡查每日工作登记簿");
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4)); // 合并第一行中的8个单元格
|
|
|
+ sheet.setHorizontallyCenter(true);
|
|
|
+ Workbook workbook = sheet.getWorkbook();
|
|
|
+ firstRowCell.setCellStyle(createHeaderCellStyle(workbook,24, HorizontalAlignment.CENTER,true));
|
|
|
+
|
|
|
+
|
|
|
+ // 第二行表头
|
|
|
+ Row secondRow = sheet.createRow(1);
|
|
|
+ Cell dateCell = secondRow.createCell(0);
|
|
|
+ dateCell.setCellValue("日期:" + exportVo.getTime()); // 假设第二行包含日期
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 2)); // 合并第二行中的前3个单元格
|
|
|
+ dateCell.setCellStyle(createHeaderCellStyle(workbook,14,HorizontalAlignment.CENTER,true));
|
|
|
+
|
|
|
+
|
|
|
+ Cell orgCell = secondRow.createCell(3);
|
|
|
+ orgCell.setCellValue("机构:" + exportVo.getOrgName()); // 假设第二行包含日期
|
|
|
+ orgCell.setCellStyle(createHeaderCellStyle(workbook,14,null,true));
|
|
|
+
|
|
|
+
|
|
|
+ Cell userNameCell = secondRow.createCell(4);
|
|
|
+ userNameCell.setCellValue("登记人:" + exportVo.getUserName()); // 假设第二行包含日期
|
|
|
+ userNameCell.setCellStyle(createHeaderCellStyle(workbook,14,null,true));
|
|
|
+
|
|
|
+ // 第三行表头
|
|
|
+ Row thirdRow = sheet.createRow(2);
|
|
|
+ thirdRow.setHeight((short) 700);
|
|
|
+ String[] thirdHeaders = {"检测项", "总数", "异常数", "系统检查结果", "处置意见"};
|
|
|
+ for (int i = 0; i < thirdHeaders.length; i++) {
|
|
|
+ Cell cell = thirdRow.createCell(i);
|
|
|
+ cell.setCellValue(thirdHeaders[i]);
|
|
|
+ // 设置字体加粗
|
|
|
+ cell.setCellStyle(createHeaderCellStyle(workbook,12,null,false));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static CellStyle createHeaderCellStyle(Workbook workbook, int fontSize, HorizontalAlignment alignment,boolean bgColor) {
|
|
|
+ CellStyle headerStyle = workbook.createCellStyle();
|
|
|
+ if (bgColor){
|
|
|
+ headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
+ headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ }
|
|
|
+ // 设置水平居中
|
|
|
+ if (ObjectUtil.isNotNull(alignment)){
|
|
|
+ headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ }
|
|
|
+ Font headerFont = workbook.createFont();
|
|
|
+ //字体大小
|
|
|
+ headerFont.setFontHeightInPoints((short)fontSize);
|
|
|
+ //是否加粗
|
|
|
+ headerFont.setBold(true);
|
|
|
+ headerStyle.setFont(headerFont);
|
|
|
+
|
|
|
+ return headerStyle;
|
|
|
+ }
|
|
|
+
|
|
|
}
|