Java POI 生成Word文檔

在開發中有時候我們需要導出 word文檔。最近因爲需要做一個生成word文件的功能。就將這塊拿出來和大家分享。

      生成word文件和我們寫word文檔是相同的概念,只不過在這裏我們換成了用代碼來操作。下面的例子中主要有添加頁眉,頁腳,正文(段落,表格)。在正文中,段落包含文字字體和背景的設置。表格主要是數據的填充和樣式(有無邊框)。這裏寫的例子給出的內容只是Java POI 方式生成word文件的極少數的一些方法,需要使用更多方法的還是要自己根據自己的需求去查看API。

       看到很多小夥伴反應不能用的問題,這裏我又重新把代碼下載下來生成了一次試試。確實是沒有問題。以前使用的是jdk6,最後一個版本使用的是jdk8.我再把我的導包情況貼出來。供大家參考,生成的文件office 和wps打開均沒有問題。

那就直接先上代碼吧:

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 wan.tao on 2018/10/23.
 */
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文件了。

如圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章