java工具类POI导出word
1、新建一个word,里面填写内容,如:

2、导出wordjava类
/**
* POI导出word测试
* @throws Exception
*/
@RequestMapping(value="exportApplyForm")
public void exportApplyForm(HttpServletResponse response) throws Exception { Map<String, Object> params = new HashMap<String, Object>(); params.put("${name}", "1");
params.put("${sex}","1");
params.put("${political}", "1");
params.put("${place}", "1");
params.put("${classes}", "1");
params.put("${id}", "1");
params.put("${qq}", "1");
params.put("${tel}", "1");
params.put("${oldJob}", "1");
params.put("${swap}", "1");
params.put("${first}", "1");
params.put("${second}", "1");
params.put("${award}", "1");
params.put("${achievement}", "1");
params.put("${advice}", "1");
params.put("${attach}", "1"); XwpfTUtil xwpfTUtil = new XwpfTUtil(); XWPFDocument doc;
String fileNameInResource = "sta.docx";
InputStream is;
/*is = new FileInputStream(filePath);*/
is = getClass().getClassLoader().getResourceAsStream(fileNameInResource); //本身就在编译路径下。。。。 doc = new XWPFDocument(is); xwpfTUtil.replaceInPara(doc, params);
//替换表格里面的变量
xwpfTUtil.replaceInTable(doc, params);
OutputStream os = response.getOutputStream(); response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment;filename=测试.docx"); doc.write(os); xwpfTUtil.close(os);
xwpfTUtil.close(is); os.flush();
os.close();
}
3、文件工具类
package util; import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test; import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class XwpfTUtil { /*String filePath = "/sta.docx";
InputStream is;
XWPFDocument doc;
Map<String, Object> params = new HashMap<String, Object>(); {
params.put("${name}", "xxx");
params.put("${sex}", "男");
params.put("${political}", "共青团员");
params.put("${place}", "sssss");
params.put("${classes}", "3102");
params.put("${id}", "213123123");
params.put("${qq}", "213123");
params.put("${tel}", "312313213");
params.put("${oldJob}", "sadasd");
params.put("${swap}", "是");
params.put("${first}", "asdasd");
params.put("${second}", "综合事务部");
params.put("${award}", "asda");
params.put("${achievement}", "完成科协网站的开发");
params.put("${advice}", "没有建议");
params.put("${attach}", "无"); try {
is = new FileInputStream(filePath);
doc = new XWPFDocument(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }*/ /**
* 用一个docx文档作为模板,然后替换其中的内容,再写入目标文档中。
*
* @throws Exception
*/
/*@Test
public void testTemplateWrite() throws Exception {
//替换段落里面的变量
this.replaceInPara(doc, params);
//替换表格里面的变量
this.replaceInTable(doc, params);
OutputStream os = new FileOutputStream("D:\\sta1.docx");
doc.write(os);
this.close(os);
this.close(is);
}*/ /*@Test
public void myTest1() throws Exception {
*//*Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
XWPFParagraph para;
while (iterator.hasNext()) {
para = iterator.next();
List<XWPFRun> runs = para.getRuns();
para.removeRun(0);
para.insertNewRun(0).setText("hello");
} OutputStream os = new FileOutputStream("D:\\sta1.docx");
doc.write(os);
this.close(os);
this.close(is);*//* System.out.println(this.matcher("报告日期:${reportDate}").find()); }*/ /*@Test
public void myReplaceInPara() {
// Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
// XWPFParagraph para;
// while (iterator.hasNext()) {
// para = iterator.next();
// List<XWPFRun> runs = para.getRuns();
//
//
// } System.out.println('{'=='{'); }*/ /**
* 替换段落里面的变量
*
* @param doc 要替换的文档
* @param params 参数
*/
public void replaceInPara(XWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
XWPFParagraph para;
while (iterator.hasNext()) {
para = iterator.next();
this.replaceInPara(para, params);
}
} /**
* 替换段落里面的变量
*
* @param para 要替换的段落
* @param params 参数
*/
public void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
List<XWPFRun> runs;
Matcher matcher;
if (this.matcher(para.getParagraphText()).find()) {
runs = para.getRuns(); int start = -1;
int end = -1;
String str = "";
for (int i = 0; i < runs.size(); i++) {
XWPFRun run = runs.get(i);
String runText = run.toString();
System.out.println("------>>>>>>>>>" + runText);
if ('$' == runText.charAt(0)&&'{' == runText.charAt(1)) {
start = i;
}
if ((start != -1)) {
str += runText;
}
if ('}' == runText.charAt(runText.length() - 1)) {
if (start != -1) {
end = i;
break;
}
}
}
System.out.println("start--->"+start);
System.out.println("end--->"+end); System.out.println("str---->>>" + str); for (int i = start; i <= end; i++) {
para.removeRun(i);
i--;
end--;
System.out.println("remove i="+i);
} for (String key : params.keySet()) {
if (str.equals(key)) {
para.createRun().setText((String) params.get(key));
break;
}
} }
} /**
* 替换表格里面的变量
*
* @param doc 要替换的文档
* @param params 参数
*/
public void replaceInTable(XWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFTable> iterator = doc.getTablesIterator();
XWPFTable table;
List<XWPFTableRow> rows;
List<XWPFTableCell> cells;
List<XWPFParagraph> paras;
while (iterator.hasNext()) {
table = iterator.next();
rows = table.getRows();
for (XWPFTableRow row : rows) {
cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
paras = cell.getParagraphs();
for (XWPFParagraph para : paras) {
this.replaceInPara(para, params);
}
}
}
}
} /**
* 正则匹配字符串
*
* @param str
* @return
*/
private Matcher matcher(String str) {
Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
return matcher;
} /**
* 关闭输入流
*
* @param is
*/
public void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* 关闭输出流
*
* @param os
*/
public void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
4、maven对应的jar包
<!--导入操作excel所需要的jar包-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>
poi</artifactId>
<version>3.10-beta2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-beta2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.10-beta2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.0.2-FINAL</version>
</dependency>
java工具类POI导出word的更多相关文章
- 使用POI导出Word(含表格)的实现方式及操作Word的工具类
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- Java 用Freemarker完美导出word文档(带图片)
Java 用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...
- poi导出word表格详解 超详细了
转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138 一.效果如下 二.js代码 function export_word( ...
- poi导出word
最近做了个poi导出word的功能 下面是代码: 一个可以参考的例子: package com.lzb.crm.web; import java.io.FileOutputStream; import ...
- poi导出word时设置兼容性
接上一篇poi导出word http://www.cnblogs.com/xiufengd/p/4708680.html. public static void setAuto(XWPFDocumen ...
- java工具类系列 (四.SerializationUtils)
java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- 排名前 16 的 Java 工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- 排名前16的Java工具类
原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...
随机推荐
- 【webservice】使用命令wsimport构建WebService客户端
wsimport命令介绍 在JDK的bin文件夹中,有一个wsimport.exe,这个工具依据wsdl文件生成相应的类文件,然后用这些类文件,就可以像调用本地的类一样调用WebService提供的方 ...
- 利用eclipse+jdk1.8搭建Java开发环境(超具体的)
利用eclipse+jdk1.8搭建Java开发环境 转载请声明出处:http://blog.csdn.net/u013067166/article/details/50267003 引言:eclip ...
- PCM、G.729等常用VoIP编码的理论带宽计算
可能通信背景的同学,一提到PCM编码,脑海里都能跳出来一个数值64K. 一.64KB还是64Kb? 64Kb! 二.哪里来的64Kb? CCITT规定抽样率为每秒8000KHz,每抽样值编8位码,所以 ...
- STM32定时器T2纯软件仿真时间准确,JTAG在线调试查看时间不准的问题
通过查看Sec的值和上次中断的差值计算的,虽然这个值是不准的 ,但实际上时间是准的, 原因如下:stm32在调试模式下虽然进断点之后程序停止了,但定时器的时钟还在走,计数器还在计数,若要在产生断点时计 ...
- Axis1.4开发webservice个人笔记
Axis1.4开发webservice SOAP 是基于 XML 的简易协议,SOAP 即Simple Object Access Protocol(简单对象访问协议),可使应用程序在 HTTP 之上 ...
- SQL 正则表达式 `(user_log_acct)?+.+`
SELECT 语句可以使用正则表达式做列选择,下面的语句查询除了 ds 和 hr 之外的所有列: SELECT `(ds|hr)?+.+` FROM test
- [转]Java 变量和常量
变量和常量 在程序中存在大量的数据来代表程序的状态,其中有些数据在程序的运行过程中值会发生改变,有些数据在程序运行过程中值不能发生改变,这些数据在程序中分别被叫做变量和常量. 在实际的程序中,可以根据 ...
- [转]BSIM Parameters
Name Alias Description MOSFET LevelL LV1 Channel Length (L)This is also the effective channel length ...
- Android Studio开发-高效插件强烈推荐
Android Studio开发-高效插件强烈推荐 现在Android的开发者基本上都使用Android Studio进行开发(如果你还在使用eclipse那也行,毕竟你乐意怎么样都行).使用好And ...
- Android Manifest.xml文件的结构及作用
原文链接:http://android.eoe.cn/topic/android_sdk 每一个应用程序在工程的根目录下必须要有一个AndroidManifest.xml文件(一定要用这个名称).这个 ...