Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔
前言
通過(guò)Apache POI導(dǎo)出excel,而Apache POI包含是操作Office Open XML(OOXML)標(biāo)準(zhǔn)和微軟的OLE 2復(fù)合文檔格式(OLE2)的Java API。所以也是可以通過(guò)POI來(lái)導(dǎo)出word的。本文主要介紹通過(guò)SpringBoot集成POI工具實(shí)現(xiàn)Word的導(dǎo)出功能。
知識(shí)準(zhǔn)備
需要理解Apache POI遵循的標(biāo)準(zhǔn)(Office Open XML(OOXML)標(biāo)準(zhǔn)和微軟的OLE 2復(fù)合文檔格式(OLE2)), 這將對(duì)應(yīng)著API的依賴(lài)包。
什么是POI
Apache POI 是用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫(xiě)的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫(xiě),意為“簡(jiǎn)潔版的模糊實(shí)現(xiàn)”。
Apache POI 是創(chuàng)建和維護(hù)操作各種符合Office Open XML(OOXML)標(biāo)準(zhǔn)和微軟的OLE 2復(fù)合文檔格式(OLE2)的Java API。更多請(qǐng)參考? 官方文檔。
實(shí)現(xiàn)案例
這里展示SpringBoot集成POI導(dǎo)出用戶信息的Word例子。
Pom依賴(lài)
引入poi的依賴(lài)包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.2</version> </dependency>
導(dǎo)出Word
UserController中導(dǎo)出的方法
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中導(dǎo)出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 全棧知識(shí)體系"); // Chapter 1 createChapterH1(doc, "1. 知識(shí)準(zhǔn)備"); createChapterH2(doc, "1.1 什么是POI"); createParagraph(doc, "Apache POI 是創(chuàng)建和維護(hù)操作各種符合Office Open XML(OOXML)標(biāo)準(zhǔn)和微軟的OLE 2復(fù)合文檔格式(OLE2)的Java API。用它可以使用Java讀取和創(chuàng)建,修改MS Excel文件.而且,還可以使用Java讀取和創(chuàng)建MS Word和MSPowerPoint文件。更多請(qǐng)參考[官方文檔](https://poi.apache.org/index.html)"); createChapterH2(doc, "1.2 POI中基礎(chǔ)概念"); createParagraph(doc, "生成xls和xlsx有什么區(qū)別?POI對(duì)Excel中的對(duì)象的封裝對(duì)應(yīng)關(guān)系?"); // Chapter 2 createChapterH1(doc, "2. 實(shí)現(xiàn)案例"); createChapterH2(doc, "2.1 用戶列表示例"); createParagraph(doc, "以導(dǎo)出用戶列表為例"); // 表格 List<User> 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< userList.size(); i++) { List<XWPFTableCell> tableCells = table.getRow(i).getTableCells(); tableCells.get(0).setText(userList.get(i).getId()+""); tableCells.get(1).setText(userList.get(i).getUserName()); tableCells.get(2).setText(userList.get(i).getEmail()); tableCells.get(3).setText(userList.get(i).getPhoneNumber()+""); tableCells.get(4).setText(userList.get(i).getDescription()); } createChapterH2(doc, "2.2 圖片導(dǎo)出示例"); createParagraph(doc, "以導(dǎo)出圖片為例"); // 圖片 InputStream stream = null; try { XWPFParagraph paragraph2 = doc.createParagraph(); Resource resource = new ClassPathResource("pdai-guli.png"); stream = new FileInputStream(resource.getFile()); XWPFRun run = paragraph2.createRun(); run.addPicture(stream, Document.PICTURE_TYPE_PNG, "Generated", Units.toEMU(256), Units.toEMU(256)); } catch (IOException | InvalidFormatException e) { e.printStackTrace(); } return doc; } private void createTitle(XWPFDocument doc, String content){ XWPFParagraph title = doc.createParagraph(); title.setAlignment(ParagraphAlignment.CENTER); XWPFRun r1 = title.createRun(); r1.setBold(true); r1.setFontFamily("宋體"); r1.setText(content); r1.setFontSize(22); } private void createChapterH1(XWPFDocument doc, String content){ XWPFParagraph actTheme = doc.createParagraph(); actTheme.setAlignment(ParagraphAlignment.LEFT); XWPFRun runText1 = actTheme.createRun(); runText1.setBold(true); runText1.setText(content); runText1.setFontSize(18); } private void createChapterH2(XWPFDocument doc, String content){ XWPFParagraph actType = doc.createParagraph(); XWPFRun runText2 = actType.createRun(); runText2.setBold(true); runText2.setText(content); runText2.setFontSize(15); } private void createParagraph(XWPFDocument doc, String content){ XWPFParagraph actType = doc.createParagraph(); XWPFRun runText2 = actType.createRun(); runText2.setText(content); runText2.setFontSize(11); } private List<User> getUserList(){ List<User> userList = new ArrayList<>(); for (int i = 0; i < 5; i++) { userList.add(User.builder() .id(Long.parseLong(i + "")).userName("pdai" + i).email("pdai@pdai.tech" + i).phoneNumber(121231231231L) .description("hello world" + i) .build()); } return userList; } }
導(dǎo)出:
導(dǎo)出后的word:
到此這篇關(guān)于Java SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔的文章就介紹到這了,更多相關(guān)Java POI導(dǎo)出Word文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
web.xml中servlet, bean, filter, listenr 加載順序_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了web.xml中servlet, bean, filter, listenr 加載順序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08java獲取系統(tǒng)路徑字體、得到某個(gè)目錄下的所有文件名、獲取當(dāng)前路徑
這篇文章主要介紹了java獲取系統(tǒng)路徑字體、得到某個(gè)目錄下的所有文件名、獲取當(dāng)前路徑,需要的朋友可以參考下2014-04-04