模版导出

This commit is contained in:
hzj
2025-10-10 08:49:42 +08:00
parent 64f72ebad4
commit 6f8206954f
2 changed files with 93 additions and 85 deletions

View File

@@ -35,10 +35,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
@@ -167,7 +164,7 @@ public class EasyPoiWordExportServiceImpl implements EasyPoiWordExportService {
Map<String,Object> map = mapper.convertValue(bjReportDTO,Map.class);
WordTemplate.generateWordDownload(wordTemplatePath+"/test.docx", response, "重要敏感用户电压暂降事件监测情况.docx", map);
WordTemplate.generateWordDownload(wordTemplatePath+"/test.docx", response, bjReportDTO.getDateFormat()+"重要敏感用户电压暂降事件监测情况.docx", map);
} catch (Exception e) {
e.printStackTrace();
}
@@ -314,6 +311,8 @@ public class EasyPoiWordExportServiceImpl implements EasyPoiWordExportService {
// 或者直接使用 \n
String result2 = String.join("\n", areaContentList);
bjReportDTO.setAreaContent(result2);
eventTemplateList.sort(Comparator.comparing(map -> (String) map.get("userName")));
bjReportDTO.setCompanyEvent(eventTemplateList);
}
@@ -394,8 +393,12 @@ public class EasyPoiWordExportServiceImpl implements EasyPoiWordExportService {
}
bjReportDTO.setAreaInfo(temStr.toString().substring(0,temStr.length()-1));
if(DateUtil.format(DateUtil.parse(param.getSearchBeginTime()), DatePattern.CHINESE_DATE_FORMATTER).equals(DateUtil.format(DateUtil.parse(param.getSearchEndTime()), DatePattern.CHINESE_DATE_FORMATTER))){
bjReportDTO.setDateFormat(DateUtil.format(DateUtil.parse(param.getSearchBeginTime()), DatePattern.CHINESE_DATE_FORMATTER));
}else {
bjReportDTO.setDateFormat(DateUtil.format(DateUtil.parse(param.getSearchBeginTime()), DatePattern.CHINESE_DATE_FORMATTER)+"-"+DateUtil.format(DateUtil.parse(param.getSearchEndTime()), DatePattern.CHINESE_DATE_FORMATTER));
bjReportDTO.setDateFormat(DateUtil.format(DateUtil.parse(param.getSearchBeginTime()), DatePattern.NORM_DATE_PATTERN));
}
bjReportDTO.setTotalDevice((int) devCount);
bjReportDTO.setTotalSubstation((int) stationCount);
bjReportDTO.setTotalBus((int) busCount);
@@ -464,86 +467,7 @@ public class EasyPoiWordExportServiceImpl implements EasyPoiWordExportService {
return result;
}
/**
* 手动合并表格单元格
* 合并规则:相邻行中指定列的值相同时进行合并
*/
private void mergeTableCells(XWPFDocument doc) {
// 获取文档中的所有表格
List<XWPFTable> tables = doc.getTables();
for (XWPFTable table : tables) {
int rowCount = table.getNumberOfRows();
if (rowCount <= 1) continue;
// 定义需要合并的列索引(根据你的表格结构调整)
int[] mergeColumns = {0, 1}; // 第1列(姓名)和第2列(班级)需要合并
for (int colIndex : mergeColumns) {
mergeVerticalCells(table, colIndex);
}
}
}
/**
* 垂直合并指定列的相同值单元格
*/
private void mergeVerticalCells(XWPFTable table, int colIndex) {
int rowCount = table.getNumberOfRows();
int mergeStartRow = -1;
String previousValue = null;
for (int i = 0; i < rowCount; i++) {
XWPFTableRow row = table.getRow(i);
if (row == null) continue;
XWPFTableCell cell = row.getCell(colIndex);
if (cell == null) continue;
String currentValue = cell.getText();
if (currentValue.equals(previousValue)) {
// 值相同,继续合并区间
if (mergeStartRow == -1) {
mergeStartRow = i - 1;
}
// 如果是最后一行,需要执行合并
if (i == rowCount - 1) {
mergeCells(table, colIndex, mergeStartRow, i);
}
} else {
// 值不同,合并之前的区间
if (mergeStartRow != -1) {
mergeCells(table, colIndex, mergeStartRow, i - 1);
mergeStartRow = -1;
}
previousValue = currentValue;
}
}
}
/**
* 执行单元格合并
*/
private void mergeCells(XWPFTable table, int colIndex, int startRow, int endRow) {
if (startRow >= endRow) return;
for (int i = startRow; i <= endRow; i++) {
XWPFTableRow row = table.getRow(i);
if (row != null) {
XWPFTableCell cell = row.getCell(colIndex);
if (cell != null) {
if (i == startRow) {
// 起始行设置合并属性
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// 后续行设置为继续合并
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
}
}
public static void main(String[] args) {
String temp ="共监测到电压暂降事件%s条涉及%s座变电站";

View File

@@ -2,6 +2,10 @@ package com.njcn.product.event.report.utils;
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import java.io.*;
import java.net.URL;
@@ -61,6 +65,7 @@ public class WordTemplate {
// }
// 渲染文档并响应
XWPFDocument document = WordExportUtil.exportWord07(templatePath, data);
mergeTableCells(document);
try (OutputStream outputStream = response.getOutputStream()) {
document.write(outputStream);
} catch (Exception e) {
@@ -94,7 +99,86 @@ public class WordTemplate {
}
}
/**
* 手动合并表格单元格
* 合并规则:相邻行中指定列的值相同时进行合并
*/
private static void mergeTableCells(XWPFDocument doc) {
// 获取文档中的所有表格
List<XWPFTable> tables = doc.getTables();
for (XWPFTable table : tables) {
int rowCount = table.getNumberOfRows();
if (rowCount <= 1) continue;
// 定义需要合并的列索引(根据你的表格结构调整)
int[] mergeColumns = {0}; // 第1列(姓名)和第2列(班级)需要合并
for (int colIndex : mergeColumns) {
mergeVerticalCells(table, colIndex);
}
}
}
/**
* 垂直合并指定列的相同值单元格
*/
private static void mergeVerticalCells(XWPFTable table, int colIndex) {
int rowCount = table.getNumberOfRows();
int mergeStartRow = -1;
String previousValue = null;
for (int i = 0; i < rowCount; i++) {
XWPFTableRow row = table.getRow(i);
if (row == null) continue;
XWPFTableCell cell = row.getCell(colIndex);
if (cell == null) continue;
String currentValue = cell.getText();
if (currentValue.equals(previousValue)) {
// 值相同,继续合并区间
if (mergeStartRow == -1) {
mergeStartRow = i - 1;
}
// 如果是最后一行,需要执行合并
if (i == rowCount - 1) {
mergeCells(table, colIndex, mergeStartRow, i);
}
} else {
// 值不同,合并之前的区间
if (mergeStartRow != -1) {
mergeCells(table, colIndex, mergeStartRow, i - 1);
mergeStartRow = -1;
}
previousValue = currentValue;
}
}
}
/**
* 执行单元格合并
*/
private static void mergeCells(XWPFTable table, int colIndex, int startRow, int endRow) {
if (startRow >= endRow) return;
for (int i = startRow; i <= endRow; i++) {
XWPFTableRow row = table.getRow(i);
if (row != null) {
XWPFTableCell cell = row.getCell(colIndex);
if (cell != null) {
if (i == startRow) {
// 起始行设置合并属性
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// 后续行设置为继续合并
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
}
}
/**
* 获取模板文件输入流优先从classpath查找再查找绝对路径
*