hutool工具类也有导出word的封装,本来图方便想用它的,但是后来发现导出的文档数字和汉字不是一个字体,也就是一行里面会有不同的字体,hutool只能一段一段的换字体,所以换了poi
先看看效果:这里汉字是宋体,数字和字母是Times New Roman
下面是我写的一个测试导出类:
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;
/**
* @program: fire
* @description:
* @author: fbl
* @create: 2021-07-22 09:34
**/
public class WordTest {
public static void main(String[] args) throws Exception {
String finalText = "(5)《中华人民共和国城乡规划法》(中华人民共和国主席令第74号,自2008年1月1日起施行,2019年4月23日第十三届全国人民代表大会常务委员会第十次会议修改);";
//Blank Document
XWPFDocument document = new XWPFDocument();
//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("E:\\国恒文档管理系统\\导出模板.docx"));
String[] nums = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
List<String> numsStr = Arrays.asList(nums);
XWPFParagraph firstParagraph = document.createParagraph();
for (int j = 0; j < finalText.length(); j++) {
String textChar = finalText.substring(j, j + 1);
if (numsStr.contains(textChar)) {
XWPFRun run = firstParagraph.createRun();
run.setText(textChar);
run.setFontSize(14);
run.setFontFamily("Times New Roman");
} else {
XWPFRun run = firstParagraph.createRun();
run.setText(textChar);
run.setFontSize(14);
run.setFontFamily("宋体");
}
}
document.write(out);
out.close();
System.out.println("create_table document written success.");
}
}
这里实现了数字和汉字不是一个字体,后来发现还有字母也是Times New Roman,这个使用正则判断一下即可,这个输出到指定文件中了,正常是应该返回流,让前台下载的。
controller
@PostMapping("/export-file")
@ApiOperation(value = "法律法规文件列表导出word")
@AnonymousAccess
public void exportFile(@RequestBody @Validated SearchFilter searchFilter, HttpServletResponse response) {
fileService.exportFile(searchFilter,response);
}
service
public void exportFile(SearchFilter searchFilter, HttpServletResponse response) {
JSONObject filters = searchFilter.getFilters();
Integer fileType = null;
List<String> fileLabel = null;
if (Objects.nonNull(filters)) {
// 文件类型
if (Objects.nonNull(filters.getInteger("fileType"))) {
fileType = filters.getInteger("fileType");
}
// 文件标签
if (Objects.nonNull(filters.getInteger("fileLabel"))) {
String fileLabelStr = filters.getString("fileLabel");
fileLabel = Arrays.asList(fileLabelStr.split(","));
}
}
// 查询文件列表
List<FileListVo> files = fileMapper.selectFileList(fileType, fileLabel);
// 置入标签名称
setLabelName(files);
XWPFDocument document = new XWPFDocument();
for (int i = 0; i < files.size(); i++) {
String publishOrder = "";
String reviseTime = "";
String executeTime = "";
String nationalStandard = "";
if (Objects.nonNull(files.get(i).getPublishOrder())) {
publishOrder = files.get(i).getPublishOrder() + ",";
}
if (Objects.nonNull(files.get(i).getReviseTime())) {
reviseTime = files.get(i).getReviseTime() + ",";
}
if (Objects.nonNull(files.get(i).getExecuteTime())) {
executeTime = files.get(i).getExecuteTime() + ",";
}
if (Objects.nonNull(files.get(i).getNationalStandard())) {
nationalStandard = files.get(i).getNationalStandard() + ",";
}
int num = i + 1;
String text = " (" + num + ")" + files.get(i).getFileName() + "(" + nationalStandard + publishOrder + reviseTime + executeTime;
// 去掉最后一个逗号
String finalText = text.substring(0, text.length() - 1);
XWPFParagraph firstParagraph = document.createParagraph();
for (int j = 0; j < finalText.length(); j++) {
String textChar = finalText.substring(j, j + 1);
// 数字
boolean numsMatches = textChar.matches("^[0-9]*$");
// 大写字母
boolean charMatches = textChar.matches("^[A-Z]+$");
if (numsMatches || charMatches) {
XWPFRun run = firstParagraph.createRun();
run.setText(textChar);
run.setFontSize(14);
run.setFontFamily("Times New Roman");
} else {
XWPFRun run = firstParagraph.createRun();
run.setText(textChar);
run.setFontSize(14);
run.setFontFamily("宋体");
}
}
// 最后一行
if (files.size() == i + 1) {
XWPFRun run = firstParagraph.createRun();
run.setText(")。");
run.setFontSize(14);
run.setFontFamily("宋体");
} else {
XWPFRun run = firstParagraph.createRun();
run.setText(");");
run.setFontSize(14);
run.setFontFamily("宋体");
}
}
OutputStream out = null;
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
try {
response.setHeader("Content-Disposition",
"attachment;filename=" + new String("法律法规".getBytes(), "iso8859-1") + ".docx");
out = response.getOutputStream();
document.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
//此处记得关闭输出Servlet流
IoUtil.close(out);
}
}