最近公司做的项目需要实现导出Word文档的功能,网上关于POI生成Word文档的例子很少,找了半天才在官网里找到个Demo,有了Demo一切就好办了。
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xwpf.usermodel;
import java.io.FileOutputStream;
/**
* A simple WOrdprocessingML document created by POI XWPF API
*
* @author Yegor Kozlov
*/
public class SimpleDocument {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p1 = doc.createParagraph();
p1.setAlignment(ParagraphAlignment.CENTER);
p1.setBorderBottom(Borders.DOUBLE);
p1.setBorderTop(Borders.DOUBLE);
p1.setBorderRight(Borders.DOUBLE);
p1.setBorderLeft(Borders.DOUBLE);
p1.setBorderBetween(Borders.SINGLE);
p1.setVerticalAlignment(TextAlignment.TOP);
XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setBold(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);
XWPFParagraph p2 = doc.createParagraph();
p2.setAlignment(ParagraphAlignment.RIGHT);
//BORDERS
p2.setBorderBottom(Borders.DOUBLE);
p2.setBorderTop(Borders.DOUBLE);
p2.setBorderRight(Borders.DOUBLE);
p2.setBorderLeft(Borders.DOUBLE);
p2.setBorderBetween(Borders.SINGLE);
XWPFRun r2 = p2.createRun();
r2.setText("jumped over the lazy dog");
r2.setStrike(true);
r2.setFontSize(20);
XWPFRun r3 = p2.createRun();
r3.setText("and went away");
r3.setStrike(true);
r3.setFontSize(20);
r3.setSubscript(VerticalAlign.SUPERSCRIPT);
XWPFParagraph p3 = doc.createParagraph();
p3.setWordWrap(true);
p3.setPageBreak(true);
//p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
p3.setAlignment(ParagraphAlignment.BOTH);
p3.setSpacingLineRule(LineSpacingRule.EXACT);
p3.setIndentationFirstLine(600);
XWPFRun r4 = p3.createRun();
r4.setTextPosition(20);
r4.setText("To be, or not to be: that is the question: "
+ "Whether 'tis nobler in the mind to suffer "
+ "The slings and arrows of outrageous fortune, "
+ "Or to take arms against a sea of troubles, "
+ "And by opposing end them? To die: to sleep; ");
r4.addBreak(BreakType.PAGE);
r4.setText("No more; and by a sleep to say we end "
+ "The heart-ache and the thousand natural shocks "
+ "That flesh is heir to, 'tis a consummation "
+ "Devoutly to be wish'd. To die, to sleep; "
+ "To sleep: perchance to dream: ay, there's the rub; "
+ ".......");
r4.setItalic(true);
//This would imply that this break shall be treated as a simple line break, and break the line after that word:
XWPFRun r5 = p3.createRun();
r5.setTextPosition(-10);
r5.setText("For in that sleep of death what dreams may come");
r5.addCarriageReturn();
r5.setText("When we have shuffled off this mortal coil,"
+ "Must give us pause: there's the respect"
+ "That makes calamity of so long life;");
r5.addBreak();
r5.setText("For who would bear the whips and scorns of time,"
+ "The oppressor's wrong, the proud man's contumely,");
r5.addBreak(BreakClear.ALL);
r5.setText("The pangs of despised love, the law's delay,"
+ "The insolence of office and the spurns" + ".......");
FileOutputStream out = new FileOutputStream("simple.docx");
doc.write(out);
out.close();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# apache
# poi
# 生成word
# word
# java
# poi生成word文档
# Java使用poi-tl1.9.1生成Word文档的技巧分享
# java 使用POI合并两个word文档
# java实现在线预览--poi实现word、excel、ppt转html的方法
# java poi设置生成的word的图片为上下型环绕以及其位置的实现
# java Apache poi 对word doc文件进行读写操作
# Java使用poi将word转换为html
# Java中使用Apache POI读取word文件简单示例
# java使用poi读取ppt文件和poi读取excel、word示例
# java通过poi解析word入门的操作方法
# 文档
# 就好
# 半天
# 找了
# 办了
# 官网
# 大家多多
# 网上
# Yegor
# public
# Kozlov
# XWPF
# API
# author
# args
# main
# String
# SimpleDocument
# static
# void
相关文章:
网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?
php条件判断怎么写_ifelse和switchcase的使用区别【对比】
,如何利用word制作宣传手册?
专业网站建设制作报价,网页设计制作要考什么证?
中山网站制作网页,中山新生登记系统登记流程?
音乐网站服务器如何优化API响应速度?
如何在香港免费服务器上快速搭建网站?
临沂网站制作企业,临沂第三中学官方网站?
如何撰写建站申请书?关键要点有哪些?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
如何选择PHP开源工具快速搭建网站?
青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?
在线制作视频网站免费,都有哪些好的动漫网站?
上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?
,在苏州找工作,上哪个网站比较好?
昆明高端网站制作公司,昆明公租房申请网上登录入口?
无锡营销型网站制作公司,无锡网选车牌流程?
建站之星与建站宝盒如何选择最佳方案?
如何注册花生壳免费域名并搭建个人网站?
高配服务器限时抢购:企业级配置与回收服务一站式优惠方案
深圳防火门网站制作公司,深圳中天明防火门怎么编码?
h5网站制作工具有哪些,h5页面制作工具有哪些?
简历在线制作网站免费版,如何创建个人简历?
青岛网站建设如何选择本地服务器?
如何续费美橙建站之星域名及服务?
如何在万网自助建站平台快速创建网站?
如何高效生成建站之星成品网站源码?
如何设计高效校园网站?
如何在阿里云ECS服务器部署织梦CMS网站?
网站网页制作电话怎么打,怎样安装和使用钉钉软件免费打电话?
C++时间戳转换成日期时间的步骤和示例代码
如何通过PHP快速构建高效问答网站功能?
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
山东云建站价格为何差异显著?
,怎么在广州志愿者网站注册?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
建站中国官网:模板定制+SEO优化+建站流程一站式指南
宿州网站制作公司兴策,安徽省低保查询网站?
手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?
建站之星客服服务时间及联系方式如何?
如何快速搭建二级域名独立网站?
定制建站策划方案_专业建站与网站建设方案一站式指南
专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?
GML (Geography Markup Language)是什么,它如何用XML来表示地理空间信息?
如何登录建站主机?访问步骤全解析
高防服务器租用首荐平台,企业级优惠套餐快速部署
建站之星如何通过成品分离优化网站效率?
如何高效利用亚马逊云主机搭建企业网站?
香港服务器租用费用高吗?如何避免常见误区?
*请认真填写需求信息,我们会在24小时内与您取得联系。