最近公司做的项目需要实现导出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
相关文章:
建站OpenVZ教程与优化策略:配置指南与性能提升
上海网站制作网站建设公司,建筑电工证网上查询系统入口?
如何通过智能用户系统一键生成高效建站方案?
微信小程序 input输入框控件详解及实例(多种示例)
建设网站制作价格,怎样建立自己的公司网站?
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
模具网站制作流程,如何找模具客户?
广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?
开封网站制作公司,网络用语开封是什么意思?
如何在IIS7中新建站点?详细步骤解析
大学网站设计制作软件有哪些,如何将网站制作成自己app?
手机怎么制作网站教程步骤,手机怎么做自己的网页链接?
如何选择可靠的免备案建站服务器?
实现点击下箭头变上箭头来回切换的两种方法【推荐】
如何快速生成橙子建站落地页链接?
北京网站制作的公司有哪些,北京白云观官方网站?
香港服务器部署网站为何提示未备案?
Android使用GridView实现日历的简单功能
如何在Golang中指定模块版本_使用go.mod控制版本号
nginx修改上传文件大小限制的方法
如何快速使用云服务器搭建个人网站?
如何破解联通资金短缺导致的基站建设难题?
微信小程序 五星评分(包括半颗星评分)实例代码
c++23 std::expected怎么用 c++优雅处理函数错误返回【详解】
装修招标网站设计制作流程,装修招标流程?
如何在Windows虚拟主机上快速搭建网站?
宝塔新建站点报错如何解决?
建站之星如何一键生成手机站?
如何配置FTP站点权限与安全设置?
如何挑选优质建站一级代理提升网站排名?
无锡制作网站公司有哪些,无锡优八网络科技有限公司介绍?
如何在Windows服务器上快速搭建网站?
如何在腾讯云服务器上快速搭建个人网站?
西安大型网站制作公司,西安招聘网站最好的是哪个?
如何在阿里云完成域名注册与建站?
如何在局域网内绑定自建网站域名?
制作网站哪家好,cc、.co、.cm哪个域名更适合做网站?
建站主机是否属于云主机类型?
建站之星与建站宝盒如何选择最佳方案?
电影网站制作价格表,那些提供免费电影的网站,他们是怎么盈利的?
如何正确下载安装西数主机建站助手?
活动邀请函制作网站有哪些,活动邀请函文案?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
湖北网站制作公司有哪些,湖北清能集团官网?
已有域名和空间,如何快速搭建网站?
文字头像制作网站推荐软件,醒图能自动配文字吗?
青浦网站制作公司有哪些,苹果官网发货地是哪里?
小程序网站制作需要准备什么资料,如何制作小程序?
如何零基础开发自助建站系统?完整教程解析
小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建
*请认真填写需求信息,我们会在24小时内与您取得联系。