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 導入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");
}
}
相關工具類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;
}
/**
* 設置表格寬度和水平對齊方式
* @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);
}
// 設置寬度
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();
}
}
運行,就可以看到效果了!!
到此這篇關于Java使用poi生成word文檔的簡單實例的文章就介紹到這了,更多相關Java poi生成word文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java+Eclipse+Selenium環(huán)境搭建的方法步驟
這篇文章主要介紹了Java+Eclipse+Selenium環(huán)境搭建的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
SpringBoot應用中出現(xiàn)的Full GC問題的場景與解決
這篇文章主要為大家詳細介紹了SpringBoot應用中出現(xiàn)的Full GC問題的場景與解決方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2025-04-04
java發(fā)送post請求使用multipart/form-data格式文件數(shù)據(jù)到接口代碼示例
這篇文章主要介紹了java發(fā)送post請求使用multipart/form-data格式文件數(shù)據(jù)到接口的相關資料,文中指定了數(shù)據(jù)編碼格式為UTF-8,并強調了所需依賴工具類,需要的朋友可以參考下2024-12-12
Double.parseDouble()與Double.valueOf()的區(qū)別及說明
這篇文章主要介紹了Double.parseDouble()與Double.valueOf()的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

