Commit 42ee8357 authored by 周昊's avatar 周昊

1、修改pc端导出项目风险清单表excel列表

2、修改pc端导出风险告知卡图片
parent 7f366b42
......@@ -132,18 +132,22 @@ public class RiskPlanController extends BaseController
RiskNotificationExportWordDto exportWordDto = riskPlanInherentListService.getRiskNotificationExportWordDto(inherentId);
Map<String, Object> dataMap = Convert.convert(Map.class, exportWordDto);
WordUtil word = new WordUtil();
word.exportWord(request, response,dataMap,"风险告知卡");
dataMap.put("title", "风险告知卡");
dataMap.put("path", "D:/ruoyi/uploadPath/upload/");
// 数据填装至模板,保存文件
String name = WordUtil.createDoc(dataMap, "word.ftl");
// word导出
WordUtil.responseDownloadFile(request, response, "D:/ruoyi/uploadPath/upload/", name);
}
/**
* 导出项目风险清单表
*/
@PostMapping("/exportExcel/InherentList/{inherentId}")
public void export(@PathVariable Long inherentId, HttpServletResponse response) {
@PostMapping("/exportExcel/InherentList/{planId}")
public void export(@PathVariable Long planId, HttpServletResponse response) {
try {
List<RiskInherentListExportDto> dtos = riskPlanInherentListService.getPlanInherentListByPlanId(inherentId);
List<RiskInherentListExportDto> dtos = riskPlanInherentListService.getPlanInherentListByPlanId(planId);
riskPlanInherentListService.exportPlanInherentList(response,dtos);
} catch (IOException e) {
e.printStackTrace();
......
......@@ -59,6 +59,5 @@ public class RiskNotificationExportWordDto {
/**
* 应急措施
*/
@TableField("measures_emergency")
private String measuresEmergency;
}
......@@ -16,6 +16,7 @@ import com.censoft.censoftrongtong.mapper.RiskPlanTaskMapper;
import com.censoft.censoftrongtong.service.IRiskPlanInherentListService;
import com.censoft.censoftrongtong.service.IRiskPlanTaskService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.WordUtil;
import com.ruoyi.system.service.ISysUploadFileService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
......@@ -29,6 +30,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
......@@ -235,7 +237,11 @@ public class RiskPlanInherentListServiceImpl
@Override
public RiskNotificationExportWordDto getRiskNotificationExportWordDto(Long inherentId) {
return riskPlanInherentListMapper.getRiskNotificationExportWordDto(inherentId);
RiskNotificationExportWordDto dto = riskPlanInherentListMapper.getRiskNotificationExportWordDto(inherentId);
if (StrUtil.isNotBlank(dto.getSafetyWarningSigns())){
dto.setSafetyWarningSigns(WordUtil.getImageStr("D:\\ruoyi\\uploadPath\\upload\\警告标志\\"+dto.getSafetyWarningSigns()+".png"));
}
return dto;
}
@Override
......
......@@ -423,7 +423,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
LEFT JOIN risk_plan_inherent_list rpil ON rpil.room_id = lr.id
LEFT JOIN risk_plan_existing_list rpel ON rpel.inherent_id = rpil.id
WHERE
rp.id = #{planId}
-- rp.id = #{planId}
rpil.plan_id = #{planId}
AND rpil.id IS NOT NULL
GROUP BY rpel.id
</select>
</mapper>
\ No newline at end of file
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -3,84 +3,146 @@ package com.ruoyi.common.utils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static freemarker.template.Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;
/**
* @author 笑小枫
* @date 2022/7/27
* @see <a href="https://www.xiaoxiaofeng.com">https://www.xiaoxiaofeng.com</a>
* @created: 2020/10/16 16:49
* @author: winston
* @description: 导出Word工具类
*/
@Component
public class WordUtil {
private final Configuration configuration;
@Autowired
private static Logger logger = LogManager.getLogger(WordUtil.class);
/**
* 模板路径
*/
private static final String FTL_FP = "/templates/ftl/";
public WordUtil() {
configuration = new Configuration(DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
configuration.setDefaultEncoding("UTF-8");
private static Configuration configuration = null;
static {
configuration = new Configuration();
//设置默认的编码
configuration.setDefaultEncoding("utf-8");
}
public void createWord(Map<String, Object> dataMap, String templateName, String fileName) {
// 模板文件所在路径
configuration.setClassForTemplateLoading(this.getClass(), "/templates");
Template t = null;
/**
* @param dataMap 数据库数据
* @param ftl 替换的模板
* @return
* @throws IOException
*/
public static String createDoc(Map dataMap, String ftl) throws IOException {
configuration.setClassForTemplateLoading(WordUtil.class, FTL_FP);
String path = (String) dataMap.get("path");
Template template = null;
try {
// 获取模板文件
t = configuration.getTemplate(templateName, "UTF-8");
// yxkj.ftl为要装载的模板
template = configuration.getTemplate(ftl);
template.setEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
// 导出文件
File outFile = new File(fileName);
try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), StandardCharsets.UTF_8))) {
if (t != null) {
// 将填充数据填入模板文件并输出到目标文件
t.process(dataMap, out);
// 输出文档路径及名称
String fileName = dataMap.get("title") + ".doc";
File outFile = new File(path + fileName);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
logger.error(e1);
fileName = "";
}
try {
template.process(dataMap, out);
out.close();
} catch (TemplateException e2) {
logger.error(e2);
fileName = "";
} catch (IOException e) {
logger.error(e);
fileName = "";
}
return fileName;
}
/**
* 通用文件下载方法
*
* @param response
* @param filePath 文件绝对路径
* this.getClass().getClassLoader().getResource("file").getPath
* ();
* @param fileName 文件名
* @throws IOException
*/
public static void responseDownloadFile(HttpServletRequest request, HttpServletResponse response, String filePath, String fileName) throws IOException {
// 读到流中
InputStream inStream = new FileInputStream(filePath + fileName);
String agent = request.getHeader("User-Agent").toUpperCase();
String encodedfileName = "";
// IE
if (agent.indexOf("MSIE") != -1 || agent.indexOf("TRIDENT") != -1) {
encodedfileName = URLEncoder.encode(fileName, "utf-8");
// 谷歌或火狐
} else if (agent.indexOf("CHROME") != -1 || agent.indexOf("FIREFOX") != -1) {
encodedfileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
} else {
encodedfileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
}
} catch (IOException | TemplateException e1) {
e1.printStackTrace();
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
inStream.close();
}
public void exportWord(HttpServletRequest request, HttpServletResponse response,Map<String, Object> dataMap,String exportName) throws IOException {
// 1.创建临时文件夹
String rootPath = request.getSession().getServletContext().getRealPath("/");
String fileName = UUID.randomUUID().toString().replace("-", "");
// 加载word中的数据信息
WordUtil word = new WordUtil();
word.createWord(dataMap, "word.xml", rootPath + "/" + fileName + ".doc");
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
// 设置浏览器以下载的方式处理该文件名
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(exportName + ".doc", "UTF-8"))));
File file = new File(rootPath + "/" + fileName + ".doc");
try (InputStream fin = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream()) {
// 缓冲区
byte[] buffer = new byte[512];
int bytesToRead;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
public static String getImageStr(String imgFile) {
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
data = new byte[in.available()];
//注:FileInputStream.available()方法可以从输入流中阻断由下一个方法调用这个输入流中读取的剩余字节数
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 删除临时文件
file.delete();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
public static void main(String[] args) {
System.out.println(getImageStr("C:/aqpt/uploadPath/upload/signature/20220114095137.png"));
}
}
......@@ -111,7 +111,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 过滤请求
.authorizeRequests()
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
.antMatchers("/login","/app-api/login","/app-api/risk/plan/test", "/register", "/captchaImage").permitAll()
.antMatchers("/login","/app-api/login", "/register", "/captchaImage").permitAll()
// 静态资源,可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment