Java POI 生成Word文件
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/zhouseawater/article/details/54289495
在開發中有時候我們需要匯出MS word文件。最近因為需要做一個生成word檔案的功能。就將這塊拿出來和大家分享。
生成word檔案和我們寫word文件是相同的概念,只不過在這裡我們換成了用程式碼來操作。下面的例子中主要有新增頁首,頁尾,正文(段落,表格)。在正文中,段落包含文字字型和背景的設定。表格主要是資料的填充和樣式(有無邊框)。這裡寫的例子給出的內容只是Java POI 方式生成word檔案的極少數的一些方法,需要使用更多方法的還是要自己根據自己的需求去檢視API。
看到很多小夥伴反應不能用的問題,這裡我又重新把程式碼下載下來生成了一次試試。確實是沒有問題。以前使用的是jdk6,最後一個版本使用的是jdk8.我再把我的maven導包情況貼出來。供大家參考,生成的檔案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();
-
//設定段落居中
-
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);
-
//設定段落背景顏色
-
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 開發工程師");
-
//表格第二行
-
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("現居地");
-
infoTableRowFive.getCell(1).setText(": xx");
-
//兩個表格之間加個換行
-
XWPFParagraph paragraph = document.createParagraph();
-
XWPFRun paragraphRun = paragraph.createRun();
-
paragraphRun.setText("\r");
-
//工作經歷表格
-
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("結束時間");
-
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開發工程師");
-
//表格第三行
-
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開發工程師");
-
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);
-
//設定為右對齊
-
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.");
-
}
-
}
程式碼我放到這一個檔案當中了。下面我就一些程式碼做一些解釋,因為有的是我在做的過程中遇到的問題。大部分的程式碼大家都是一眼就可以看懂的。
-
//設定段落背景顏色
-
CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
-
cTShd.setVal(STShd.CLEAR);
-
cTShd.setFill("97FFFF");
這段程式碼設定段落的背景顏色。
如果我們的表格不需要邊框呢就加下面的程式碼:
infoTable.getCTTbl().getTblPr().unsetTblBorders();
infoTable換成自己的table名稱就可以了。
建立一個表格的時候設定列寬跟隨內容伸縮
-
CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
-
infoTableWidth.setType(STTblWidth.DXA);
-
infoTableWidth.setW(BigInteger.valueOf(9072));
其他的程式碼我就不解釋了。執行就可以得到我們的word檔案了。
結果: