JAVA语言使用POI导出Word文档的操作教程
小标 2018-09-11 来源 : 阅读 1210 评论 0

摘要:本文主要向大家介绍了JAVA语言使用POI导出Word文档的操作教程,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言使用POI导出Word文档的操作教程,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

一、主要pom依赖

                <dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.16</version>

</dependency>

   

二、需要导出word模板

三、相关导出代码

package com.iflytek.chy;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

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.XWPFTableCell;

import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class App {

private static String regex = "[a-zA-Z]+";

private static Pattern pattern = Pattern.compile(regex);

public static void main(String[] args) throws IOException {

Map<String, String> map = new HashMap<String, String>();

map.put("qbdj", "你好");

map.put("zlhtbt", "姓名");

map.put("fwqh", "性别");

map.put("qbly", "年龄");

map.put("lrsj", "生日");

map.put("bt", "职业");

map.put("xxzw", "工作城市");

map.put("bb", "武汉光谷");

map.put("ss", "武汉光谷");

map.put("ff", "武汉光谷");

map.put("gg", "武汉光谷");

map.put("hh", "武汉光谷");

map.put("dd", "武汉光谷");

String filePath = "D:\\java_workspace_qbNew\\qb-yp\\qb-xsfx\\target\\classes\\config\\template\\SWSJ-test.docx";

InputStream is = new FileInputStream(filePath);

@SuppressWarnings("resource")

XWPFDocument doc = new XWPFDocument(is);

List<XWPFParagraph> list = doc.getParagraphs();

// 替换占位符

for (int i = 0; i < list.size(); i++) {

XWPFParagraph xwpfParagraph = list.get(i);

List<XWPFRun> runs = xwpfParagraph.getRuns();

if (runs != null && runs.size() > 0) {

for (int j = runs.size() - 1; j >= 0; j--) {

if (map.containsKey(runs.get(j).text())) {

System.out.println("run:" + runs.get(j).text());

runs.get(j).setText(map.get(runs.get(j).text()), 0);

}

}

}

}

// 替换表格中的占位符

List<XWPFTable> tables = doc.getTables();

for (XWPFTable xwpfTable : tables) {

for (int i = 0; i < xwpfTable.getNumberOfRows(); i++) {

XWPFTableRow row = xwpfTable.getRow(i);

List<XWPFTableCell> tableCells = row.getTableCells();

for (XWPFTableCell xwpfTableCell : tableCells) {

List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();

for (XWPFParagraph xwpfParagraph : paragraphs) {

List<XWPFRun> runs = xwpfParagraph.getRuns();

for (XWPFRun xwpfRun : runs) {

String trim = xwpfRun.text();

Matcher matcher = pattern.matcher(trim);

String temp = "";

while (matcher.find()) {

temp = matcher.group();

}

if (StringUtils.isNotBlank(temp)) {

trim = trim.replace(temp, map.get(temp));

}

xwpfRun.setText(trim, 0);

}

}

}

}

}

OutputStream os = new FileOutputStream(

"D:\\\\java_workspace_qbNew\\\\qb-yp\\\\qb-xsfx\\\\target\\\\classes\\\\config\\\\template\\\\MY-SWSJ-test.docx");

doc.write(os);

os.close();

is.close();

}

}


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程