2025-08-13 11:43:02 +08:00

158 lines
5.9 KiB
Java

package com.zhgd.xmgl.util;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.parse.ParseWord07;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
public class WordUtils {
/**
* word下载
*
* @param fileName 下载时的文件名称
* @param response
* @param doc
*/
private static void downLoadWord(String fileName, HttpServletResponse response, XWPFDocument doc) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/msword");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".docx", "UTF-8"));
doc.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* word下载
*
* @param fileName 下载时的文件名称
* @param response
*/
private static void downLoadWord(String fileName, HttpServletResponse response, POIFSFileSystem poifsFileSystem) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".docx", "UTF-8"));
poifsFileSystem.writeFilesystem(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* html转word
*
* @param response
* @param html
* @throws IOException
*/
public static void html2Word(HttpServletResponse response, String html) throws IOException {
// String html = FileUtil.readString("C:\\Users\\Administrator\\Desktop\\t\\文档.html", StandardCharsets.UTF_8 );
byte by[] = html.getBytes("UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(by);
POIFSFileSystem poifsFileSystem = new POIFSFileSystem();
DirectoryEntry directoryEntry = poifsFileSystem.getRoot();
directoryEntry.createDocument("WordDocument", byteArrayInputStream);
WordUtils.downLoadWord("导出word", response, poifsFileSystem);
}
/**
* word模板导出
*
* @param map
* @param templatePath
* @param fileName
* @param response
* @throws Exception
*/
public static void WordTemplateExport(Map<String, Object> map, String templatePath, String fileName, HttpServletResponse response) throws Exception {
XWPFDocument doc = WordExportUtil.exportWord07(templatePath, map);
downLoadWord(fileName, response, doc);
}
/**
* word模板导出多页
*
* @param list
* @param templatePath
* @param fileName
* @param response
* @throws Exception
*/
public static void WordTemplateExportMorePage(List<Map<String, Object>> list, String templatePath, String fileName, HttpServletResponse response) throws Exception {
XWPFDocument doc = new ParseWord07().parseWord(templatePath, list);
downLoadWord(fileName, response, doc);
}
/**
* 文本换行
*/
public static void addBreakInCell(List<XWPFParagraph> paragraphs) {
for (XWPFParagraph p : paragraphs) {
for (XWPFRun run : p.getRuns()) {//XWPFRun对象定义具有一组公共属性的文本区域
if (run.getText(0) != null && run.getText(0).contains("\n")) {
String[] lines = run.getText(0).split("\n");
if (lines.length > 0) {
run.setText(lines[0], 0); // set first line into XWPFRun
for (int i = 1; i < lines.length; i++) {
// add break and insert new text
run.addBreak();//中断
// run.addCarriageReturn();//回车符,但是不起作用
run.setText(lines[i]);
}
}
}
}
}
}
/**
* 文本换行
*/
public static void addBreakInCellForExam(List<XWPFParagraph> paragraphs) {
for (XWPFParagraph p : paragraphs) {
List<XWPFRun> runs = p.getRuns();
if (runs.size() > 0) {
XWPFRun run = runs.get(0);
String text = run.getText(0); // 获取完整文本
if (text != null && text.contains("\n")) {
String[] lines = text.split("\n");
if (lines.length > 0) {
// 设置第一行文本
run.setText(lines[0], 0);
run.addBreak(); // 添加换行符
// 为剩余行创建新的 XWPFRun
for (int j = 1; j < lines.length; j++) {
XWPFRun newRun = p.insertNewRun(j); // 插入新的 XWPFRun
newRun.setText(lines[j]); // 设置新行的文本
// 复制原始 XWPFRun 的样式到新的 XWPFRun
newRun.setBold(run.isBold());
newRun.setItalic(run.isItalic());
newRun.setFontSize(10);
newRun.setFontFamily("宋体");
if (j != lines.length - 1) {
newRun.addBreak();
}
}
}
}
}
}
}
}