Java使用POI生成Word文檔簡單代碼示例
在開發(fā)中有時候我們需要導(dǎo)出MS word文檔。最近因為需要做一個生成word文件的功能。就將這塊拿出來和大家分享。
生成word文件和我們寫word文檔是相同的概念,只不過在這里我們換成了用代碼來操作。下面的例子中主要有添加頁眉,頁腳,正文(段落,表格)。在正文中,段落包含文字字體和背景的設(shè)置。表格主要是數(shù)據(jù)的填充和樣式(有無邊框)。這里寫的例子給出的內(nèi)容只是Java POI 方式生成word文件的極少數(shù)的一些方法,需要使用更多方法的還是要自己根據(jù)自己的需求去查看API。
看到很多小伙伴反應(yīng)不能用的問題,這里我又重新把代碼下載下來生成了一次試試。確實是沒有問題。以前使用的是jdk6,最后一個版本使用的是jdk8.我再把我的maven導(dǎo)包情況貼出來。供大家參考,生成的文件MS office 和wps打開均沒有問題。
<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"));
//添加標題
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();
//列寬自動分割
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 開發(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");
//兩個表格之間加個換行
XWPFParagraph paragraph = document.createParagraph();
XWPFRun paragraphRun = paragraph.createRun();
paragraphRun.setText("\r");
//工作經(jīng)歷表格
XWPFTable ComTable = document.createTable();
//列寬自動分割
CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
comTableWidth.setType(STTblWidth.DXA);
comTableWidth.setW(BigInteger.valueOf(9072));
//表格第一行
XWPFTableRow comTableRowOne = ComTable.getRow(0);
comTableRowOne.getCell(0).setText("開始時間");
comTableRowOne.addNewTableCell().setText("結(jié)束時間");
comTableRowOne.addNewTableCell().setText("公司名稱");
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開發(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開發(fā)工程師");
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
//添加頁眉
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è)置為右對齊
headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
//添加頁腳
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.");
}
}代碼我放到這一個文件當(dāng)中了。下面我就一些代碼做一些解釋,因為有的是我在做的過程中遇到的問題。大部分的代碼大家都是一眼就可以看懂的。
//設(shè)置段落背景顏色
CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
cTShd.setVal(STShd.CLEAR);
cTShd.setFill("97FFFF");這段代碼設(shè)置段落的背景顏色。
如果我們的表格不需要邊框呢就加下面的代碼:
infoTable.getCTTbl().getTblPr().unsetTblBorders();
infoTable換成自己的table名稱就可以了。
建立一個表格的時候設(shè)置列寬跟隨內(nèi)容伸縮
CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
infoTableWidth.setType(STTblWidth.DXA);
infoTableWidth.setW(BigInteger.valueOf(9072));其他的代碼我就不解釋了。運行就可以得到我們的word文件了。
結(jié)果:

總結(jié)
到此這篇關(guān)于Java使用POI生成Word文檔的文章就介紹到這了,更多相關(guān)Java POI生成Word文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jersey實現(xiàn)Restful服務(wù)(實例講解)
下面小編就為大家?guī)硪黄狫ersey實現(xiàn)Restful服務(wù)(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
springboot整合RabbitMQ發(fā)送短信的實現(xiàn)
本文會和SpringBoot做整合,實現(xiàn)RabbitMQ發(fā)送短信,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Nacos下線服務(wù)時,下線報錯選舉Leader失敗問題以及解決
這篇文章主要介紹了Nacos下線服務(wù)時,下線報錯選舉Leader失敗問題以及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot后端接收前端傳數(shù)組參數(shù)三種方法
這篇文章主要給大家介紹了關(guān)于springboot后端接收前端傳數(shù)組參數(shù)三種方法,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-07-07

