Skip to content

Commit

Permalink
Merge pull request #125 from lsqGitHub716/master
Browse files Browse the repository at this point in the history
autopoi通过word模板生成word时:三目、求长、常量、日期转换没起效果
  • Loading branch information
zhangdaiscott authored Sep 6, 2024
2 parents 029ddd5 + 3b0d935 commit 19120a7
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
*/
package org.jeecgframework.poi.excel.entity;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl;

import java.io.IOException;
import java.io.InputStream;

/**
* 模板导出参数设置
*
Expand All @@ -34,6 +39,10 @@ public class TemplateExportParams extends ExcelBaseParams {
* 模板的路径
*/
private String templateUrl;
/**
* 模板
*/
private Workbook templateWb;

/**
* 需要导出的第几个 sheetNum,默认是第0个
Expand Down Expand Up @@ -127,7 +136,46 @@ public TemplateExportParams(String templateUrl, String sheetName, Integer... she
this.sheetNum = sheetNum;
}
}
//update-begin-author:liusq---date:2024-09-03--for: [issues/7048]TemplateExportParams类建议增加传入模板文件InputStream的方式
/**
* 构造器
* @param inputStream 输入流
* @param scanAllsheet 是否输出全部的sheet
* @param sheetName sheet的名称,可不填
*/
public TemplateExportParams(InputStream inputStream, boolean scanAllsheet, String... sheetName) throws IOException {
this.templateWb = WorkbookFactory.create(inputStream);
this.scanAllsheet = scanAllsheet;
if (sheetName != null && sheetName.length > 0) {
this.sheetName = sheetName;
}
}
/**
* 构造器
* @param inputStream 输入流
* @param sheetNum sheet 的位置,可不填
*/
public TemplateExportParams(InputStream inputStream, Integer... sheetNum) throws IOException {
this.templateWb = WorkbookFactory.create(inputStream);
if (sheetNum != null && sheetNum.length > 0) {
this.sheetNum = sheetNum;
}
}

