Commit 7c82ca23 by wangqi

会议信息导出

parent 100dea2f
...@@ -195,10 +195,21 @@ ...@@ -195,10 +195,21 @@
</dependency> </dependency>
<!-- excel工具 --> <!-- excel工具 -->
<dependency> <!-- <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version> <version>${poi.version}</version>
</dependency>-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.1</version>
</dependency>
<dependency>
<groupId>io.github.draco1023</groupId>
<artifactId>poi-tl-ext</artifactId>
<version>0.4.23-poi5</version>
</dependency> </dependency>
<!-- velocity代码生成使用模板 --> <!-- velocity代码生成使用模板 -->
......
package com.ruoyi.project.ys.controller; package com.ruoyi.project.ys.controller;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.ys.domain.HyjyxxFile;
import com.ruoyi.project.ys.service.IHyjyxxFileService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -21,8 +39,12 @@ import com.ruoyi.framework.web.domain.AjaxResult; ...@@ -21,8 +39,12 @@ import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.framework.web.page.TableDataInfo;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.*;
/** /**
* 会议信息Controller * 会议信息Controller
...@@ -131,4 +153,199 @@ public class HyjyxxController extends BaseController ...@@ -131,4 +153,199 @@ public class HyjyxxController extends BaseController
return success(hyjyxxService.selectHyjyxxById_xq(id)); return success(hyjyxxService.selectHyjyxxById_xq(id));
} }
@PostMapping("/exportWord_YY")
public void exportWord_YY(long id,HttpServletResponse response) throws Exception {
Hyjyxx hyxx = hyjyxxService.selectHyjyxxById_xq(id);
InputStream in = null;
XWPFTemplate template = null;
OutputStream os = null;
try {
in = this.getClass().getResourceAsStream("/static/excel/yyzl.docx");
os = response.getOutputStream();
String fileName = hyxx.getHymc() ;
// 设置响应头
response.setContentType("application/force-download");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".docx");
//加载数据
Map<String, Object> data = new HashMap<>();
data.put("hymc", hyxx.getHymc());
String originalJson = hyxx.getYwBzh();
List<Map<String, Object>> speakerList = new ArrayList<>(); // 初始化空列表,避免空指针
if (originalJson != null && !originalJson.isEmpty()) {
try {
ObjectMapper objectMapper = new ObjectMapper();
// 关键:使用 TypeReference 指定 List<Map> 类型
speakerList = objectMapper.readValue(originalJson,
new TypeReference<List<Map<String, Object>>>() {});
} catch (Exception e) {
// 处理 JSON 解析异常(如格式错误)
// 可抛自定义异常或返回空列表
}
}
for (Map<String, Object> raw : speakerList) {
List<Map<String, PictureRenderData>> fileList = new ArrayList<>();
// 获取数据库中的相对路径列表(如 ["/profile/upload/2025/11/03/旗帜.png", ...])
List<String> relativePaths = (List<String>) raw.get("file");
if (relativePaths != null && !relativePaths.isEmpty()) {
for (String relativePath : relativePaths) {
// 1. 拼接绝对路径(关键:根路径 + 相对路径,避免重复路径)
// 本地资源路径
String localPath = RuoYiConfig.getProfile();
// 数据库资源地址
String absolutePath = localPath + StringUtils.substringAfter(relativePath, Constants.RESOURCE_PREFIX);
// 2. 验证文件是否存在
File imgFile = new File(absolutePath);
if (!imgFile.exists() || !imgFile.isFile()) {
continue;
}
// 3. 读取图片原始尺寸(宽/高)
BufferedImage originalImage = ImageIO.read(imgFile);
int originalWidth = originalImage.getWidth(); // 原始宽度
int originalHeight = originalImage.getHeight(); // 原始高度
// 目标宽度固定为500,计算等比缩放后的高度
int targetWidth = 400;
int targetHeight;
if (originalWidth == 0) {
// 避免除零异常,使用默认高度
targetHeight = 400;
} else {
// 等比缩放公式:目标高度 = 原始高度 × (目标宽度 / 原始宽度)
targetHeight = (int) Math.round(originalHeight * (double) targetWidth / originalWidth);
}
PictureRenderData imgData= Pictures.ofLocal(absolutePath).size(targetWidth, targetHeight).create();
Map<String, PictureRenderData> pictures = new HashMap<>();
pictures.put("file",imgData);
fileList.add(pictures);
}
}
raw.put("fileList",fileList);
}
data.put("speakerList", speakerList);
// 配置
Configure config = Configure.builder().useSpringEL()
.build();
XWPFTemplate compile = XWPFTemplate.compile(in, config);
// 加载模板并填充数据
template = compile.render(data);
template.write(os);
os.flush();
} catch (Exception e) {
throw e;
} finally {
if (in != null) {
in.close();
}
if (template != null) {
template.close();
}
if (os != null) {
os.close();
}
}
}
@Autowired
private IHyjyxxFileService hyjyxxFileService;
@PostMapping("/exportWord_JY")
public void exportWord_JY(long id,HttpServletResponse response) throws Exception {
Hyjyxx hyxx = hyjyxxService.selectHyjyxxById(id);
HyjyxxFile file = new HyjyxxFile();
file.setHyid(id);
List<HyjyxxFile> fileList = hyjyxxFileService.selectHyjyxxFileList(file);
InputStream in = null;
XWPFTemplate template = null;
OutputStream os = null;
try {
in = this.getClass().getResourceAsStream("/static/excel/hyjy.docx");
os = response.getOutputStream();
String fileName = hyxx.getHymc() ;
// 设置响应头
response.setContentType("application/force-download");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".docx");
//加载数据
Map<String, Object> data = new HashMap<>();
data.put("hymc", hyxx.getHymc());
data.put("hyjy", hyxx.getHyjy());
List<Map<String, PictureRenderData>> fileList1 = new ArrayList<>();
for (HyjyxxFile file_xx : fileList) {
String localPath = RuoYiConfig.getProfile();
// 数据库资源地址
String absolutePath = localPath + StringUtils.substringAfter(file_xx.getFileUrl(), Constants.RESOURCE_PREFIX);
// 2. 验证文件是否存在
File imgFile = new File(absolutePath);
if (!imgFile.exists() || !imgFile.isFile()) {
continue;
}
// 1. 读取图片原始尺寸(宽/高)
BufferedImage originalImage = ImageIO.read(imgFile);
int originalWidth = originalImage.getWidth(); // 原始宽度
int originalHeight = originalImage.getHeight(); // 原始高度
// 2. 目标宽度固定为500,计算等比缩放后的高度
int targetWidth = 400;
int targetHeight;
if (originalWidth == 0) {
// 避免除零异常,使用默认高度
targetHeight = 400;
} else {
// 等比缩放公式:目标高度 = 原始高度 × (目标宽度 / 原始宽度)
targetHeight = (int) Math.round(originalHeight * (double) targetWidth / originalWidth);
}
PictureRenderData imgData= Pictures.ofLocal(absolutePath).size(targetWidth, targetHeight).create();
Map<String, PictureRenderData> pictures = new HashMap<>();
pictures.put("file",imgData);
fileList1.add(pictures);
}
data.put("fileList", fileList1);
// 配置
Configure config = Configure.builder().useSpringEL()
.build();
XWPFTemplate compile = XWPFTemplate.compile(in, config);
// 加载模板并填充数据
template = compile.render(data);
template.write(os);
os.flush();
} catch (Exception e) {
throw e;
} finally {
if (in != null) {
in.close();
}
if (template != null) {
template.close();
}
if (os != null) {
os.close();
}
}
}
} }
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