|
|
@@ -1,10 +1,14 @@ |
|
|
|
package com.ningdatech.pmapi.common.util; |
|
|
|
|
|
|
|
import cn.hutool.core.io.IoUtil; |
|
|
|
import com.ningdatech.basic.exception.BizException; |
|
|
|
import com.ningdatech.pmapi.meeting.entity.dto.ExpertFeeExportDTO; |
|
|
|
import com.ningdatech.pmapi.meeting.entity.dto.ExpertInfoDTO; |
|
|
|
import freemarker.template.Configuration; |
|
|
|
import freemarker.template.Template; |
|
|
|
import freemarker.template.TemplateException; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.http.entity.ContentType; |
|
|
|
import org.springframework.core.io.ByteArrayResource; |
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
@@ -18,19 +22,20 @@ import java.util.ArrayList; |
|
|
|
|
|
|
|
/** |
|
|
|
* <p> |
|
|
|
* WordUtil |
|
|
|
* FreemarkerWordUtil |
|
|
|
* </p> |
|
|
|
* |
|
|
|
* @author WendyYang |
|
|
|
* @since 2023/7/24 |
|
|
|
**/ |
|
|
|
public class WordUtil { |
|
|
|
@Slf4j |
|
|
|
public class FreemarkerWordUtil { |
|
|
|
|
|
|
|
private static final Configuration CONFIGURATION = new Configuration(Configuration.VERSION_2_3_31); |
|
|
|
|
|
|
|
static { |
|
|
|
CONFIGURATION.setDefaultEncoding("UTF-8"); |
|
|
|
CONFIGURATION.setClassForTemplateLoading(WordUtil.class, "/template"); |
|
|
|
CONFIGURATION.setClassForTemplateLoading(FreemarkerWordUtil.class, "/template"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -43,30 +48,22 @@ public class WordUtil { |
|
|
|
*/ |
|
|
|
public static void create(Object dataMap, String templateName, String filePath, String fileName) { |
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
//获取模板 |
|
|
|
Template template = CONFIGURATION.getTemplate(templateName); |
|
|
|
|
|
|
|
//输出文件 |
|
|
|
File outFile = new File(filePath + File.separator + fileName); |
|
|
|
|
|
|
|
//如果输出目标文件夹不存在,则创建 |
|
|
|
if (!outFile.getParentFile().exists()) { |
|
|
|
boolean ignore = outFile.getParentFile().mkdirs(); |
|
|
|
} |
|
|
|
|
|
|
|
//将模板和数据模型合并生成文件 |
|
|
|
Writer out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(outFile.toPath()), StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
//生成文件 |
|
|
|
template.process(dataMap, out); |
|
|
|
|
|
|
|
//关闭流 |
|
|
|
out.flush(); |
|
|
|
out.close(); |
|
|
|
IoUtil.flush(out); |
|
|
|
IoUtil.close(out); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("根据模版生成Word文件失败:", e); |
|
|
|
throw BizException.wrap("根据模版生成Word文件失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -75,23 +72,19 @@ public class WordUtil { |
|
|
|
StringWriter out = new StringWriter(); |
|
|
|
Writer writer = new BufferedWriter(out, 4096); |
|
|
|
template.process(data, writer); |
|
|
|
InputStream fin = new ByteArrayResource(out.toString().getBytes(StandardCharsets.UTF_8)).getInputStream(); |
|
|
|
ServletOutputStream outputStream; |
|
|
|
response.setCharacterEncoding(StandardCharsets.UTF_8.displayName()); |
|
|
|
response.setContentType("application/json"); |
|
|
|
response.setContentType(ContentType.APPLICATION_JSON.getMimeType()); |
|
|
|
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8")); |
|
|
|
outputStream = response.getOutputStream(); |
|
|
|
// 缓冲区 |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
int bytesToRead; |
|
|
|
// 通过循环将读入的Word文件的内容输出到浏览器中 |
|
|
|
while ((bytesToRead = fin.read(buffer)) != -1) { |
|
|
|
outputStream.write(buffer, 0, bytesToRead); |
|
|
|
} |
|
|
|
if (outputStream != null) { |
|
|
|
outputStream.close(); |
|
|
|
try (InputStream is = new ByteArrayResource(out.toString().getBytes(StandardCharsets.UTF_8)).getInputStream(); |
|
|
|
ServletOutputStream outputStream = response.getOutputStream()) { |
|
|
|
// 缓冲区 |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
int bytesToRead; |
|
|
|
// 通过循环将读入的Word文件的内容输出到浏览器中 |
|
|
|
while ((bytesToRead = is.read(buffer)) != -1) { |
|
|
|
outputStream.write(buffer, 0, bytesToRead); |
|
|
|
} |
|
|
|
} |
|
|
|
fin.close(); |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
@@ -100,17 +93,17 @@ public class WordUtil { |
|
|
|
dto.setMeetingTime("2023-01-01 12:20"); |
|
|
|
dto.setProjectName("阿萨法舒服舒服舒服舒服送达方"); |
|
|
|
dto.setExperts(new ArrayList<>()); |
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
for (int i = 0; i < 10; i++) { |
|
|
|
ExpertInfoDTO expert = new ExpertInfoDTO(); |
|
|
|
expert.setNo(i); |
|
|
|
expert.setBank("中国建设银行"); |
|
|
|
expert.setMobile("17839192616"); |
|
|
|
expert.setBankNo("121212121212121212"); |
|
|
|
expert.setCompany("宁大科技有限公司"); |
|
|
|
expert.setMobile("17819392616"); |
|
|
|
expert.setBankNo("6111111172121324354"); |
|
|
|
expert.setCompany("宁达科技有限公司"); |
|
|
|
expert.setFee(BigDecimal.valueOf(500)); |
|
|
|
expert.setName("张三"); |
|
|
|
expert.setName("张三丰"); |
|
|
|
expert.setSex("男"); |
|
|
|
expert.setIdcard("121212121121"); |
|
|
|
expert.setIdcard("1212121211212121"); |
|
|
|
dto.getExperts().add(expert); |
|
|
|
} |
|
|
|
create(dto, "专家费.ftl", "/Users/wendy/Desktop/", "专家费.doc"); |