/**
* 单个sheet输出构造器
* @param inputStream 输入流
* @param sheetName sheet的名称
* @param sheetNum sheet的位置,可不填
*/
public TemplateExportParams(InputStream inputStream, String sheetName, Integer... sheetNum) throws IOException {
this.templateWb = WorkbookFactory.create(inputStream);
this.sheetName = new String[] { sheetName };
if (sheetNum != null && sheetNum.length > 0) {
this.sheetNum = sheetNum;
}
}
//update-end-author:liusq---date:2024-09-03--for: [issues/7048]TemplateExportParams类建议增加传入模板文件InputStream的方式
public int getHeadingRows() {
return headingRows;
}
Expand Down Expand Up @@ -215,4 +263,11 @@ public boolean isColForEach() {
public void setColForEach(boolean colForEach) {
this.colForEach = colForEach;
}
public Workbook getTemplateWb() {
return templateWb;
}

public void setTemplateWb(Workbook templateWb) {
this.templateWb = templateWb;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,20 @@ public int getOneObjectSize(Object t, List<ExcelExportEntity> excelParams) throw

public Workbook createExcleByTemplate(TemplateExportParams params, Class<?> pojoClass, Collection<?> dataSet, Map<String, Object> map) {
// step 1. 判断模板的地址
if (params == null || map == null || StringUtils.isEmpty(params.getTemplateUrl())) {
if (params == null || map == null || (StringUtils.isEmpty(params.getTemplateUrl()) && params.getTemplateWb() == null)) {
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
}
Workbook wb = null;
// step 2. 判断模板的Excel类型,解析模板
try {
this.teplateParams = params;
wb = getCloneWorkBook();
//update-begin-author:liusq---date:2024-09-03--for: [issues/7048]TemplateExportParams类建议增加传入模板文件InputStream的方式
if (params.getTemplateWb() != null) {
wb = params.getTemplateWb();
} else {
wb = getCloneWorkBook();
}
//update-end-author:liusq---date:2024-09-03--for: [issues/7048]TemplateExportParams类建议增加传入模板文件InputStream的方式
// 创建表格样式
setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle().getConstructor(Workbook.class).newInstance(wb));
// step 3. 解析模板
Expand Down
59 changes: 52 additions & 7 deletions autopoi/src/main/java/org/jeecgframework/poi/util/PoiElUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ public static Object eval(String text, Map<String, Object> map) throws Exception
String tempText = new String(text);
Object obj = innerEval(text, map);
// 如果没有被处理而且这个值找map中存在就处理这个值
if (tempText.equals(obj.toString()) && map.containsKey(tempText.split("\\.")[0])) {
return PoiPublicUtil.getParamsValue(tempText, map);
if (tempText.equals(obj.toString())) {
if (map.containsKey(tempText.split("\\.")[0])) {
return PoiPublicUtil.getParamsValue(text, map);
} else {
return "";
}
}
return obj;
}
Expand Down Expand Up @@ -245,17 +249,58 @@ public static void main(String[] args) {
* @throws Exception
*/
private static Object trinocular(String text, Map<String, Object> map) throws Exception {
// 把多个空格变成一个空格
//update-begin-author:liusq---date:2024-08-07--for: [issues/6925]autopoi通过word模板生成word时:三目、求长、常量、日期转换没起效果
//把多个空格变成一个空格
text = text.replaceAll("\\s{1,}", " ").trim();
String testText = text.substring(0, text.indexOf("?"));
text = text.substring(text.indexOf("?") + 1, text.length()).trim();
text = innerEval(text, map).toString();
String[] keys = text.split(":");
Object first = eval(keys[0].trim(), map);
Object second = eval(keys[1].trim(), map);
String[] keys = text.split(":");
Object first = null, second = null;
if (keys.length > 2) {
if (keys[0].trim().contains("?")) {
String trinocular = keys[0];
for (int i = 1; i < keys.length - 1; i++) {
trinocular += ":" + keys[i];
}
first = evalNoParse(trinocular, map);
second = evalNoParse(keys[keys.length - 1].trim(), map);
} else {
first = evalNoParse(keys[0].trim(), map);
String trinocular = keys[1];
for (int i = 2; i < keys.length; i++) {
trinocular += ":" + keys[i];
}
second = evalNoParse(trinocular, map);
}
} else {
first = evalNoParse(keys[0].trim(), map);
second = evalNoParse(keys[1].trim(), map);
}
return isTrue(testText.split(" "), map) ? first : second;
//update-end-author:liusq---date:2024-08-07--for: [issues/6925]autopoi通过word模板生成word时:三目、求长、常量、日期转换没起效果
}
/**
* 解析字符串,支持 le,fd,fn,!if,三目 找不到返回原值
*
* @param text
* @param map
* @return
* @throws Exception
*/
public static Object evalNoParse(String text, Map<String, Object> map) throws Exception {
String tempText = new String(text);
Object obj = innerEval(text, map);
//如果没有被处理而且这个值找map中存在就处理这个值,找不到就返回空字符串
if (tempText.equals(obj.toString())) {
if (map.containsKey(tempText.split("\\.")[0])) {
return PoiPublicUtil.getParamsValue(tempText, map);
} else {
return obj;
}
}
return obj;
}

/**
* 解析字符串, 不支持 le,fd,fn,!if,三目 ,获取是集合的字段前缀
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,9 @@ public static Object getRealValue(String currentText, Map<String, Object> map) t
String params = "";
while (currentText.indexOf("{{") != -1) {
params = currentText.substring(currentText.indexOf("{{") + 2, currentText.indexOf("}}"));
Object obj = getParamsValue(params.trim(), map);
//update-begin-author:liusq---date:2024-08-07--for: [issues/6925]autopoi通过word模板生成word时:三目、求长、常量、日期转换没起效果
Object obj = PoiElUtil.eval(params.trim(), map);
//update-end-author:liusq---date:2024-08-07--for: [issues/6925]autopoi通过word模板生成word时:三目、求长、常量、日期转换没起效果
// 判断图片或者是集合
// update-begin-author:taoyan date:20210914 for:autopoi模板导出,赋值的方法建议增加空判断或抛出异常说明。 /issues/3005
if(obj==null){
Expand Down
12 changes: 11 additions & 1 deletion docs/修改日志.log
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@ src\test\java\ImportExcelTest.java

---author:chenrui---date:2024/8/1-----for:[issues/6925]xlsx模版导出图片---
src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java
---author:chenrui---date:2024/8/1-----for:[issues/6925]xlsx模版导出图片---
---author:chenrui---date:2024/8/1-----for:[issues/6925]xlsx模版导出图片---

---author:liusq---date:2024/8/7-----for:[issues/6925]autopoi通过word模板生成word时:三目、求长、常量、日期转换没起效果---
autopoi\src\main\java\org\jeecgframework\poi\util\PoiElUtil.java
autopoi\src\main\java\org\jeecgframework\poi\util\PoiPublicUtil.java
---author:liusq---date:2024/8/7-----for:[issues#6096]autopoi通过word模板生成word时:三目、求长、常量、日期转换没起效果---

---author:liusq---date:2024/9/3-----for:[issues/7048]TemplateExportParams类建议增加传入模板文件InputStream的方式---
autopoi\src\main\java\org\jeecgframework\poi\excel\export\template\ExcelExportOfTemplateUtil.java
autopoi\src\main\java\org\jeecgframework\poi\excel\entity\TemplateExportParams.java
---author:liusq---date:2024/9/3-----for:[issues/7048]TemplateExportParams类建议增加传入模板文件InputStream的方式---

0 comments on commit 19120a7

Please sign in to comment.