Pārlūkot izejas kodu

fix Excel导出

LLL 8 mēneši atpakaļ
vecāks
revīzija
e6d70ab601

+ 51 - 35
jeecg-module-interlock/src/main/java/org/jeecg/modules/history/controller/InterlockDetailHistoryController.java

@@ -1,53 +1,41 @@
 package org.jeecg.modules.history.controller;
 
-import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.stream.Collectors;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.aspect.annotation.AutoLog;
+import org.jeecg.common.system.base.controller.JeecgController;
 import org.jeecg.common.system.query.QueryGenerator;
-import org.jeecg.common.util.oConvertUtils;
 import org.jeecg.modules.detail.dto.InterlockDetailQueryDTO;
 import org.jeecg.modules.detail.mapper.InterlockDetailMapper;
 import org.jeecg.modules.history.dto.InterlockHistoryQueryDTO;
 import org.jeecg.modules.history.entity.InterlockDetailHistory;
 import org.jeecg.modules.history.mapper.InterlockSummaryHistoryMapper;
 import org.jeecg.modules.history.service.IInterlockDetailHistoryService;
-
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import lombok.extern.slf4j.Slf4j;
-
 import org.jeecg.modules.history.vo.InterlockDetailHistoryQueryVO;
 import org.jeecg.modules.history.vo.InterlockHistoryDistinctZZXTVO;
 import org.jeecg.modules.history.vo.InterlockSummaryHistoryVO;
-import org.jeecgframework.poi.excel.ExcelImportUtil;
-import org.jeecgframework.poi.excel.def.NormalExcelConstants;
-import org.jeecgframework.poi.excel.entity.ExportParams;
-import org.jeecgframework.poi.excel.entity.ImportParams;
-import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
-import org.jeecg.common.system.base.controller.JeecgController;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
-import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.servlet.ModelAndView;
-import com.alibaba.fastjson.JSON;
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiOperation;
-import org.jeecg.common.aspect.annotation.AutoLog;
 
- /**
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+/**
  * @Description: 联锁详细信息历史数据表
  * @Author: jeecg-boot
  * @Date:   2024-05-30
@@ -68,10 +56,29 @@ public class InterlockDetailHistoryController extends JeecgController<InterlockD
 	 @SuppressWarnings("all")
 	 private InterlockDetailMapper interlockDetailMapper;
 
+	 /**
+	  * 设置响应结果
+	  *
+	  * @param response    响应结果对象
+	  * @param rawFileName 文件名
+	  * @throws UnsupportedEncodingException 不支持编码异常
+	  */
+	 private void setExcelResponseProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
+		 //设置内容类型
+//		 response.setContentType("application/vnd.vnd.ms-excel"); //xls
+		 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //xlsx
+		 //设置编码格式
+		 response.setCharacterEncoding("utf-8");
+		 //设置导出文件名称(避免乱码)
+		 String fileName = URLEncoder.encode(rawFileName.concat(".xlsx"), "UTF-8");
+		 // 设置响应头
+		 response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
+	 }
+
 
 	 @GetMapping(value = "/sdexportLS")
 	 @ApiOperation("..............手动导出excel")
