diff --git a/event_smart/src/main/java/com/njcn/product/event/report/service/impl/EasyPoiWordExportServiceImpl.java b/event_smart/src/main/java/com/njcn/product/event/report/service/impl/EasyPoiWordExportServiceImpl.java index ad9f2c4..88ad83c 100644 --- a/event_smart/src/main/java/com/njcn/product/event/report/service/impl/EasyPoiWordExportServiceImpl.java +++ b/event_smart/src/main/java/com/njcn/product/event/report/service/impl/EasyPoiWordExportServiceImpl.java @@ -66,8 +66,8 @@ public class EasyPoiWordExportServiceImpl implements EasyPoiWordExportService { private final PqUserLedgerMapper pqUserLedgerMapper; private final PqsDicTreeMapper pqsDicTreeMapper; private final ObjectMapper mapper = new ObjectMapper(); - @Value("${business.wordTemplatePath}") - private String wordTemplatePath; +// @Value("${business.wordTemplatePath}") +// private String wordTemplatePath; @Override public void test(HttpServletResponse response, ReportExportParam param) { @@ -164,7 +164,7 @@ public class EasyPoiWordExportServiceImpl implements EasyPoiWordExportService { Map map = mapper.convertValue(bjReportDTO,Map.class); - WordTemplate.generateWordDownload(wordTemplatePath+"/test.docx", response, bjReportDTO.getDateFormat()+"重要敏感用户电压暂降事件监测情况.docx", map); + WordTemplate.generateWordDownload("template/test.docx", response, bjReportDTO.getDateFormat()+"重要敏感用户电压暂降事件监测情况.docx", map); } catch (Exception e) { e.printStackTrace(); } diff --git a/event_smart/src/main/java/com/njcn/product/event/report/utils/WordTemplate.java b/event_smart/src/main/java/com/njcn/product/event/report/utils/WordTemplate.java index cd35cf8..f65cc33 100644 --- a/event_smart/src/main/java/com/njcn/product/event/report/utils/WordTemplate.java +++ b/event_smart/src/main/java/com/njcn/product/event/report/utils/WordTemplate.java @@ -1,6 +1,7 @@ package com.njcn.product.event.report.utils; import cn.afterturn.easypoi.word.WordExportUtil; +import cn.afterturn.easypoi.word.entity.MyXWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; @@ -24,19 +25,19 @@ public class WordTemplate { public static void main(String[] args) throws Exception { // 模板文件路径 // 1. 处理模板路径并获取输入流 - String temPath = getTemplateInputStream("template/test.docx"); - if (temPath == null) { - throw new FileNotFoundException("模板文件不存在: template/test.docx"); - } - // 准备数据 - Map map = new HashMap<>(); - map.put("title", "用户信息表"); - map.put("userList", getUsersData()); // 假设getUsersData()方法返回用户数据列表 - // 导出 Word 文档 - XWPFDocument doc = WordExportUtil.exportWord07(temPath, map); - try (FileOutputStream outStream = new FileOutputStream("user_info.docx")) { - doc.write(outStream); - } +// String temPath = getTemplateInputStream("template/test.docx"); +// if (temPath == null) { +// throw new FileNotFoundException("模板文件不存在: template/test.docx"); +// } +// // 准备数据 +// Map map = new HashMap<>(); +// map.put("title", "用户信息表"); +// map.put("userList", getUsersData()); // 假设getUsersData()方法返回用户数据列表 +// // 导出 Word 文档 +// XWPFDocument doc = WordExportUtil.exportWord07(temPath, map); +// try (FileOutputStream outStream = new FileOutputStream("user_info.docx")) { +// doc.write(outStream); +// } } private static List> getUsersData() { @@ -59,12 +60,14 @@ public class WordTemplate { response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1")); response.setCharacterEncoding("UTF-8"); // 1. 处理模板路径并获取输入流 -// String temPath = getTemplateInputStream(templatePath); -// if (temPath == null) { -// throw new FileNotFoundException("模板文件不存在: " + templatePath); -// } + InputStream temPath = getTemplateInputStream(templatePath); + if (temPath == null) { + throw new FileNotFoundException("模板文件不存在: " + templatePath); + } // 渲染文档并响应 - XWPFDocument document = WordExportUtil.exportWord07(templatePath, data); + MyXWPFDocument document = new MyXWPFDocument (temPath); + + WordExportUtil.exportWord07(document, data); mergeTableCells(document); try (OutputStream outputStream = response.getOutputStream()) { document.write(outputStream); @@ -83,22 +86,22 @@ public class WordTemplate { * @param outputPath 输出文件路径 * @throws IOException 当模板不存在或IO操作失败时抛出 */ - public static void exportWord(String templatePath, Map data, String outputPath) throws IOException { - // 1. 处理模板路径并获取输入流 - String temPath = getTemplateInputStream(templatePath); - if (temPath == null) { - throw new FileNotFoundException("模板文件不存在: " + templatePath); - } - - // 3. 写入输出文件 - try (FileOutputStream outStream = new FileOutputStream(outputPath); XWPFDocument doc = WordExportUtil.exportWord07(temPath, data)) { - // 2. 导出Word文档 - doc.write(outStream); - } catch (Exception e) { - e.printStackTrace(); - } - - } +// public static void exportWord(String templatePath, Map data, String outputPath) throws IOException { +// // 1. 处理模板路径并获取输入流 +// String temPath = getTemplateInputStream(templatePath); +// if (temPath == null) { +// throw new FileNotFoundException("模板文件不存在: " + templatePath); +// } +// +// // 3. 写入输出文件 +// try (FileOutputStream outStream = new FileOutputStream(outputPath); XWPFDocument doc = WordExportUtil.exportWord07(temPath, data)) { +// // 2. 导出Word文档 +// doc.write(outStream); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// +// } /** * 手动合并表格单元格 * 合并规则:相邻行中指定列的值相同时进行合并 @@ -185,19 +188,11 @@ public class WordTemplate { * @param templatePath 模板路径 * @return 输入流,若文件不存在则返回null */ - private static String getTemplateInputStream(String templatePath) { + private static InputStream getTemplateInputStream(String templatePath) { // 1. 尝试从classpath资源加载(适用于Spring Boot项目的resources目录) ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - URL url = classLoader.getResource(templatePath); - if (url != null) { - return url.getPath(); - } + InputStream inputStream = classLoader.getResourceAsStream(templatePath); - // 2. 尝试从绝对路径加载 - File file = new File(templatePath); - if (file.exists() && file.isFile()) { - return file.getPath(); - } - return null; + return inputStream; } } diff --git a/event_smart/src/main/resources/application.yml b/event_smart/src/main/resources/application.yml index ffa4f8d..5f33029 100644 --- a/event_smart/src/main/resources/application.yml +++ b/event_smart/src/main/resources/application.yml @@ -20,9 +20,9 @@ mybatis-plus: #驼峰命名 map-underscore-to-camel-case: true #配置sql日志输出 - #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #关闭日志输出 - log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl +# log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl global-config: db-config: #指定主键生成策略 @@ -38,7 +38,6 @@ db: business: #处理波形数据位置 wavePath: D://Comtrade - wordTemplatePath: D:/wordTemplatePath #wavePath: /usr/local/comtrade #处理临时数据 tempPath: D://file