ruoyi-tpl 文件模板打印后台

This commit is contained in:
lizhengwei 2026-01-06 11:10:14 +08:00
parent 4453b470a7
commit 61560691d9
4 changed files with 163 additions and 34 deletions

View File

@ -5,9 +5,8 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import org.dromara.common.tenant.core.TenantEntity;
import java.time.LocalDateTime;
import java.util.Date;
/**
* 模板配置实体
@ -15,7 +14,7 @@ import java.time.LocalDateTime;
@Getter
@Setter
@TableName("tpl_config")
public class TplConfig extends TenantEntity {
public class TplConfig {
/**
* 主键
@ -23,6 +22,11 @@ public class TplConfig extends TenantEntity {
@TableId(value = "id")
private Long id;
/**
* 租户编号
*/
private String tenantId;
/**
* 模板名称
*/
@ -37,10 +41,9 @@ public class TplConfig extends TenantEntity {
*/
private String tplUrl;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private Date createTime;
private Date updateTime;
/**
* 状态
*/

View File

@ -10,6 +10,7 @@ import com.lizhw.tpl.module.docx.dao.TplConfigMapper;
import com.lizhw.tpl.module.docx.model.TplConfig;
import com.lizhw.tpl.module.docx.service.ITplConfigService;
import com.lizhw.tpl.module.docx.ttt.NiceDoc;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
@ -19,10 +20,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
/**
* 模板配置服务层实现
@ -65,13 +63,8 @@ public class TplConfigServiceImpl implements ITplConfigService {
private LambdaQueryWrapper<TplConfig> buildQueryWrapper(TplConfig tplConfig) {
Map<String, Object> params = tplConfig.getParams();
LambdaQueryWrapper<TplConfig> lqw = Wrappers.lambdaQuery();
lqw.like(StringUtils.isNotBlank(tplConfig.getTplName()), TplConfig::getTplName, tplConfig.getTplName());
lqw.like(StringUtils.isNotBlank(tplConfig.getDescription()), TplConfig::getDescription, tplConfig.getDescription());
lqw.eq(StringUtils.isNotBlank(tplConfig.getStatus()), TplConfig::getStatus, tplConfig.getStatus());
lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
TplConfig::getCreateTime, params.get("beginTime"), params.get("endTime"));
lqw.likeRight(StringUtils.isNotBlank(tplConfig.getTplName()), TplConfig::getTplName, tplConfig.getTplName());
lqw.orderByAsc(TplConfig::getId);
return lqw;
}
@ -138,15 +131,54 @@ public class TplConfigServiceImpl implements ITplConfigService {
TplConfig tplConfig = selectTplConfigById(req.getTplId());
String tplUrl = tplConfig.getTplUrl();
//下载文件
NiceDoc niceDoc = null;
try {
niceDoc = new NiceDoc(tplUrl);
Map<String, Object> labels = new HashMap<>();
//值标签
labels.put("startTime", "1881年9月25日");
labels.put("endTime", "1936年10月19日");
labels.put("title", "精选作品目录");
labels.put("press", "鲁迅同学出版社");
//枚举标签
labels.put("likeBook", 2);
//布尔标签
labels.put("isQ", true);
//等于
labels.put("isNew", 2);
//多选二进制值
labels.put("look", 3);
//if语句
labels.put("showContent", 2);
//日期格式标签
labels.put("printDate", new Date());
labels.put("fileReceiveBy", "陈先生");
labels.put("fileRelation", 2);
labels.put("fileDate", new Date());
//添加头像
labels.put("headImg", "D:\\project\\java\\lizhw\\RuoYi-Vue-Plus\\ruoyi-site\\ruoyi-tpl\\src\\main\\resources\\static\\head.png");
//表格
List<Map<String, Object>> books = new ArrayList<>();
Map<String, Object> book1 = new HashMap<>();
book1.put("name", "汉文学史纲要");
book1.put("time", "1938年鲁迅全集出版社");
books.add(book1);
Map<String, Object> book2 = new HashMap<>();
book2.put("name", "中国小说史略");
book2.put("time", "1923年12月上册1924年6月下册");
books.add(book2);
try (NiceDoc niceDoc = new NiceDoc(tplUrl)) {
niceDoc.pushLabels(req.getParams());
niceDoc.pushTable("books", books);
XWPFDocument docx = niceDoc.getDocx();
niceDoc.save("D:\\project\\", UUID.randomUUID() + ".docx");
} catch (IOException e) {
throw new RuntimeException(e);
}
niceDoc.pushLabels(req.getParams());
niceDoc.save("", UUID.randomUUID() + ".docx");
return "";
}

View File

@ -11,10 +11,7 @@ import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
/**
@ -115,11 +112,10 @@ public class NiceDoc implements Closeable {
* {{tableName:colName}}
*
* @param tableName
* @param list
* @param collection
*/
public void pushTable(String tableName, List<Map<String, Object>> list) {
List<XWPFTable> tables = this.allTables;
for (XWPFTable table : tables) {
public void pushTable(String tableName, Collection<Map<String, Object>> collection) {
for (XWPFTable table : this.allTables) {
boolean isFind = false;
XWPFTableRow baseRow = null;
@ -153,7 +149,7 @@ public class NiceDoc implements Closeable {
//已知数据行开始填充数据
if (baseRow != null) {
int addRowIndex = 1;
for (Map<String, Object> listRow : list) {
for (Map<String, Object> listRow : collection) {
CTRow ctRow = table.getCTTbl().insertNewTr(i + addRowIndex);
XWPFTableRow newRow = new XWPFTableRow(ctRow, table);
copyRowAndPushLabels(newRow, baseRow, listRow);
@ -541,12 +537,10 @@ public class NiceDoc implements Closeable {
* @param path
* @param name
*/
public void save(String path, String name) {
public void save(String path, String name) throws IOException {
try (FileOutputStream outStream = new FileOutputStream(path + name)) {
//removeNullParagraphs();
docx.write(outStream);
} catch (Exception e) {
log.error("保存文件失败!", e);
}
}

View File

@ -0,0 +1,100 @@
package com.lizhw.tpl.module.docx.ttt;
import java.io.IOException;
import java.util.*;
/**
* @author lee
* @emailmiracleren@gmail.com
* @date2023/6/5
*/
public class TestTemplate2 {
static String path = "C:\\Users\\PC\\Documents\\WXWork\\1688855053808853\\Cache\\File\\2026-01\\muban\\";
/**
* 测试示例模板生成word
*/
public static void buildTestDocx() {
Map<String, Object> labels = new HashMap<>();
//值标签
labels.put("tysh", "统一书号test1");
labels.put("sm", "书名test1");
labels.put("hhwb", "第一行\n第二行");
//表格
List<Map<String, Object>> books = new ArrayList<>();
Map<String, Object> book1 = new HashMap<>();
book1.put("ys", "10");
book1.put("ks", "20");
book1.put("yllx", "宣纸");
book1.put("zl", "30");
book1.put("ss", "40");
book1.put("yf", "50");
book1.put("sfl", "60.00");
book1.put("fl", "70");
book1.put("hl", "80");
books.add(book1);
Map<String, Object> book2 = new HashMap<>();
book2.put("ys", "11");
book2.put("ks", "21");
book2.put("yllx", "");
book2.put("zl", "31");
book2.put("ss", "41");
book2.put("yf", "51");
book2.put("sfl", "61.00");
book2.put("fl", "71");
book2.put("hl", "81");
books.add(book2);
//测试示例模板生成word
try (NiceDoc docx = new NiceDoc(path + "发印单-FYD202510000014-模板.docx")){
docx.pushLabels(labels);
docx.pushTable("books", books);
//生成文档
docx.save(path, UUID.randomUUID() + ".docx");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 测试示例模板生成xlsx
*/
public static void buildTestXlsx() {
//测试示例模板生成word
NiceExcel excel = new NiceExcel(path + "test.xlsx");
Map<String, Object> labels = new HashMap<>();
//值标签
labels.put("date", "2023年1月1日");
labels.put("title", "精选作品统计");
//枚举标签
labels.put("likeBook", 2);
//多选二进制值
labels.put("lookType", 3);
//if语句
labels.put("showBanner", 1);
//日期格式标签
labels.put("printDate", new Date());
excel.pushLabels(labels);
//表格
List<Map<String, Object>> books = new ArrayList<>();
for (int i = 0; i <= 10; i++) {
Map<String, Object> book = new HashMap<>();
book.put("name", "汉文学史纲要" + i);
book.put("time", 1900 + i + "");
book.put("intro", "简明扼要的介绍,本书是一本好书,推荐" + i + "");
book.put("byName", "作者" + i + "");
book.put("pages", i * 100);
books.add(book);
}
excel.pushTable("books", books);
//生成文档
excel.save(path, UUID.randomUUID() + ".xlsx");
}
}