Java使用poi生成word文檔的簡單實例
Java使用poi生成word文檔的簡單實例
生成的效果如下:
用到的poi的簡單的知識
新建一個word對象
//新建文件 XWPFDocument document = new XWPFDocument();
新建段落以及文字樣式
//創(chuàng)建段落 XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.CENTER); //創(chuàng)建標題 XWPFRun titleFun = paragraph.createRun(); titleFun.setText("個人信息表"); titleFun.setBold(true); titleFun.setFontSize(25); titleFun.setColor("000000"); titleFun.setFontFamily("宋體"); titleFun.addBreak();
titleFun.addBreak(); 換行
實例-生成一個簡單word文檔
新建一個maven項目
pom.xml 導(dǎo)入5.2.5的poi的依賴包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.5</version> </dependency>
新建SimpleWord.java文件
文件內(nèi)容如下:
package com.wumeng.wordexport; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @author wumeng 2024/6/27 17:56 */ public class SimpleWord { /** * 新建word * @throws IOException */ static void newWord(String outputPath) throws IOException { //新建文件 XWPFDocument document = new XWPFDocument(); //創(chuàng)建段落 XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.CENTER); //創(chuàng)建標題 XWPFRun titleFun = paragraph.createRun(); titleFun.setText("個人信息表"); titleFun.setBold(true); titleFun.setFontSize(25); titleFun.setColor("000000"); titleFun.setFontFamily("宋體"); titleFun.addBreak(); //添加引言 XWPFParagraph paragraph2 = document.createParagraph(); paragraph2.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleFun2 = paragraph2.createRun(); titleFun2.setText("引言"); titleFun2.setBold(true); titleFun2.setFontSize(20); titleFun2.setColor("000000"); titleFun2.setFontFamily("宋體"); titleFun2.addBreak(); //添加第一段內(nèi)容 XWPFParagraph paragraph3 = document.createParagraph(); paragraph3.setAlignment(ParagraphAlignment.LEFT); XWPFRun titleFun3 = paragraph3.createRun(); titleFun3.setText("個人信息表"); titleFun3.setBold(true); titleFun3.setFontSize(14); titleFun3.setColor("000000"); titleFun3.setFontFamily("宋體"); XWPFRun titleFun4 = paragraph3.createRun(); titleFun4.setText(",(注:以下內(nèi)容請根據(jù)個人情況如實填寫)"); titleFun4.setFontSize(14); titleFun4.setColor("000000"); titleFun4.setFontFamily("宋體"); titleFun4.addBreak(); //創(chuàng)建表格 XWPFTable table = document.createTable(1,3); SimpleWordUtil.setTableWidthAndHAlign(table, "9072",STJcTable.Enum.forString("center")); List<String> headers = new ArrayList<String>(); headers.add("姓名"); headers.add("性別"); headers.add("年齡"); for (int i = 0; i < headers.size(); i++) { XWPFTableCell cell = table.getRow(0).getCell(i); XWPFParagraph cellParagraph = cell.getParagraphArray(0); cellParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun cellParagraphRun = cellParagraph.createRun(); cellParagraphRun.setFontSize(12); cellParagraphRun.setFontFamily("宋體"); cellParagraphRun.setText(headers.get(i)); cellParagraphRun.setBold(true); } List<List<String>> lists = new ArrayList<List<String>>(); List<String> list1 = new ArrayList<String>(); list1.add("黃xx"); list1.add("男"); list1.add("18"); lists.add(list1); List<String> list2 = new ArrayList<String>(); list2.add("王xx"); list2.add("女"); list2.add("16"); lists.add(list2); for (int i = 0; i < lists.size(); i++) { table.createRow(); for (int j = 0; j < lists.get(i).size(); j++) { XWPFTableCell cell = table.getRow(i+1).getCell(j); XWPFParagraph cellParagraph = cell.getParagraphArray(0); cellParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun cellParagraphRun = cellParagraph.createRun(); cellParagraphRun.setFontSize(12); cellParagraphRun.setFontFamily("宋體"); cellParagraphRun.setText(lists.get(i).get(j)); } } //保存文檔 SimpleWordUtil.saveDocument(document, outputPath); } public static void main(String[] args) throws Exception { newWord("./14.docx"); } }
相關(guān)工具類SimpleWordUtil.java
package com.wumeng.wordexport; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigInteger; /** * @author wumeng 2024/6/27 18:03 */ public class SimpleWordUtil { /** * 獲取表格屬性 * @param table * @return */ public static CTTblPr getTableCTTblPr(XWPFTable table) { CTTbl ttbl = table.getCTTbl(); // 表格屬性 CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr(); return tblPr; } /** * 設(shè)置表格寬度和水平對齊方式 * @param table * @param width * @param enumValue */ public static void setTableWidthAndHAlign(XWPFTable table, String width, STJcTable.Enum enumValue) { CTTblPr tblPr = getTableCTTblPr(table); // 表格寬度 CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); if (enumValue != null) { CTJcTable ctJcTable = tblPr.addNewJc(); ctJcTable.setVal(enumValue); } // 設(shè)置寬度 tblWidth.setW(new BigInteger(width)); tblWidth.setType(STTblWidth.DXA); } /** * 保存文檔 * @param document * @param savePath * @throws IOException */ public static void saveDocument(XWPFDocument document, String savePath) throws IOException { OutputStream os = new FileOutputStream(savePath); document.write(os); os.close(); } }
運行,就可以看到效果了!!
到此這篇關(guān)于Java使用poi生成word文檔的簡單實例的文章就介紹到這了,更多相關(guān)Java poi生成word文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中@PathVariable、@RequestParam和@RequestBody的區(qū)別和使用詳解
這篇文章主要介紹了SpringBoot中@PathVariable、@RequestParam和@RequestBody的區(qū)別和使用詳解,@PathVariable 映射 URL 綁定的占位符,通過@RequestMapping注解中的{}占位符來標識URL中的變量部分,需要的朋友可以參考下2024-01-01Java concurrency之CountDownLatch原理和示例_動力節(jié)點Java學(xué)院整理
CountDownLatch是一個同步輔助類,在完成一組正在其他線程中執(zhí)行的操作之前,它允許一個或多個線程一直等待。 下面通過本文給大家分享Java concurrency之CountDownLatch原理和示例,需要的的朋友參考下吧2017-06-06Sentinel?Gateway自定義限流返回結(jié)果方式
這篇文章主要介紹了Sentinel?Gateway自定義限流返回結(jié)果方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04Java?MapStruct優(yōu)雅地實現(xiàn)對象轉(zhuǎn)換
MapSturct?是一個生成類型安全,高性能且無依賴的?JavaBean?映射代碼的注解處理器,用它可以輕松實現(xiàn)對象轉(zhuǎn)換,下面就來和大家聊聊具體操作吧2023-06-06mybatis多個區(qū)間處理方式(雙foreach循環(huán))
這篇文章主要介紹了mybatis多個區(qū)間處理方式(雙foreach循環(huán)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02idea項目啟動報錯,日志包沖突slf4j和logback沖突問題
遇到SLF4J沖突時,可以嘗試移除沖突的綁定或調(diào)整項目依賴,具體方法包括刪除多余的Logger綁定庫,如Logback或Log4j,或在項目配置文件中明確指定使用的日志框架,若使用WebLogic服務(wù)器,需在weblogic.xml中進行特定配置,適當調(diào)整pom.xml文件中的依賴版本也可能解決問題2024-09-09SpringBoot中@Value獲取值和@ConfigurationProperties獲取值用法及比較
在Spring Boot中,@Value注解是一個非常有用的特性,它允許我們將外部的配置注入到我們的Bean中,@ConfigurationProperties用于將配置文件中的屬性綁定到 Java Bean 上,本文介紹了@Value獲取值和@ConfigurationProperties獲取值用法及比較,需要的朋友可以參考下2024-08-08