EPC、国网模板
This commit is contained in:
parent
5ef3f97aea
commit
be0bf2fde6
|
|
@ -62,7 +62,11 @@
|
|||
<groupId>com.bonus</groupId>
|
||||
<artifactId>bonus-generator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
<version>3.2.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,133 @@
|
|||
package com.bonus.tool.template;
|
||||
|
||||
import com.bonus.tool.template.util.FreeMarkerUtil;
|
||||
import com.bonus.tool.template.util.WordUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @className:Test
|
||||
* @author:cwchen
|
||||
* @date:2025-04-25-17:05
|
||||
* @version:1.0
|
||||
* @description:
|
||||
*/
|
||||
@Slf4j
|
||||
public class TestEPC {
|
||||
|
||||
public static void download() {
|
||||
Map<String, Object> data = new HashMap<>(16);
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
List<Map<String, Object>> list2 = new ArrayList<>();
|
||||
String fileName = null;
|
||||
try {
|
||||
data.put("title","中老500千伏联网项目(老挝段)EPC总承包项目投标技术文件");
|
||||
data.put("Introduction","项目经理、设计负责人、采购负责人、施工负责人、商务负责人等主要负责人");
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("i",i + 1);
|
||||
map.put("zw","项目经理");
|
||||
map.put("name","王杰" + (i + 1));
|
||||
map.put("zc","工程师");
|
||||
map.put("zsmc","建造师证");
|
||||
map.put("level","一级");
|
||||
map.put("zh","云1532016201673770");
|
||||
map.put("zy","机电工程");
|
||||
map.put("ylbx","正常");
|
||||
map.put("bz","");
|
||||
list.add(map);
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("hasJj",true);
|
||||
if(i == 0 || i== 3){
|
||||
map.put("zy","项目经理简介");
|
||||
map.put("hasJj",true);
|
||||
}else{
|
||||
map.put("zy","项目经理");
|
||||
map.put("hasJj",false);
|
||||
}
|
||||
map.put("name","王杰" + (i + 1));
|
||||
map.put("age","13");
|
||||
map.put("xl","本科");
|
||||
map.put("zc","工程师");
|
||||
map.put("zw","职务");
|
||||
map.put("rz","项目经理");
|
||||
map.put("byxx","2007年6月毕业于三峡大学机械设计制造及其自动化专业");
|
||||
map.put("Introduction","项目经理简历表");
|
||||
List<Map<String,Object>> infoList = new ArrayList<>();
|
||||
for (int j = 0; j < 3; j++){
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("time","2019.3-2020.12");
|
||||
map2.put("xm","乌东德电站送电广东广西输电工程送端电网侧交流配套工程-龙开口、鲁地拉电站改接入丽山换流站线路工程(三标段)");
|
||||
map2.put("zy","项目经理");
|
||||
map2.put("lxr","云南电网建设分公司\n" +
|
||||
"0871-63011815");
|
||||
infoList.add(map2);
|
||||
}
|
||||
List<Map<String,Object>> imgList = new ArrayList<>();
|
||||
for (int j = 0; j < 3; j++){
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
map3.put("width","4959860");
|
||||
map3.put("height","3492375");
|
||||
map3.put("index",(100 * (i + 1)) + (j + 1));
|
||||
if(j == 0){
|
||||
map3.put("base64Url", FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片3.png"));
|
||||
}else if(j == 1){
|
||||
map3.put("base64Url",FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片4.png"));
|
||||
}else if(j == 2){
|
||||
map3.put("width","5325745");
|
||||
map3.put("height","7534910");
|
||||
map3.put("base64Url",FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
}
|
||||
if(i == 0 && j == 0){
|
||||
map3.put("base64Url",FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\1.png"));
|
||||
}
|
||||
imgList.add(map3);
|
||||
}
|
||||
map.put("yjTitle","项目经理业绩");
|
||||
List<Map<String,Object>> yjList = new ArrayList<>();
|
||||
for (int j = 0; j < 3; j++){
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
map3.put("yj","业绩" + (j + 1) + ":" + "乌东德电站送电广东广西输电工程送端电网侧交流配套工程-龙开口、鲁地拉电站改接入丽山换流站线路工程(三标段)");
|
||||
List<Map<String,Object>> imgList2 = new ArrayList<>();
|
||||
for (int k = 0; k < 3; k++) {
|
||||
Map<String, Object> map4 = new HashMap<>();
|
||||
map4.put("index",(10000 * (j + 1)) + (k + 1));
|
||||
map4.put("width","5325745");
|
||||
map4.put("height","7534910");
|
||||
map4.put("base64Url",FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
imgList2.add(map4);
|
||||
}
|
||||
map3.put("imgList",imgList2);
|
||||
yjList.add(map3);
|
||||
}
|
||||
map.put("infoList",infoList);
|
||||
map.put("imgList",imgList);
|
||||
map.put("yjList",yjList);
|
||||
list2.add(map);
|
||||
}
|
||||
data.put("list",list);
|
||||
data.put("list2",list2);
|
||||
WordUtils.exportMillCertificateWord2(null,null, data, "test", "EPC.ftl","C:\\Users\\10488\\Desktop\\test (2)\\");
|
||||
} catch (Exception e) {
|
||||
log.error("EPC下载", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
download();
|
||||
}
|
||||
|
||||
public static String handleValue(String value){
|
||||
if(StringUtils.isBlank(value)){
|
||||
return "";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,326 @@
|
|||
package com.bonus.tool.template;
|
||||
|
||||
import com.bonus.tool.template.util.FreeMarkerUtil;
|
||||
import com.bonus.tool.template.util.WordUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @className:Test
|
||||
* @author:cwchen
|
||||
* @date:2025-04-25-17:05
|
||||
* @version:1.0
|
||||
* @description:
|
||||
*/
|
||||
@Slf4j
|
||||
public class TestGW {
|
||||
|
||||
public static void download() {
|
||||
Map<String, Object> data = new HashMap<>(16);
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
List<Map<String, Object>> list2 = new ArrayList<>();
|
||||
List<Map<String, Object>> list3 = new ArrayList<>();
|
||||
List<Map<String, Object>> list4 = new ArrayList<>();
|
||||
List<Map<String, Object>> list5 = new ArrayList<>();
|
||||
String fileName = null;
|
||||
try {
|
||||
data.put("title", "中老500千伏联网项目(老挝段)EPC总承包项目投标技术文件");
|
||||
// 近年完成的类似项目情况表
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("i", i + 1);
|
||||
map.put("proName", "博诺思测试项目" + (i + 1));
|
||||
map.put("dy", "±800kV");
|
||||
map.put("num", "");
|
||||
map.put("line", "75.95km");
|
||||
map.put("date", "2018年07月17日");
|
||||
map.put("fw", "线路施工");
|
||||
map.put("fr", "中国南方电网有限责任公司超高压输电公司");
|
||||
map.put("phone", "020-38123423");
|
||||
list.add(map);
|
||||
}
|
||||
// 近年完成的类似项目情况-材料图片
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("proName", (i + 1) + "." + "博诺思测试项目" + (i + 1));
|
||||
List<Map<String, Object>> imgList = new ArrayList<>();
|
||||
for (int j = 0; j < 3; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("index", (100 * (i + 1)) + (j + 1));
|
||||
map2.put("width", "5325745");
|
||||
map2.put("height", "7534910");
|
||||
map2.put("base64Url", FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
imgList.add(map2);
|
||||
}
|
||||
map.put("imgList", imgList);
|
||||
list2.add(map);
|
||||
}
|
||||
// 核心分包商业绩证明
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("i", i + 1);
|
||||
map.put("unit", "川东电力集团有限公司" + (i + 1));
|
||||
List<Map<String, Object>> yjList = new ArrayList<>();
|
||||
List<Map<String, Object>> zzList = new ArrayList<>();
|
||||
List<Map<String, Object>> personList = new ArrayList<>();
|
||||
List<Map<String, Object>> personImgList = new ArrayList<>();
|
||||
List<Map<String, Object>> zmImgList = new ArrayList<>();
|
||||
for (int j = 0; j < 3; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("i", j + 1);
|
||||
map2.put("proName", "酒泉-湖南±800kV特高压直流输电线路工程(组塔6段)施工合同" + (j + 1));
|
||||
map2.put("unit", "安徽博诺思");
|
||||
map2.put("money", "300");
|
||||
map2.put("date", "2017年4月1日至2017年10月30日");
|
||||
map2.put("sf", "分包人");
|
||||
map2.put("bz", "");
|
||||
yjList.add(map2);
|
||||
}
|
||||
for (int j = 0; j < 4; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("index", (1000 * (i + 1)) + (j + 1));
|
||||
map2.put("width", "5325745");
|
||||
map2.put("height", "7534910");
|
||||
map2.put("base64Url", FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
zzList.add(map2);
|
||||
}
|
||||
for (int j = 0; j < 4; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("i", (j + 1));
|
||||
map2.put("name", "袁林" + (j + 1));
|
||||
map2.put("zc", "高级");
|
||||
map2.put("zf", "川1442006200805530/632527197109127012");
|
||||
map2.put("fg", "项目经理");
|
||||
map2.put("yj", "揭阳500千伏盘龙站配套220千伏线路工程(基础工程)第一标段施工专业分包、川渝特高压交流工程线路工程施工包17基础施工专业分包");
|
||||
map2.put("bz", "");
|
||||
personList.add(map2);
|
||||
}
|
||||
for (int j = 0; j < 4; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("userType", "项目经理");
|
||||
List<Map<String, Object>> imgList = new ArrayList<>();
|
||||
for (int k = 0; k < 3; k++) {
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
map3.put("index", (10000 * (j + 1)) + (k + 1));
|
||||
map3.put("width", "5325745");
|
||||
map3.put("height", "7534910");
|
||||
map3.put("base64Url", FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
imgList.add(map3);
|
||||
}
|
||||
map2.put("imgList", imgList);
|
||||
personImgList.add(map2);
|
||||
}
|
||||
for (int j = 0; j < 4; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("proName", (j + 1) + "、" + "酒泉-湖南±800kV特高压直流输电线路工程(组塔6段)施工合同");
|
||||
List<Map<String, Object>> imgList = new ArrayList<>();
|
||||
for (int k = 0; k < 3; k++) {
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
map3.put("index", (100000 * (j + 1)) + (k + 1));
|
||||
map3.put("width", "5325745");
|
||||
map3.put("height", "7534910");
|
||||
map3.put("base64Url", FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
imgList.add(map3);
|
||||
}
|
||||
map2.put("imgList", imgList);
|
||||
zmImgList.add(map2);
|
||||
}
|
||||
map.put("yjList", yjList);
|
||||
map.put("zzList", zzList);
|
||||
map.put("personList", personList);
|
||||
map.put("personImgList", personImgList);
|
||||
map.put("zmImgList", zmImgList);
|
||||
list3.add(map);
|
||||
}
|
||||
|
||||
// 项目人员配置情况
|
||||
for (int i = 0; i < 1; i++) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
List<Map<String, Object>> userSettingList = new ArrayList<>();
|
||||
List<Map<String, Object>> otherUserList = new ArrayList<>();
|
||||
List<Map<String, Object>> personImgList = new ArrayList<>();
|
||||
for (int j = 0; j < 3; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("i", j + 1);
|
||||
map2.put("fbbh", "");
|
||||
map2.put("name", "");
|
||||
map2.put("bh", "");
|
||||
map2.put("xmName", "杨跃龙");
|
||||
map2.put("zsmc", "一级注册建造师");
|
||||
map2.put("zch", "云1532010201168898");
|
||||
map2.put("xm", "陈忠海");
|
||||
map2.put("safety", "李永东");
|
||||
map2.put("aqy", "邓宇" +
|
||||
"彭阳春" +
|
||||
"张翔");
|
||||
map2.put("zjy", "普诚" +
|
||||
"李松阳" +
|
||||
"卢绍义");
|
||||
map2.put("zz", "汤合成");
|
||||
userSettingList.add(map2);
|
||||
}
|
||||
for (int j = 0; j < 2; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
List<Map<String, Object>> zsList = new ArrayList<>();
|
||||
map2.put("i", j + 1);
|
||||
map2.put("zw", "项目总工");
|
||||
map2.put("name", "陈忠海");
|
||||
map2.put("zc", "工程师");
|
||||
map2.put("yg", "正式职工");
|
||||
map2.put("bh", "");
|
||||
map2.put("zs", "工程师证");
|
||||
map2.put("jb", "中级");
|
||||
map2.put("zh", "CSG05202101310083");
|
||||
map2.put("zy", "电力工程技术");
|
||||
if (j == 0) {
|
||||
|
||||
} else {
|
||||
Map<String, Object> map4 = new HashMap<>();
|
||||
map4.put("zs", "助理工程师");
|
||||
map4.put("jb", "初级");
|
||||
map4.put("zh", "CSG05202101427936");
|
||||
map4.put("zy", "输配电及用电工程");
|
||||
map4.put("bh", "");
|
||||
zsList.add(map4);
|
||||
}
|
||||
map2.put("zsList", zsList);
|
||||
otherUserList.add(map2);
|
||||
}
|
||||
for (int j = 0; j < 4; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("index", (1000000 * (i + 1)) + (j + 1));
|
||||
map2.put("width", "5325745");
|
||||
map2.put("height", "7534910");
|
||||
map2.put("base64Url", FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
personImgList.add(map2);
|
||||
}
|
||||
map.put("userSettingList", userSettingList);
|
||||
map.put("otherUserList", otherUserList);
|
||||
map.put("personImgList", personImgList);
|
||||
list4.add(map);
|
||||
}
|
||||
// 项目关键人员简历及证明材料
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
List<Map<String, Object>> jlList = new ArrayList<>();
|
||||
List<Map<String, Object>> personImgList = new ArrayList<>();
|
||||
if (i == 0) {
|
||||
map.put("jlTitle", "项目经理简历及证明材料");
|
||||
map.put("userType", "项目经理");
|
||||
for (int j = 0; j < 1; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("type", "项目经理");
|
||||
map2.put("name", "杨跃龙");
|
||||
map2.put("card", "420502197612010155");
|
||||
map2.put("xl", "专科");
|
||||
map2.put("zc", "工程师");
|
||||
map2.put("zs", "一级注册建造师");
|
||||
map2.put("bh", "云1532010201168898");
|
||||
map2.put("rcdate", "2024.06");
|
||||
map2.put("tcDate", "2026.12");
|
||||
List<Map<String, Object>> yjList = new ArrayList<>();
|
||||
for (int k = 0; k < 4; k++) {
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
map3.put("date", "2016.06~2017.11");
|
||||
map3.put("proName", "滇西北至广东±800kV特高压直流输电工程1标");
|
||||
map3.put("gm", "新建铁塔286基,新建线路145.55km");
|
||||
map3.put("content", "线路施工");
|
||||
map3.put("zw", "项目经理");
|
||||
yjList.add(map3);
|
||||
}
|
||||
map2.put("yjList", yjList);
|
||||
jlList.add(map2);
|
||||
}
|
||||
} else {
|
||||
map.put("jlTitle", "项目部质检员及证明材料");
|
||||
map.put("userType", "项目部质检员");
|
||||
for (int j = 0; j < 2; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("type", "项目部质检员" + "(" + (j + 1) + ")");
|
||||
map2.put("name", "杨跃龙" + (j + 1));
|
||||
map2.put("card", "420502197612010155");
|
||||
map2.put("xl", "专科");
|
||||
map2.put("zc", "工程师");
|
||||
map2.put("zs", "一级注册建造师");
|
||||
map2.put("bh", "云1532010201168898");
|
||||
map2.put("rcdate", "2024.06");
|
||||
map2.put("tcDate", "2026.12");
|
||||
List<Map<String, Object>> yjList = new ArrayList<>();
|
||||
for (int k = 0; k < 4; k++) {
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
map3.put("date", "2016.06~2017.11");
|
||||
map3.put("proName", "滇西北至广东±800kV特高压直流输电工程1标");
|
||||
map3.put("gm", "新建铁塔286基,新建线路145.55km");
|
||||
map3.put("content", "线路施工");
|
||||
map3.put("zw", "项目经理");
|
||||
yjList.add(map3);
|
||||
}
|
||||
map2.put("yjList", yjList);
|
||||
jlList.add(map2);
|
||||
}
|
||||
}
|
||||
if (i == 0) {
|
||||
for (int j = 0; j < 1; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("zmcl", "");
|
||||
List<Map<String, Object>> imgList = new ArrayList<>();
|
||||
for (int k = 0; k < 3; k++) {
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
map3.put("index", (1000000 * (j + 1)) + (k + 1));
|
||||
map3.put("width", "5325745");
|
||||
map3.put("height", "7534910");
|
||||
map3.put("base64Url", FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
imgList.add(map3);
|
||||
}
|
||||
map2.put("imgList", imgList);
|
||||
personImgList.add(map2);
|
||||
}
|
||||
} else {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("zmcl", "项目部安全员(" + (j + 1) + ")(邓宇)证明材料");
|
||||
List<Map<String, Object>> imgList = new ArrayList<>();
|
||||
for (int k = 0; k < 3; k++) {
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
map3.put("index", (1000000 * (j + 1)) + (k + 1));
|
||||
map3.put("width", "5325745");
|
||||
map3.put("height", "7534910");
|
||||
map3.put("base64Url", FreeMarkerUtil.getImageBase("C:\\Users\\10488\\Desktop\\图片5.png"));
|
||||
imgList.add(map3);
|
||||
}
|
||||
map2.put("imgList", imgList);
|
||||
personImgList.add(map2);
|
||||
}
|
||||
}
|
||||
map.put("jlList", jlList);
|
||||
map.put("personImgList", personImgList);
|
||||
list5.add(map);
|
||||
}
|
||||
data.put("tbr", "云南送变电工程有限公司");
|
||||
data.put("list", list);
|
||||
data.put("list2", list2);
|
||||
data.put("list3", list3);
|
||||
data.put("list4", list4);
|
||||
data.put("list5", list5);
|
||||
WordUtils.exportMillCertificateWord2(null, null, data, "test", "GW.ftl", "C:\\Users\\10488\\Desktop\\test (2)\\");
|
||||
} catch (Exception e) {
|
||||
log.error("EPC下载", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
download();
|
||||
}
|
||||
|
||||
public static String handleValue(String value) {
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return "";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,143 @@
|
|||
package com.bonus.tool.template.util;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.DefaultObjectWrapper;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Auther: ccw
|
||||
* @Date: 2021/06/30/16:04
|
||||
* @description:
|
||||
*/
|
||||
public class FreeMarkerUtil {
|
||||
private static Logger logger = LoggerFactory.getLogger(FreeMarkerUtil.class);
|
||||
private static final String ENCODING = "UTF-8";
|
||||
private static Configuration cfg = new Configuration();
|
||||
|
||||
//初始化cfg
|
||||
static {
|
||||
//设置模板所在文件夹
|
||||
cfg.setClassForTemplateLoading(FreeMarkerUtil.class, "/templates/word");
|
||||
// setEncoding这个方法一定要设置国家及其编码,不然在ftl中的中文在生成html后会变成乱码
|
||||
cfg.setEncoding(Locale.getDefault(), ENCODING);
|
||||
// 设置对象的包装器
|
||||
cfg.setObjectWrapper(new DefaultObjectWrapper());
|
||||
// 设置异常处理器,这样的话就可以${a.b.c.d}即使没有属性也不会出错
|
||||
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
|
||||
|
||||
}
|
||||
|
||||
//获取模板对象
|
||||
public static Template getTemplate(String templateFileName) throws IOException {
|
||||
return cfg.getTemplate(templateFileName, ENCODING);
|
||||
}
|
||||
|
||||
/**
|
||||
* 45 * 据数据及模板生成文件
|
||||
* 46 * @param data Map的数据结果集
|
||||
* 47 * @param templateFileName ftl模版文件名
|
||||
* 48 * @param outFilePath 生成文件名称(可带路径)
|
||||
* 49
|
||||
*/
|
||||
public static File crateFile(Map<String, Object> data, String templateFileName, String outFilePath) {
|
||||
Writer out = null;
|
||||
File outFile = new File(outFilePath);
|
||||
try {
|
||||
// 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
|
||||
Template template = getTemplate(templateFileName);
|
||||
if (!outFile.getParentFile().exists()) {
|
||||
outFile.getParentFile().mkdirs();
|
||||
}
|
||||
out = new OutputStreamWriter(new FileOutputStream(outFile), ENCODING);
|
||||
// 处理模版
|
||||
template.process(data, out);
|
||||
out.flush();
|
||||
logger.info("由模板文件" + templateFileName + "生成" + outFilePath + "成功.");
|
||||
} catch (Exception e) {
|
||||
logger.error("由模板文件" + templateFileName + "生成" + outFilePath + "出错");
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("关闭Write对象出错", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return outFile;
|
||||
}
|
||||
|
||||
//获得图片的base64码
|
||||
public static String getImageBase(String src) throws Exception {
|
||||
try {
|
||||
if (src == null || src == "") {
|
||||
return "";
|
||||
}
|
||||
File file = new File(src);
|
||||
if (!file.exists()) {
|
||||
return "";
|
||||
}
|
||||
InputStream in = null;
|
||||
byte[] data = null;
|
||||
|
||||
in = new FileInputStream(file);
|
||||
data = new byte[in.available()];
|
||||
in.read(data);
|
||||
in.close();
|
||||
BASE64Encoder encoder = new BASE64Encoder();
|
||||
return encoder.encode(data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getsuffix(String url) {
|
||||
String suffix = url.substring(url.lastIndexOf("."));
|
||||
String replaceStr = url.replace(suffix, ".png");
|
||||
return replaceStr;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String imageBase = getImageBase(getsuffix("C:\\Users\\bonus\\Desktop\\按钮\\选中\\微信图片_20210129113032.jpg"));
|
||||
System.err.println(getsuffix("C:\\Users\\bonus\\Desktop\\按钮\\选中\\微信图片_20210129113032.jpg"));
|
||||
// Map<String, Object> data = new HashMap<String, Object>();
|
||||
// data.put("orgName", "段然涛");
|
||||
// data.put("proName", "男");
|
||||
// data.put("inspectionUser", "测试李");
|
||||
// data.put("inspectionDate", "1994-03-14");
|
||||
// List<Map<String, String>> RectificationList = new ArrayList<>();
|
||||
// Map<String, String> paramsMap = new HashMap<String, String>();
|
||||
// paramsMap.put("index", "1");
|
||||
// paramsMap.put("content", "2008-09");
|
||||
// paramsMap.put("rectifyRemark", "2012-06");
|
||||
// Map<String, String> paramsMap2 = new HashMap<String, String>();
|
||||
// paramsMap2.put("index", "2");
|
||||
// paramsMap2.put("content", "2012-09");
|
||||
// paramsMap2.put("rectifyRemark", "2016-07");
|
||||
// RectificationList.add(paramsMap);
|
||||
// RectificationList.add(paramsMap2);
|
||||
// data.put("RectificationList", RectificationList);
|
||||
// List<String> images = new LinkedList<String>();
|
||||
// images.add(0,getImageBase(getsuffix("C:\\Users\\bonus\\Desktop\\按钮\\选中\\微信图片_20210129113032.jpg")));
|
||||
// images.add(1,getImageBase(getsuffix("C:\\Users\\bonus\\Desktop\\按钮\\选中\\背景.jpg")));
|
||||
// data.put("images", images);
|
||||
//// logger.error("图片的数量"+images.size());
|
||||
// crateFile(data, "质量巡查问题整改单5.ftl", "C:\\Users\\bonus\\Desktop\\文档\\简历5.doc");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,260 @@
|
|||
package com.bonus.tool.template.util;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @Auther: ccw
|
||||
* @Date: 2021/07/22/9:57
|
||||
* @description:
|
||||
*/
|
||||
public class WordUtils {
|
||||
|
||||
@Value("${file.upload_path}")
|
||||
private String UPLOAD_PATH;
|
||||
|
||||
//配置信息,代码本身写的还是很可读的,就不过多注解了
|
||||
private static Configuration configuration = null;
|
||||
|
||||
//这里注意的是利用WordUtils的类加载器动态获得模板文件的位置
|
||||
// private static final String templateFolder = WordUtils.class.getClassLoader().getResource("../../").getPath() + "/templates/word";
|
||||
static {
|
||||
configuration = new Configuration();
|
||||
configuration.setDefaultEncoding("utf-8");
|
||||
configuration.setClassForTemplateLoading(WordUtils.class, "/download/");
|
||||
// try {
|
||||
//
|
||||
//// configuration.setDirectoryForTemplateLoading(new File(templateFolder));
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
|
||||
private WordUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map, String title, String ftlFile) throws IOException {
|
||||
Template freemarkerTemplate = configuration.getTemplate(ftlFile);
|
||||
File file = null;
|
||||
InputStream fin = null;
|
||||
ServletOutputStream out = null;
|
||||
try {
|
||||
// 调用工具类的createDoc方法生成Word文档
|
||||
file = createDoc(map, freemarkerTemplate);
|
||||
fin = new FileInputStream(file);
|
||||
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("application/msword");
|
||||
// 设置浏览器以下载的方式处理该文件名
|
||||
String fileName = title + ".docx";
|
||||
response.setHeader("Content-Disposition", "attachment;filename="
|
||||
.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
|
||||
|
||||
out = response.getOutputStream();
|
||||
byte[] buffer = new byte[512]; // 缓冲区
|
||||
int bytesToRead = -1;
|
||||
// 通过循环将读入的Word文件的内容输出到浏览器中
|
||||
while ((bytesToRead = fin.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesToRead);
|
||||
}
|
||||
} finally {
|
||||
if (fin != null) {fin.close();}
|
||||
if (out != null) {out.close();}
|
||||
// 删除临时文件
|
||||
if (file != null) {file.delete();}
|
||||
}
|
||||
}
|
||||
|
||||
private static File createDoc(Map<?, ?> dataMap, Template template) {
|
||||
String name = "sellPlan.doc";
|
||||
File f = new File(name);
|
||||
Template t = template;
|
||||
try {
|
||||
// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
|
||||
Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
|
||||
t.process(dataMap, w);
|
||||
w.close();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public static void exportMillCertificateWord2(HttpServletRequest request, HttpServletResponse response, Map map, String title, String ftlFile, String path) throws IOException {
|
||||
Template freemarkerTemplate = configuration.getTemplate(ftlFile);
|
||||
File file = null;
|
||||
InputStream fin = null;
|
||||
OutputStream out = null;
|
||||
// String systemName = System.getProperty("os.name");
|
||||
String uploadPath = path + "word" + File.separator;
|
||||
try {
|
||||
// 调用工具类的createDoc方法生成Word文档
|
||||
file = createDoc(map, freemarkerTemplate);
|
||||
fin = new FileInputStream(file);
|
||||
|
||||
File file2 = new File(uploadPath);
|
||||
// 生成文件夹
|
||||
if (!file2.exists()) {
|
||||
file2.mkdirs();
|
||||
}
|
||||
String fileName = uploadPath + File.separator + title + ".docx";
|
||||
out = new FileOutputStream(new File(fileName));
|
||||
byte[] buffer = new byte[512]; // 缓冲区
|
||||
int bytesToRead = -1;
|
||||
// 通过循环将读入的Word文件的内容输出到浏览器中
|
||||
while ((bytesToRead = fin.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesToRead);
|
||||
}
|
||||
} finally {
|
||||
if (fin != null) {fin.close();}
|
||||
if (out != null) {out.close();}
|
||||
// 删除临时文件
|
||||
if (file != null) {file.delete();}
|
||||
}
|
||||
}
|
||||
|
||||
public static void exportPDF(HttpServletRequest request, HttpServletResponse response, String titleName, String fileName) throws IOException {
|
||||
File file = null;
|
||||
InputStream fin = null;
|
||||
ServletOutputStream out = null;
|
||||
try {
|
||||
file = new File(fileName);
|
||||
fin = new FileInputStream(file);
|
||||
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("application/pdf");
|
||||
// 设置浏览器以下载的方式处理该文件名
|
||||
response.setHeader("Content-Disposition", "attachment;filename="
|
||||
.concat(String.valueOf(URLEncoder.encode(titleName, "UTF-8"))));
|
||||
out = response.getOutputStream();
|
||||
// 缓冲区
|
||||
byte[] buffer = new byte[512];
|
||||
int bytesToRead = -1;
|
||||
// 通过循环将读入的pdf文件的内容输出到浏览器中
|
||||
while ((bytesToRead = fin.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesToRead);
|
||||
}
|
||||
} finally {
|
||||
if (fin != null) {fin.close();}
|
||||
if (out != null) {out.close();}
|
||||
}
|
||||
}
|
||||
|
||||
public static void exportFile(HttpServletRequest request, HttpServletResponse response,String fileName,byte[] decode) throws IOException {
|
||||
InputStream fin = null;
|
||||
ServletOutputStream out = null;
|
||||
try {
|
||||
fin = new ByteArrayInputStream(decode);
|
||||
response.setCharacterEncoding("utf-8");
|
||||
String subfix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
|
||||
if(Objects.equals(subfix,"pdf")){
|
||||
response.setContentType("application/pdf");
|
||||
}else if(Objects.equals(subfix,"doc") || Objects.equals(subfix,"docx")){
|
||||
response.setContentType("application/msword");
|
||||
}
|
||||
// 设置浏览器以下载的方式处理该文件名
|
||||
response.setHeader("Content-Disposition", "attachment;filename="
|
||||
.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
|
||||
out = response.getOutputStream();
|
||||
// 缓冲区
|
||||
byte[] buffer = new byte[512];
|
||||
int bytesToRead = -1;
|
||||
// 通过循环将读入的pdf文件的内容输出到浏览器中
|
||||
while ((bytesToRead = fin.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesToRead);
|
||||
}
|
||||
} finally {
|
||||
if (fin != null) {fin.close();}
|
||||
if (out != null) {out.close();}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @author cw chen
|
||||
* @description 下载word文件
|
||||
* @Param request
|
||||
* @Param response
|
||||
* @Param titleName
|
||||
* @Param fileName
|
||||
* @date 2023-04-17 11:07
|
||||
*/
|
||||
public static void exportWord(HttpServletRequest request, HttpServletResponse response, String titleName, String fileName) throws IOException {
|
||||
File file = null;
|
||||
InputStream fin = null;
|
||||
ServletOutputStream out = null;
|
||||
try {
|
||||
file = new File(fileName);
|
||||
fin = new FileInputStream(file);
|
||||
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("application/msword");
|
||||
// 设置浏览器以下载的方式处理该文件名
|
||||
response.setHeader("Content-Disposition", "attachment;filename="
|
||||
.concat(String.valueOf(URLEncoder.encode(titleName, "UTF-8"))));
|
||||
out = response.getOutputStream();
|
||||
// 缓冲区
|
||||
byte[] buffer = new byte[512];
|
||||
int bytesToRead = -1;
|
||||
// 通过循环将读入的pdf文件的内容输出到浏览器中
|
||||
while ((bytesToRead = fin.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesToRead);
|
||||
}
|
||||
} finally {
|
||||
if (fin != null) {fin.close();}
|
||||
if (out != null) {out.close();}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @author cw chen
|
||||
* @description 下载word文件
|
||||
* @Param request
|
||||
* @Param response
|
||||
* @Param titleName
|
||||
* @Param fileName
|
||||
* @date 2023-04-17 11:07
|
||||
*/
|
||||
public static void exportVideo(HttpServletRequest request, HttpServletResponse response, String titleName, String fileName) throws IOException {
|
||||
File file = null;
|
||||
InputStream fin = null;
|
||||
ServletOutputStream out = null;
|
||||
try {
|
||||
file = new File(fileName);
|
||||
fin = new FileInputStream(file);
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("video/mp4");
|
||||
// 设置浏览器以下载的方式处理该文件名
|
||||
response.setHeader("Content-Disposition", "attachment;filename="
|
||||
.concat(String.valueOf(URLEncoder.encode(titleName, "UTF-8"))));
|
||||
out = response.getOutputStream();
|
||||
// 缓冲区
|
||||
byte[] buffer = new byte[512];
|
||||
int bytesToRead = -1;
|
||||
// 通过循环将读入的pdf文件的内容输出到浏览器中
|
||||
while ((bytesToRead = fin.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesToRead);
|
||||
}
|
||||
} finally {
|
||||
if (fin != null) {
|
||||
fin.close();
|
||||
}
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue