欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用POI生成Word文檔簡單代碼示例

 更新時間:2024年08月17日 14:25:44   作者:show_code_z  
Java?POI是一個用于操作Microsoft?Office格式文件的Java庫,包括?Word、Excel和PowerPoint等文件,這篇文章主要給大家介紹了關于Java使用POI生成Word文檔的相關資料,需要的朋友可以參考下

在開發(fā)中有時候我們需要導出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 開發(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("現居地");
        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開發(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);
        //設置為右對齊
        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文件了。

結果:

總結

到此這篇關于Java使用POI生成Word文檔的文章就介紹到這了,更多相關Java POI生成Word文檔內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Jersey實現Restful服務(實例講解)

    Jersey實現Restful服務(實例講解)

    下面小編就為大家?guī)硪黄狫ersey實現Restful服務(實例講解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java爬蟲抓取視頻網站下載鏈接

    Java爬蟲抓取視頻網站下載鏈接

    本文是通過JAVA獲取優(yōu)酷、土豆、酷6、6間房等視頻,小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • 詳解Spring中bean實例化的三種方式

    詳解Spring中bean實例化的三種方式

    本篇文章主要介紹了詳解Spring中bean實例化的三種方式,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Java架構設計之六步拆解 DDD

    Java架構設計之六步拆解 DDD

    DDD(Domain-Driven Design 領域驅動設計)是由Eric Evans最先提出,目的是對軟件所涉及到的領域進行建模,以應對系統(tǒng)規(guī)模過大時引起的軟件復雜性的問題
    2022-02-02
  • SpringBoot解決跨域的超實用方案分享

    SpringBoot解決跨域的超實用方案分享

    這篇文章介紹了使用SpringBoot解決跨域問題的方法,并提供了詳細的代碼示例和解釋,適合對跨域問題不太熟悉的讀者,感興趣的小伙伴跟著小編一起來學習吧
    2023-05-05
  • Java枚舉實現自增賦值的方法

    Java枚舉實現自增賦值的方法

    在Java編程里,枚舉(enum)其實是一種特別的類型,用來表示一組常量,當我們開發(fā)程序的時候,常常需要給這些枚舉加點其他功能,比如自增賦值的方法,這樣就能更方便地管理和使用啦,這篇文章和大家聊聊,怎么在Java中實現枚舉的自增賦值
    2025-04-04
  • springboot整合RabbitMQ發(fā)送短信的實現

    springboot整合RabbitMQ發(fā)送短信的實現

    本文會和SpringBoot做整合,實現RabbitMQ發(fā)送短信,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • Nacos下線服務時,下線報錯選舉Leader失敗問題以及解決

    Nacos下線服務時,下線報錯選舉Leader失敗問題以及解決

    這篇文章主要介紹了Nacos下線服務時,下線報錯選舉Leader失敗問題以及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • java 中RandomAccess接口源碼分析

    java 中RandomAccess接口源碼分析

    這篇文章主要介紹了java 中RandomAccess接口源碼分析的相關資料,需要的朋友可以參考下
    2017-05-05
  • springboot后端接收前端傳數組參數三種方法

    springboot后端接收前端傳數組參數三種方法

    這篇文章主要給大家介紹了關于springboot后端接收前端傳數組參數三種方法,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2023-07-07

最新評論