如何使用Java SpringBoot集成POI实现Word文档导出

您所在的位置:网站首页 easypoi读取word 如何使用Java SpringBoot集成POI实现Word文档导出

如何使用Java SpringBoot集成POI实现Word文档导出

2023-04-24 05:31| 来源: 网络整理| 查看: 265

如何使用Java SpringBoot集成POI实现Word文档导出 发布时间:2023-04-21 13:41:49 来源:亿速云 阅读:78 作者:iii 栏目:编程语言

这篇文章主要介绍“如何使用Java SpringBoot集成POI实现Word文档导出”,在日常操作中,相信很多人在如何使用Java SpringBoot集成POI实现Word文档导出问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用Java SpringBoot集成POI实现Word文档导出”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

知识准备

需要理解Apache POI遵循的标准(Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)), 这将对应着API的依赖包。

什么是POI

Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊实现”。

Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。

如何使用Java SpringBoot集成POI实现Word文档导出

实现案例

这里展示SpringBoot集成POI导出用户信息的Word例子。

Pom依赖

引入poi的依赖包

org.apache.poi poi 5.2.2 org.apache.poi poi-ooxml 5.2.2 导出Word

UserController中导出的方法

package tech.pdai.springboot.file.word.poi.controller; import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import io.swagger.annotations.ApiOperation; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import tech.pdai.springboot.file.word.poi.service.IUserService; /** * @author pdai */ @RestController @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; @ApiOperation("Download Word") @GetMapping("/word/download") public void download(HttpServletResponse response){ try { XWPFDocument document = userService.generateWordXWPFDocument(); response.reset(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=user_world_" + System.currentTimeMillis() + ".docx"); OutputStream os = response.getOutputStream(); document.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } } }

UserServiceImple中导出Word方法

package tech.pdai.springboot.file.word.poi.service.impl; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import lombok.extern.slf4j.Slf4j; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.util.Units; import org.apache.poi.xwpf.usermodel.BreakType; import org.apache.poi.xwpf.usermodel.Document; 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.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Service; import tech.pdai.springboot.file.word.poi.entity.User; import tech.pdai.springboot.file.word.poi.service.IUserService; /** * @author pdai */ @Slf4j @Service public class UserServiceImpl implements IUserService { @Override public XWPFDocument generateWordXWPFDocument(){ XWPFDocument doc = new XWPFDocument(); // Title createTitle(doc, "Java 全栈知识体系"); // Chapter 1 createChapterH1(doc, "1. 知识准备"); createChapterH2(doc, "1.1 什么是POI"); createParagraph(doc, "Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。更多请参考[官方文档](https://poi.apache.org/index.html)"); createChapterH2(doc, "1.2 POI中基础概念"); createParagraph(doc, "生成xls和xlsx有什么区别?POI对Excel中的对象的封装对应关系?"); // Chapter 2 createChapterH1(doc, "2. 实现案例"); createChapterH2(doc, "2.1 用户列表示例"); createParagraph(doc, "以导出用户列表为例"); // 表格 List userList = getUserList(); XWPFParagraph paragraph = doc.createParagraph(); XWPFTable table = paragraph.getDocument().createTable(userList.size(), 5); table.setWidth(500); table.setCellMargins(20, 20, 20, 20); //表格属性 CTTblPr tablePr = table.getCTTbl().addNewTblPr(); //表格宽度 CTTblWidth width = tablePr.addNewTblW(); width.setW(BigInteger.valueOf(8000)); for(int i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3