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 systemFileOutputStream 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 *///createParagraphXWPFParagraph titleParagraph = document.createParagraph();//alignmenttitleParagraph.setAlignment(ParagraphAlignment.CENTER);//ParagraphAlignment  设置段落居中XWPFRun titleParagraphRun = titleParagraph.createRun();//setTexttitleParagraphRun.setText("国网郑州供电公司建设部周工作汇报");//font colortitleParagraphRun.setColor("000000");      //黑色//font sizetitleParagraphRun.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);//xwpfout.close();System.out.println("create_table document written success.");
}

}

poi导出word:包括**普通的段落以及表格**。常用的**api**已经以注释的方式写了进去。相关推荐

  1. POI导出word表格 office打开没问题 wps打开列有问题

    POI导出word表格 office打开没问题 wps打开列有问题 模板样式 导出文档office打开 wps打开文件 1.给表格设置宽,指定宽度 2.将布局固定 3.动态设置单元格的宽度 4.重点设 ...

  2. poi导出word时内嵌表格场景,即开即用

    poi导出word内嵌表格 设计分析部分 后端代码部分 xml文件配置 实体类 controller层 使用到的工具类 service层 前端代码部分 测试页面 设计分析部分 首先要对导出word的需 ...

  3. poi导出word文件(带表格)

    poi导出word文件(带表格) 一.背景介绍 现有业务需求根据前端页面上所选的时间和列,来生成word表格,方便打印. 二.POM <dependency><groupId> ...

  4. POI导出word文件中表格合并方法(行合并,列合并)

    项目中遇到记录一下 POI导出word文件中表格合并方法(行合并,列合并) . // word表格跨列合并单元格//row 指定行.fromCell 开始列数.toCell 结束列数.public v ...

  5. JAVA使用POI导出Word文档和Excel文档

    导包 注意:POI包的版本,不同版本poi导出API会有差别!!!! <dependency><groupId>org.apache.poi</groupId>&l ...

  6. poi导出word模板

    **poi导出word模板段落处理 流程: 1:读取文档:` //获取docx解析对象 XWPFDocument document = new XWPFDocument(POIXMLDocument. ...

  7. 使用POI导出word文档

    使用POI导出word文档 步骤一.创建一个需要导出的word文档模板.将表里需要填充的数据对应好要填充的键最后在表的最前方加入一个书签(一个table一个书签) 步骤二.工具类 public cla ...

  8. SpringBoot Poi导出word,浏览器下载

    文章目录 SpringBoot Poi导出word,浏览器下载 1.引依赖: 2.写代码(生成本地word): 3.返回给浏览器下载 1.如何返回给浏览器让它下载 4.解决方案 5.为什么没使用eas ...

  9. Java poi导出word文件

    Java在导出word文件时主要对表格中内容垂直居中处理做以记录方便后续碰到类似问题解决. maven pom.xml中添加poi依赖 <!-- word.excel工具 --> < ...

最新文章

  1. usaco ★Stamps 邮票
  2. 调用jsapi缺少参数appid_【Python学习笔记】18、函数的参数关键字参数
  3. JSF实现“Hello World!”
  4. 详解OS X和iOS图像处理框架Core Image
  5. gc就是fullgc吗 major_线上出现fullgc问题如何排查?
  6. 玩转 Tomcat 配置必备的 10 个小技巧!
  7. c++求n的几次方_14.八年级数学:若a+b+c=1,怎么求 a+b+c的值?分式培优拓展
  8. android编辑配置文件,如何在android studio中修改配置文件
  9. java零碎要点013---JAVA执行js_java执行JavaScript_java执行js引擎不能识别document浏览器内置对象解决办法
  10. 何时在脚本标记中需要CDATA节?
  11. Altium Designer元件库常见获取方法
  12. 乐山市计算机学校灵异事件,真人真事:我们学校那些灵异的事情(西南交通大学灵异事件)...
  13. 搜索引擎开源代码(转)
  14. 高频电源模块FB230D10NZ-D浮充模块充电机
  15. STM32 CM0+系列芯片的NRST模式之坑
  16. C++之struct构造函数
  17. 机制检验、异质性检验、调节效应
  18. MICCAI2021部分论文使用数据集总结
  19. bat批处理---实现输入指定拷贝文件
  20. 【C#学习】文本字体、颜色操作

热门文章

  1. 20_微信小程序-BLE低功耗蓝牙开发-发布小程序
  2. 优秀的设计师是怎么找素材下载的?
  3. 如何 vscode 文件标签栏多行显示?
  4. 全国计算机专业人才证书有什么用?
  5. 中金公司、 汇金公司、 中投公司有什么联系和区别?
  6. C#网络应用编程 第2版
  7. H5怎么关闭IOS键盘的首字母自动大写
  8. SHELL编程之CASE语句+函数+正则
  9. mysql查询_MySQL常用查询
  10. paypal ipn java_PayPal IPN验证