Переглянути джерело

修改word工具类

master
WendyYang 1 рік тому
джерело
коміт
22ca1ecacf
2 змінених файлів з 33 додано та 40 видалено
  1. +28
    -35
      pmapi/src/main/java/com/ningdatech/pmapi/common/util/FreemarkerWordUtil.java
  2. +5
    -5
      pmapi/src/main/java/com/ningdatech/pmapi/meeting/manage/ExpertExportManage.java

pmapi/src/main/java/com/ningdatech/pmapi/common/util/WordUtil.java → pmapi/src/main/java/com/ningdatech/pmapi/common/util/FreemarkerWordUtil.java Переглянути файл

@@ -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");

+ 5
- 5
pmapi/src/main/java/com/ningdatech/pmapi/meeting/manage/ExpertExportManage.java Переглянути файл

@@ -5,7 +5,7 @@ import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.util.WordUtil;
import com.ningdatech.pmapi.common.util.FreemarkerWordUtil;
import com.ningdatech.pmapi.expert.entity.ExpertUserFullInfo;
import com.ningdatech.pmapi.expert.service.IExpertUserFullInfoService;
import com.ningdatech.pmapi.meeting.entity.domain.Meeting;
@@ -110,7 +110,7 @@ public class ExpertExportManage {
}
data.getExperts().add(expert);
});
WordUtil.export(fileName, data, response, template);
FreemarkerWordUtil.export(fileName, data, response, template);
} catch (TemplateException | IOException e) {
log.error("专家费用单导出异常:{}", meetingId, e);
throw BizException.wrap("专家费用单导出失败");
@@ -137,7 +137,7 @@ public class ExpertExportManage {
inners.forEach(w -> projectNames.add(w.getProjectName()));
}
data.setProjectName(StrUtil.join("、", projectNames));
WordUtil.export(fileName, data, response, template);
FreemarkerWordUtil.export(fileName, data, response, template);
} catch (TemplateException | IOException e) {
log.error("专家评审单导出异常:{}", meetingId, e);
throw BizException.wrap("专家评审单导出失败");
@@ -180,7 +180,7 @@ public class ExpertExportManage {
data.getExperts().add(expert);
});
data.setExpertCount(experts.size());
WordUtil.export(fileName, data, response, template);
FreemarkerWordUtil.export(fileName, data, response, template);
} catch (TemplateException | IOException e) {
log.error("劳务费发放审批单导出异常:{}", meetingId, e);
throw BizException.wrap("劳务费发放审批单导出失败");
@@ -229,7 +229,7 @@ public class ExpertExportManage {
data.getExperts().add(expert);
});
String fileName = UUID.randomUUID().toString(true) + ".doc";
WordUtil.export(fileName, data, response, template);
FreemarkerWordUtil.export(fileName, data, response, template);
} catch (Exception e) {
log.error("专家抽取表导出异常:{}", meetingId, e);
throw BizException.wrap("专家抽取表导出失败");


Завантаження…
Відмінити
Зберегти