Java使用POI生成Word文檔簡(jiǎn)單代碼示例
在開(kāi)發(fā)中有時(shí)候我們需要導(dǎo)出MS word文檔。最近因?yàn)樾枰鲆粋€(gè)生成word文件的功能。就將這塊拿出來(lái)和大家分享。
生成word文件和我們寫(xiě)word文檔是相同的概念,只不過(guò)在這里我們換成了用代碼來(lái)操作。下面的例子中主要有添加頁(yè)眉,頁(yè)腳,正文(段落,表格)。在正文中,段落包含文字字體和背景的設(shè)置。表格主要是數(shù)據(jù)的填充和樣式(有無(wú)邊框)。這里寫(xiě)的例子給出的內(nèi)容只是Java POI 方式生成word文件的極少數(shù)的一些方法,需要使用更多方法的還是要自己根據(jù)自己的需求去查看API。
看到很多小伙伴反應(yīng)不能用的問(wèn)題,這里我又重新把代碼下載下來(lái)生成了一次試試。確實(shí)是沒(méi)有問(wèn)題。以前使用的是jdk6,最后一個(gè)版本使用的是jdk8.我再把我的maven導(dǎo)包情況貼出來(lái)。供大家參考,生成的文件MS office 和wps打開(kāi)均沒(méi)有問(wèn)題。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.core -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
<version>1.0.6</version>
</dependency>那就直接先上代碼吧:
package com.seawater.controller;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
/**
* Created by zhouhs on 2017/1/9.
*/
public class WordExportController {
public static void main(String[] args)throws Exception {
//Blank Document
XWPFDocument document= new XWPFDocument();
//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("create_table.docx"));
//添加標(biāo)題
XWPFParagraph titleParagraph = document.createParagraph();
//設(shè)置段落居中
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleParagraphRun = titleParagraph.createRun();
titleParagraphRun.setText("Java PoI");
titleParagraphRun.setColor("000000");
titleParagraphRun.setFontSize(20);
//段落
XWPFParagraph firstParagraph = document.createParagraph();
XWPFRun run = firstParagraph.createRun();
run.setText("Java POI 生成word文件。");
run.setColor("696969");
run.setFontSize(16);
//設(shè)置段落背景顏色
CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
cTShd.setVal(STShd.CLEAR);
cTShd.setFill("97FFFF");
//換行
XWPFParagraph paragraph1 = document.createParagraph();
XWPFRun paragraphRun1 = paragraph1.createRun();
paragraphRun1.setText("\r");
//基本信息表格
XWPFTable infoTable = document.createTable();
//去表格邊框
infoTable.getCTTbl().getTblPr().unsetTblBorders();
//列寬自動(dòng)分割
CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
infoTableWidth.setType(STTblWidth.DXA);
infoTableWidth.setW(BigInteger.valueOf(9072));
//表格第一行
XWPFTableRow infoTableRowOne = infoTable.getRow(0);
infoTableRowOne.getCell(0).setText("職位");
infoTableRowOne.addNewTableCell().setText(": Java 開(kāi)發(fā)工程師");
//表格第二行
XWPFTableRow infoTableRowTwo = infoTable.createRow();
infoTableRowTwo.getCell(0).setText("姓名");
infoTableRowTwo.getCell(1).setText(": seawater");
//表格第三行
XWPFTableRow infoTableRowThree = infoTable.createRow();
infoTableRowThree.getCell(0).setText("生日");
infoTableRowThree.getCell(1).setText(": xxx-xx-xx");
//表格第四行
XWPFTableRow infoTableRowFour = infoTable.createRow();
infoTableRowFour.getCell(0).setText("性別");
infoTableRowFour.getCell(1).setText(": 男");
//表格第五行
XWPFTableRow infoTableRowFive = infoTable.createRow();
infoTableRowFive.getCell(0).setText("現(xiàn)居地");
infoTableRowFive.getCell(1).setText(": xx");
//兩個(gè)表格之間加個(gè)換行
XWPFParagraph paragraph = document.createParagraph();
XWPFRun paragraphRun = paragraph.createRun();
paragraphRun.setText("\r");
//工作經(jīng)歷表格
XWPFTable ComTable = document.createTable();
//列寬自動(dòng)分割
CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
comTableWidth.setType(STTblWidth.DXA);
comTableWidth.setW(BigInteger.valueOf(9072));
//表格第一行
XWPFTableRow comTableRowOne = ComTable.getRow(0);
comTableRowOne.getCell(0).setText("開(kāi)始時(shí)間");
comTableRowOne.addNewTableCell().setText("結(jié)束時(shí)間");
comTableRowOne.addNewTableCell().setText("公司名稱(chēng)");
comTableRowOne.addNewTableCell().setText("title");
//表格第二行
XWPFTableRow comTableRowTwo = ComTable.createRow();
comTableRowTwo.getCell(0).setText("2016-09-06");
comTableRowTwo.getCell(1).setText("至今");
comTableRowTwo.getCell(2).setText("seawater");
comTableRowTwo.getCell(3).setText("Java開(kāi)發(fā)工程師");
//表格第三行
XWPFTableRow comTableRowThree = ComTable.createRow();
comTableRowThree.getCell(0).setText("2016-09-06");
comTableRowThree.getCell(1).setText("至今");
comTableRowThree.getCell(2).setText("seawater");
comTableRowThree.getCell(3).setText("Java開(kāi)發(fā)工程師");
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
//添加頁(yè)眉
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctHeader = ctrHeader.addNewT();
String headerText = "Java POI create MS word file.";
ctHeader.setStringValue(headerText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
//設(shè)置為右對(duì)齊
headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
//添加頁(yè)腳
CTP ctpFooter = CTP.Factory.newInstance();
CTR ctrFooter = ctpFooter.addNewR();
CTText ctFooter = ctrFooter.addNewT();
String footerText = "http://blog.csdn.net/zhouseawater";
ctFooter.setStringValue(footerText);
XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
headerParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFParagraph[] parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerParagraph;
policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
document.write(out);
out.close();
System.out.println("create_table document written success.");
}
}代碼我放到這一個(gè)文件當(dāng)中了。下面我就一些代碼做一些解釋?zhuān)驗(yàn)橛械氖俏以谧龅倪^(guò)程中遇到的問(wèn)題。大部分的代碼大家都是一眼就可以看懂的。
//設(shè)置段落背景顏色
CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
cTShd.setVal(STShd.CLEAR);
cTShd.setFill("97FFFF");這段代碼設(shè)置段落的背景顏色。
如果我們的表格不需要邊框呢就加下面的代碼:
infoTable.getCTTbl().getTblPr().unsetTblBorders();
infoTable換成自己的table名稱(chēng)就可以了。
建立一個(gè)表格的時(shí)候設(shè)置列寬跟隨內(nèi)容伸縮
CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
infoTableWidth.setType(STTblWidth.DXA);
infoTableWidth.setW(BigInteger.valueOf(9072));其他的代碼我就不解釋了。運(yùn)行就可以得到我們的word文件了。
結(jié)果:

