poi导出word:包括普通的段落以及表格。常用的api已经以注释的方式写了进去。
代码如下:
/*
* @(#)WordExport.java
* Copyright (c)
* All rights reserved
*/
package com.hyetec.zzjj.lcgl;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
/**
* Class description goes here
*@author zhangshaobo
*@version
*@time 2017-6-23
*@description
**/
public class WordExport {
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"));
/*
* 初号:42 小初:36
* 一号:26 小一:24
* 二号:22 小二:18
* 三号:16 小三:15
* 四号:14 小四:12
* 五号:10.5 小五:9
* 六号:7.5 小六:6.5
* 七号:5.5 八号:5
*/
/*
* add title
*/
//createParagraph
XWPFParagraph titleParagraph = document.createParagraph();
//alignment
titleParagraph.setAlignment(ParagraphAlignment.CENTER);//ParagraphAlignment 设置段落居中
XWPFRun titleParagraphRun = titleParagraph.createRun();
//setText
titleParagraphRun.setText("国网郑州供电公司建设部周工作汇报");
//font color
titleParagraphRun.setColor("000000"); //黑色
//font size
titleParagraphRun.setFontSize(22); //二号字 中文标题
//line height :
titleParagraphRun.setTextPosition(27);//行距
//background color 设置段落背景颜色
CTShd cTShd = titleParagraphRun.getCTR().addNewRPr().addNewShd();
cTShd.setVal(STShd.CLEAR);
cTShd.setFill("97FFFF");
//font family
titleParagraphRun.setFontFamily("仿宋");
//font bold
titleParagraphRun.setBold(true);
titleParagraphRun.setCapitalized(true);
//font family 第二种方法
//CTRPr pr= titleParagraphRun.getCTR().addNewRPr();
//pr.addNewRFonts().setAscii("仿宋");
// CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
// XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
/*//时间
XWPFParagraph paragraphTime = document.createParagraph();
paragraphTime.setAlignment(ParagraphAlignment.CENTER);//居中
XWPFRun runTime = paragraphTime.createRun();
runTime.setText("(9月26日-10月07日)");
runTime.setTextPosition(27);//行距
runTime.setFontSize(15);//字体大小
//换行
XWPFParagraph paragraph0 = document.createParagraph();
XWPFRun run0 = paragraph0.createRun();
run0.setTextPosition(27);
run0.setFontSize(12);
run0.setText("\r");
//一级标题
XWPFParagraph paragraph = document.createParagraph();
paragraph.setIndentationHanging(200);//首行缩进
XWPFRun titleParagraphRun2 = paragraph.createRun();
titleParagraphRun2.setText("一、本周重点工作完成情况"); //黑体小四
titleParagraphRun2.setFontFamily("黑体");
titleParagraphRun2.setTextPosition(27);
titleParagraphRun2.setColor("000000");
titleParagraphRun2.setFontSize(12);
XWPFParagraph paragraph2 = document.createParagraph();
XWPFRun titleParagraphRun3 = paragraph2.createRun();
titleParagraphRun3.setText("(一)综合管理方面"); //仿宋 小四
titleParagraphRun3.setFontFamily("仿宋");
titleParagraphRun3.setTextPosition(50); //行间距
titleParagraphRun3.setColor("000000");
titleParagraphRun3.setFontSize(12);
StringBuffer buffer = new StringBuffer();
buffer.append("<!DOCTYPE HTML><html><head><title>测试文档</title><meta http-equiv=");
buffer.append("\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>");
buffer.append("<p style=\"text-indent:32px;line-height:36px\"><span style=\"font-size:16px;font-family:仿宋_GB2312\">1</span><span style=\"font-size: 16px;font-family:仿宋_GB2312\">、建设部陪同胡玉生副总经理到市重点项目办与项目办夏扬主任、吕明体副主任及相关领导进行座谈,重点对接郑州市区21座变电站落地事宜。</span></p><p style=\"text-indent:32px;line-height:36px\"><span style=\"font-size:16px;font-family:仿宋_GB2312\">2</span><span style=\"font-size: 16px;font-family:仿宋_GB2312\">、参加省公司组织的项目管理、计划进度编制培训班。</span></p><p style=\"text-indent:32px;line-height:36px\"><span style=\"font-size:16px;font-family:仿宋_GB2312\">3</span><span style=\"font-size: 16px;font-family:仿宋_GB2312\">、参加省公司组织的35千伏工程模块化设计宣贯会。</span></p><p style=\"text-indent:32px;line-height:36px\"><span style=\"font-size:16px;font-family:仿宋_GB2312\">4</span><span style=\"font-size: 16px;font-family:仿宋_GB2312\">、迎接省公司优质工程复查组对长乐扩、吉庆扩的优质工程复验。</span></p><p style=\"text-indent:32px;line-height:36px\"><span style=\"font-size:16px;font-family:仿宋_GB2312\">5</span><span style=\"font-size: 16px;font-family:仿宋_GB2312\">、向省公司建设部汇报红旗变变更签证。</span></p><p style=\"text-indent:32px;line-height:36px\"><span style=\"font-size:16px;font-family:仿宋_GB2312\">6</span><span style=\"font-size: 16px;font-family:仿宋_GB2312\">、参加李智敏副总经理组织的郑州供电公司迎峰度冬工作会。</span></p><p style=\"text-indent:32px;line-height:36px\"><span style=\"font-size:16px;font-family:仿宋_GB2312\">7</span><span style=\"font-size: 16px;font-family:仿宋_GB2312\">、东区建设局来郑州局对接东区排管建设等内容,内容见后文。</span></p><p><br/></p>");
buffer.append("</body></html>");
System.out.println("====================buffer========================="+buffer.toString());
//段落
XWPFParagraph firstParagraph = document.createParagraph();
XWPFRun run = firstParagraph.createRun();
run.setText(buffer.toString());
run.setTextPosition(25);
run.setColor("000000");
run.setFontSize(10);*/
//创建段落用于换行
XWPFParagraph paragraph1 = document.createParagraph();
XWPFRun paragraphRun1 = paragraph1.createRun();
//换行符
paragraphRun1.setText("\r");
/**
* 创建表格
*/
//create table
XWPFTable infoTable = document.createTable();
//去表格边框 默认是带有边框的
infoTable.getCTTbl().getTblPr().unsetTblBorders();
//列宽自动分割
CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
//
infoTableWidth.setType(STTblWidth.DXA);
//table -width 设置表格宽度
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");
//两个表格之间加个换行
XWPFParagraph paragraph3 = document.createParagraph();
XWPFRun paragraphRun = paragraph3.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开发工程师");
/*
* 页眉页脚
*/
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 = "国家电网";
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);
/*ByteArrayInputStream bais = null;
String content = buffer.toString();
byte b[] = content.getBytes();
bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
poifs.writeFilesystem(out);*/
document.write(out);//xwpf
out.close();
System.out.println("create_table document written success.");
}
}
原网址: 访问
创建于: 2023-03-22 15:41:46
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论