跳转到主内容
websoft网络软件专家 - 深耕网络技术,打造实用软件!

Java使用POI生成Word文档简单代码示例

在开发中有时候我们需要导出MS word文档。最近因为需要做一个生成word文件的功能。就将这块拿出来和大家分享。

生成word文件和我们写word文档是相同的概念,只不过在这里我们换成了用代码来操作。下面的例子中主要有添加页眉,页脚,正文(段落,表格)。在正文中,段落包含文字字体和背景的设置。表格主要是数据的填充和样式(有无边框)。这里写的例子给出的内容只是Java POI 方式生成word文件的极少数的一些方法,需要使用更多方法的还是要自己根据自己的需求去查看API。

看到很多小伙伴反应不能用的问题,这里我又重新把代码下载下来生成了一次试试。确实是没有问题。以前使用的是jdk6,最后一个版本使用的是jdk8.我再把我的maven导包情况贴出来。供大家参考,生成的文件MS office 和wps打开均没有问题。

org.apache.poiooxml-schemas1.1fr.opensagres.xdocreportorg.apache.poi.xwpf.converter.core1.0.6

那就直接先上代码吧:

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文件了。

结果:

总结到此这篇关于Java使用POI生成Word文档的文章就介绍到这了,更多相关Java POI生成Word文档内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:Java如何使用poi生成简单word文档并导出java导出生成word的简单方法Java实现读取Word模板文档并替换内容生成新文档Java使用poi生成word文档的简单实例Java通过freemarker生成Word文档导出的方式详解

相关文章