接口:
public ResponseEntity<byte[]> exportWord(Bo bo, HttpServletResponse response) throws Exception {
try {
// 根据对象导出数据 调用导出接口
byte[] bytes = exportWord(bo);
HttpHeaders headers = new HttpHeaders();
// 设置 HTTP 响应头,告诉浏览器这是一个文件下载
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDisposition(ContentDisposition.builder("attachment")
.filename("document.docx")
.build());
headers.setContentLength(bytes.length);
// 返回字节数组作为 HTTP 响应体
return ResponseEntity.ok().headers(headers).body(bytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
导出的方法:
/**
* 导出方法
* @param bo
*/
private byte[] exportWord(Bo bo) {
// 创建一个新的 Word 文档
XWPFDocument document = new XWPFDocument();
// 整理自己要导出的数据
// 添加一个大标题 调用大标题方法
addBigTitle(document, "大标题");
// 添加一个二级标题 调用二级标题方法
addSectionTitle(document,"二级标题");
// 添加一个三级标题 调用三级标题方法
addSubHeading(document,"三级标题","你的内容","是否为图片");
// 浏览器默认下载地址
// 使用 ByteArrayOutputStream 将文件内容写入内存
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
document.write(byteArrayOutputStream);
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
大标题方法:
/**
* 添加大标题
*/
private static void addBigTitle(XWPFDocument document, String title) {
// 创建大标题段落
XWPFParagraph titleParagraph = document.createParagraph();
titleParagraph.setAlignment(ParagraphAlignment.CENTER); // 设置大标题居中对齐
// 设置大标题的样式
XWPFRun titleRun = titleParagraph.createRun();
titleRun.setText(title); // 设置大标题文本
titleRun.setBold(true); // 设置大标题加粗
titleRun.setFontFamily("宋体"); // 设置字体为 宋体
titleRun.setFontSize(18); // 设置字体大小为 小二(18pt)
}
二级标题并添加内容方法:
/**
* 添加二级标题
* @param document 要添加二级标题的 Word 文档
* @param title 二级标题文本
*/
private static void addSectionTitle(XWPFDocument document, String title) {
// 创建小标题段落
XWPFParagraph titleParagraph = document.createParagraph();
titleParagraph.setAlignment(ParagraphAlignment.LEFT); // 设置小标题为左对齐
// 设置小标题的样式
XWPFRun titleRun = titleParagraph.createRun();
titleRun.setText(title); // 设置小标题文本
titleRun.setBold(true); // 小标题加粗
titleRun.setFontFamily("宋体"); // 设置字体为 宋体
titleRun.setFontSize(18); // 设置小标题的字体大小为 14
}
三级标题并添加内容方法:
/**
* 三级标题
* @param document 要添加三级的 Word 文档
* @param title 三级文本
*/
private void addSubHeading(XWPFDocument document, String title,String content,String isPicsPath) {
// 创建子标题段落
XWPFParagraph subHeadingParagraph = document.createParagraph();
subHeadingParagraph.setAlignment(ParagraphAlignment.LEFT); // 设置子标题为左对齐
// 设置子标题的样式
XWPFRun subHeadingRun = subHeadingParagraph.createRun();
subHeadingRun.setText(title); // 设置子标题文本
subHeadingRun.setBold(true); // 子标题加粗
subHeadingRun.setFontFamily("宋体"); // 设置字体为 宋体
subHeadingRun.setFontSize(14); // 设置字体大小为 四号(14pt)
if (StringUtils.isNotBlank(isPicsPath)) {
// 图片路径
String imagePath = "图片的的路径"+"/"+content;
try {
// 插入图片,自动识别图片格式 调用插入图片方法
insertImageToWord(document, content,imagePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
// 设置同一行标题内容格式
XWPFRun subHeadingRun2 = subHeadingParagraph.createRun();
// 设置字体为宋体
subHeadingRun2.setFontFamily("宋体");
// 设置字体大小为四号字(12磅)
subHeadingRun2.setFontSize(12);
subHeadingRun2.setText(content); // 设置段落文本内容
}
}
将图片插入到word文档中:
/**
* 插入图片到Word文档
* @param document
* @param imagePath
* @throws IOException
*/
// 插入图片的函数,自动识别图片类型并插入
public static void insertImageToWord(XWPFDocument document,String content ,String imagePath) throws IOException {
// 获取图片的拓展名
String extension = content.substring(content.lastIndexOf('.') + 1);
// 图片流 调用接口获取图片的流
byte[] imageBytes = getImageBytes(imagePath); // 获取图片的字节数组
try{
// 根据图片类型插入
switch (extension.toLowerCase()) {
case "jpeg":
case "jpg":
document.addPictureData(imageBytes, XWPFDocument.PICTURE_TYPE_JPEG);
break;
case "png":
document.addPictureData(imageBytes, XWPFDocument.PICTURE_TYPE_PNG);
break;
case "gif":
document.addPictureData(imageBytes, XWPFDocument.PICTURE_TYPE_GIF);
break;
case "bmp":
document.addPictureData(imageBytes, XWPFDocument.PICTURE_TYPE_BMP);
break;
default:
System.out.println("content: " + extension);
return;
}
} catch (InvalidFormatException e) {
throw new RuntimeException(e);
}
// 创建段落,插入图片
XWPFParagraph imageParagraph = document.createParagraph();
XWPFRun imageRun = imageParagraph.createRun();
// 插入图片并控制大小
try {
imageRun.addPicture(new FileInputStream(imagePath),
// 控制图片大小
XWPFDocument.PICTURE_TYPE_PNG, imagePath, Units.toEMU(200), Units.toEMU(200));
} catch (InvalidFormatException e) {
throw new RuntimeException(e);
}
}
获取图片的流:
/**
* 获取图片的字节数组
*/
// 获取图片的字节数组
public static byte[] getImageBytes(String imagePath) throws IOException {
try (FileInputStream inputStream = new FileInputStream(new File(imagePath));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, length);
}
return byteArrayOutputStream.toByteArray();
}
}