-	 public void downloadExcel(HttpServletRequest request, HttpServletResponse response, InterlockHistoryQueryDTO dto) {
+	 public void downloadExcel(HttpServletResponse response, InterlockHistoryQueryDTO dto) {
 		 long startTime = System.currentTimeMillis();
 
 		 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
@@ -88,17 +95,26 @@ public class InterlockDetailHistoryController extends JeecgController<InterlockD
 	 	}
 
 		 // 设置响应头使浏览器能够下载文件
-		 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
-		 response.setHeader("Content-Disposition", "attachment; filename="+fileName);
+		 try {
+			 this.setExcelResponseProp(response, fileName);
+		 } catch (UnsupportedEncodingException e) {
+			 e.printStackTrace();
+		 }
 
 		 // 将Excel文件写入响应输出流
 		 try {
 			 workbook.write(response.getOutputStream());
-			 workbook.close();
 		 } catch (IOException e) {
 			 e.printStackTrace();
+		 } finally {
+			 try {
+				 workbook.close();
+			 } catch (IOException e) {
+				 e.printStackTrace();
+			 }
 		 }
 
+
 		 long endTime = System.currentTimeMillis();
 		 long duration = endTime - startTime;
 		 String log = "手动导出Excel程序运行时间(毫秒):" + duration;

+ 6 - 14
jeecg-module-interlock/src/main/java/org/jeecg/modules/history/mapper/xml/InterlockDetailHistoryMapper.xml

@@ -48,18 +48,14 @@
             <if test="dto.controlSystemStatus != null and dto.controlSystemStatus != ''"> and d.control_system_status = #{dto.controlSystemStatus}</if>
             <if test="dto.interlockStatus != null and dto.interlockStatus != ''"> and s.interlock_status = #{dto.interlockStatus}</if>
             <if test="dto.loopHealthLevel != null and dto.loopHealthLevel != ''"> and s.loop_health_level = #{dto.loopHealthLevel}</if>
-            <if test="dto.beginTime != null">
-                <![CDATA[ AND TO_TIMESTAMP(s.tag_time, 'YYYY-MM-DD HH24:MI:SS') > #{dto.beginTime} ]]>
-            </if>
-            <if test="dto.endTime != null">
-                <![CDATA[ AND TO_TIMESTAMP(s.tag_time, 'YYYY-MM-DD HH24:MI:SS') < #{dto.endTime} ]]>
-            </if>
+            <if test="dto.beginTime != null"> and s.tag_time &gt; #{dto.beginTime} </if>
+            <if test="dto.endTime != null"> and s.tag_time &lt; #{dto.endTime} </if>
         </where>
         order by s.tag_time desc
     </select>
 
     <!-- 联锁总历史数据表查询(各种逻辑状态)——用于手动导出  -->
-    <!-- 每个系统的数据 先按联锁总表的生成时间倒序排序,保证新增的联锁在最前面; 再按tag_time倒序排序,使最新发生变化的数据在最前面  -->
+    <!-- 每个系统的数据 先按联锁总表的生成时间倒序排序,s.create_time desc,  ??保证新增的联锁在最前面; 再按tag_time倒序排序,使最新发生变化的数据在最前面  -->
     <select id="getForExport" parameterType="org.jeecg.modules.history.dto.InterlockHistoryQueryDTO" resultType="org.jeecg.modules.history.vo.InterlockSummaryHistoryVO">
         select d.id, d.summaryid, d.interlockname, d.interlock_condition, d.interlock_condition_tag, d.instrument_status, d.control_system_status,
         s.interlock_status, s.loop_health_level, d.bypass, s.tag_time,
@@ -81,14 +77,10 @@
             <if test="controlSystemStatus != null and controlSystemStatus != ''"> and d.control_system_status = #{controlSystemStatus}</if>
             <if test="interlockStatus != null and interlockStatus != ''"> and s.interlock_status = #{interlockStatus}</if>
             <if test="loopHealthLevel != null and loopHealthLevel != ''"> and s.loop_health_level = #{loopHealthLevel}</if>
-            <if test="beginTime != null">
-                <![CDATA[ AND TO_TIMESTAMP(s.tag_time, 'YYYY-MM-DD HH24:MI:SS') > #{beginTime} ]]>
-            </if>
-            <if test="endTime != null">
-                <![CDATA[ AND TO_TIMESTAMP(s.tag_time, 'YYYY-MM-DD HH24:MI:SS') < #{endTime} ]]>
-            </if>
+            <if test="beginTime != null"> and s.tag_time &gt; #{beginTime} </if>
+            <if test="endTime != null"> and s.tag_time &lt; #{endTime} </if>
         </where>
-        order by s.create_time desc, s.tag_time desc
+        order by s.tag_time desc
     </select>
 
     <!-- 日统计 仪表状态(根据联锁id查询对应时间下的联锁条件) -->

+ 9 - 24
jeecg-module-interlock/src/main/java/org/jeecg/modules/history/service/impl/InterlockDetailHistoryServiceImpl.java

@@ -6,13 +6,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.shiro.SecurityUtils;
-import org.jeecg.common.system.vo.LoginUser;
-import org.jeecg.modules.base.entity.InterlockBase;
 import org.jeecg.modules.base.mapper.InterlockBaseMapper;
 import org.jeecg.modules.detail.dto.InterlockDetailQueryDTO;
-import org.jeecg.modules.detail.vo.InterlockDetailQueryVO;
 import org.jeecg.modules.history.convert.InterlockSummaryHistoryConvert;
 import org.jeecg.modules.history.dto.InterlockHistoryQueryDTO;
 import org.jeecg.modules.history.entity.InterlockDetailHistory;
@@ -22,27 +17,13 @@ import org.jeecg.modules.history.service.IInterlockDetailHistoryService;
 import org.jeecg.modules.history.vo.InterlockDetailHistoryQueryVO;
 import org.jeecg.modules.history.vo.InterlockHistoryDistinctZZXTVO;
 import org.jeecg.modules.history.vo.InterlockSummaryHistoryVO;
-import org.jeecg.modules.summary.vo.InterlockSummaryVO;
-import org.jeecgframework.poi.excel.def.NormalExcelConstants;
-import org.jeecgframework.poi.excel.entity.ExportParams;
-import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
-import org.springframework.web.servlet.ModelAndView;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.*;
-import java.net.URLEncoder;
+import java.io.IOException;
 import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.concurrent.*;
-import java.util.stream.Collectors;
+import java.util.Date;
+import java.util.List;
 
 /**
  * @Description: 联锁详细信息历史数据表
@@ -282,8 +263,8 @@ public class InterlockDetailHistoryServiceImpl extends ServiceImpl<InterlockDeta
                         startTagTimeRow = rowNum;
                     }
 
-                    // 合并 "联锁名称" 列单元格
-                    if (idx == 0 || !record.getInterlockname().equals(exportList.get(idx - 1).getInterlockname())) {
+                    // 合并 "联锁名称" 列单元格,基于 "采集时间"
+                    if (idx == 0 || !record.getTagTime().equals(exportList.get(idx - 1).getTagTime()) || !record.getInterlockname().equals(exportList.get(idx - 1).getInterlockname())) {
                         if (idx > 0 && (rowNum - startInterlockNameRow) > 1) {
                             sheet.addMergedRegion(new CellRangeAddress(startInterlockNameRow, rowNum - 1, interlockNameCol, interlockNameCol));
                         }
@@ -310,15 +291,19 @@ public class InterlockDetailHistoryServiceImpl extends ServiceImpl<InterlockDeta
                 }
 
                 // 最后一组合并区域的处理
+                // 合并 "采集时间" 列的最后一组
                 if ((rowNum - startTagTimeRow) > 1) {
                     sheet.addMergedRegion(new CellRangeAddress(startTagTimeRow, rowNum - 1, tagTimeCol, tagTimeCol));
                 }
+                // 合并 "联锁名称" 列的最后一组
                 if ((rowNum - startInterlockNameRow) > 1) {
                     sheet.addMergedRegion(new CellRangeAddress(startInterlockNameRow, rowNum - 1, interlockNameCol, interlockNameCol));
                 }
+                // 合并 "联锁状态" 列的最后一组
                 if ((rowNum - startInterlockStatusRow) > 1) {
                     sheet.addMergedRegion(new CellRangeAddress(startInterlockStatusRow, rowNum - 1, interlockStatusCol, interlockStatusCol));
                 }
+                // 合并 "回路健康状态" 列的最后一组
                 if ((rowNum - startLoopHealthLevelRow) > 1) {
                     sheet.addMergedRegion(new CellRangeAddress(startLoopHealthLevelRow, rowNum - 1, loopHealthLevelCol, loopHealthLevelCol));
                 }