總結(jié)
到此這篇關(guān)于Java使用POI生成Word文檔的文章就介紹到這了,更多相關(guān)Java POI生成Word文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Java架構(gòu)設(shè)計(jì)之六步拆解 DDD
DDD(Domain-Driven Design 領(lǐng)域驅(qū)動(dòng)設(shè)計(jì))是由Eric Evans最先提出,目的是對(duì)軟件所涉及到的領(lǐng)域進(jìn)行建模,以應(yīng)對(duì)系統(tǒng)規(guī)模過(guò)大時(shí)引起的軟件復(fù)雜性的問(wèn)題2022-02-02
springboot整合RabbitMQ發(fā)送短信的實(shí)現(xiàn)
本文會(huì)和SpringBoot做整合,實(shí)現(xiàn)RabbitMQ發(fā)送短信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Nacos下線服務(wù)時(shí),下線報(bào)錯(cuò)選舉Leader失敗問(wèn)題以及解決
這篇文章主要介紹了Nacos下線服務(wù)時(shí),下線報(bào)錯(cuò)選舉Leader失敗問(wèn)題以及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot后端接收前端傳數(shù)組參數(shù)三種方法
這篇文章主要給大家介紹了關(guān)于springboot后端接收前端傳數(shù)組參數(shù)三種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07

