poi导出word模板项目实例(一个文件)
在页面上填写值,然后导出到word模板中,并把页面上的值带到模板中,也就是导出word文档,提前有word 的模板形式,
1.jsp 页面
<table class="formTable">
<TR>
<TD class="label">会议地点</TD>
<TD class="content">
<INPUT id="meetingSite" type="text" class="text" name="CommonLink/meetingSite"></TD>
<TD class="label">会议人员</TD>
<TD>
<INPUT id="meetingPerson" type="text" class="text" name="CommonLink/meetingPerson"></TD>
</TR>
<TR>
<TD class="label">会议内容</TD>
<TD class="content">
<INPUT id="meetingContent" type="text" class="text" name="CommonLink/meetingContent"></TD>
<TD class="label">会议时间</TD>
<TD><INPUT id="meetingDate" type="text" class="text" name="CommonLink/meetingDate"></TD>
</TR>
<TR>
<TD class="label">总经理</TD>
<TD class="content">
<INPUT id="manager" type="text" class="text" name="CommonLink/manager"></TD>
<TD class="label">采购部门</TD>
<TD><INPUT id="purchaseDep" type="text" class="text" name="CommonLink/purchaseDep"></TD>
</TR>
</table>
<div id="btns" class="form-btns">
<INPUT value="导出word文档" type="button" class="btn" onclick="exportWord();">
</div>
2.js异步的方法
function exportWord(){
var data = setData();
jQuery.post("http://localhost:8080/expWord/GKBX29_word.jsp",data,function(data1){
var url = data1; //回调函数,返回值是地址,data1
window.open(url); //打开
});
}
function setData(){
var data = {};
data.author='zzz';
data.meetingSite = document.getElementById("meetingSite").value;
data.purchaseDep = document.getElementById("purchaseDep").value;
data.meetingPerson = document.getElementById("meetingPerson").value;
data.meetingContent = document.getElementById("meetingContent").value;
data.meetingDate = document.getElementById("meetingDate").value;
data.manager = document.getElementById("manager").value;
return data;
}
3.GKBX29_word.jsp其实是后台的处理方法,因为本次开发用EOS,所以在jsp用request接收值,并传递到模板
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.boco.eoms.word.client.word"%> <%
request.setCharacterEncoding("UTF-8"); String author = request.getParameter("author"); String meetingSite = request.getParameter("meetingSite");
String purchaseDep = request.getParameter("purchaseDep");
String meetingPerson = request.getParameter("meetingPerson");
String meetingContent = request.getParameter("meetingContent");
String meetingDate = request.getParameter("meetingDate");
String manager = request.getParameter("manager"); word w = new word();
Map map = new HashMap(); map.put("author", author); map.put("meetingSite", meetingSite);
map.put("purchaseDep", purchaseDep);
map.put("meetingPerson", meetingPerson);
map.put("meetingContent", meetingContent);
map.put("meetingDate", meetingDate);
map.put("manager", manager); String url = w.replacWordByMap(map, "model"); //模板名称,默认是.doc文件
response.getWriter().write(url);
%>
4. 生成word 的方法
package com.boco.eoms; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; //***************************************************************// //** 需要使用变量的地方可以用如下替换 ${name} ,name为变量名,在map中key使用变量名,value使用要替换成的字符串,如 map.put("name","栾主峰") **//
//***************************************************************// public class word { /**
* 根据传入的模板编号将变量替换成实际值之后生成word文档
* 需要使用变量的地方word中可以用变量替换 ,格式: ${变量名} 如: ${name}
* name为变量名,在map中key使用变量名,value使用要替换成的字符串
* 如 map.put("name","栾主峰")
* @param maps 变量集合
* @param DocName 模板名称
* @return 重新生成的文档名称包含全路径,返回-1时,文件生成错误
*/
public String createWordByMap(Map<String, String> maps, String DocName) {
String path = this.getClass().getClassLoader().getResource("/")
.getPath()
+ "attach/";//获取基础路径
String docModelPath = path + "word/model/"; //生成模板所在路径
String docPath = path + "word/create/" + getCurrentDateTime("yyyyMMdd")
+ "/";
//+"/"+getCurrentDateTime("yyyyMMddHHmmssSSS");
java.io.File filetemp = new java.io.File(docPath);
if (!filetemp.exists())
filetemp.mkdirs();
String destFile = docPath + getCurrentDateTime("yyyyMMddHHmmssSSS")
+ ".doc";
HWPFDocument document = new word().replaceDoc(docModelPath + DocName
+ ".doc", maps);
if (document != null)
{
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
try {
document.write(ostream);
// 输出word文件
OutputStream outs = new FileOutputStream(destFile);
outs.write(ostream.toByteArray());
outs.close(); } catch (IOException e) {
e.printStackTrace();
destFile = "-1";
}
} else
destFile = "-1";
return destFile;
} /**
*
* @param destFile
* @param fileCon
*/
public void exportDoc(String destFile, String fileCon) {
try {
//doc content
ByteArrayInputStream bais = new ByteArrayInputStream(fileCon
.getBytes());
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream(destFile);
fs.writeFilesystem(ostream);
bais.close();
ostream.close(); } catch (IOException e) {
e.printStackTrace();
}
} /**
* 读取word模板并替换变量
* @param srcPath
* @param map
* @return
*/
public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) {
try {
// 读取word模板
FileInputStream fis = new FileInputStream(new File(srcPath));
HWPFDocument doc = new HWPFDocument(fis);
// 读取word文本内容
Range bodyRange = doc.getRange();
// 替换文本内容
for (Map.Entry<String, String> entry : map.entrySet()) {
bodyRange.replaceText("${" + entry.getKey() + "}", entry
.getValue());
}
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} //得到当前的系统时间
/*
根据输入的格式(String _dtFormat)得到当前时间格式
*/
public String getCurrentDateTime(String _dtFormat) {
String currentdatetime = "";
try {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dtFormat = new SimpleDateFormat(_dtFormat);
currentdatetime = dtFormat.format(date);
} catch (Exception e) {
System.out.println("时间格式不正确");
e.printStackTrace();
}
return currentdatetime;
} }
5.模板中的样式
poi导出word模板项目实例(一个文件)的更多相关文章
- java工具类POI导出word
1.新建一个word,里面填写内容,如: 2.导出wordjava类 /** * POI导出word测试 * @throws Exception */ @RequestMapping(value=&q ...
- 使用java Apache poi 根据word模板生成word报表
项目开发过程中,客户提出一堆导出报表的需求,需要导出word格式,页眉还需要加上客户公司的logo,试了几种方案,最后选择了用 Apache poi 加上自定义标签的方式实现. 目前功能还比较简单,一 ...
- 使用POI导出Word(含表格)的实现方式及操作Word的工具类
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- poi导出word
最近做了个poi导出word的功能 下面是代码: 一个可以参考的例子: package com.lzb.crm.web; import java.io.FileOutputStream; import ...
- POI往word模板中写入数据
转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...
- poi导出word表格详解 超详细了
转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138 一.效果如下 二.js代码 function export_word( ...
- 使用word模板生成pdf文件
使用word模板生成pdf文件 源码:UserWord
- poi导出word时设置兼容性
接上一篇poi导出word http://www.cnblogs.com/xiufengd/p/4708680.html. public static void setAuto(XWPFDocumen ...
- SpringBoot 集成 FreeMarker 导出 Word 模板文件(底部附源码)
思路解说 word 模板文件(doc 或 docx 文件)另存为 xml 文件 将后缀 xml 改成 html:大部分文档会改成 ftl(FreeMarker 的后缀名),因为 word 文件另存为 ...
随机推荐
- 在虚拟机中设置NAT模式实现主机和虚拟机的通信
1.打开虚拟机,对几个节点进行网络连接的设置.虚拟机设置/网络连接,选择“NAT模式” 2.编辑—>虚拟网络编辑器来查看NAT模式中所用到的网段. 从上图可以看出,NAT中的子网IP是192.1 ...
- 「开源」SpringCloud+vue搭建的商城项目
最近在研究SpringCloud,看到一个基于SpringCloud+vue搭建的模拟商城项目.用来辅助学习SpringCloud企业级开发还是很有帮助的.强烈推荐!! 源码地址在最后. spring ...
- pycharm 中切换虚拟环境
在pycharm上创建虚拟环境,网上的资料非常多. 如果pycharm上有多个项目,如何切换每个项目的虚拟环境? cmd 命令进入虚拟环境所在的文件夹(Pycharm在每创建一个新项目时就会创建一个虚 ...
- c# JsonReader读取json字符串
使用JsonReader读Json字符串: string jsonText = @"{""input"" : ""val ...
- Count on a tree II SPOJ - COT2 && bzoj1086 王室联邦 && bzoj2589
https://cn.vjudge.net/problem/SPOJ-COT2 这个是树上莫队模版啊.. 树上莫队有两种,第一种就是括号序莫队 设节点i在括号序中首次出现位置为pl[i] 那么路径(i ...
- centOS+uwsgi+nginx 部署flask项目,问题记录
用flask做的项目想要部署到centOS系统上,填了一些坑,终于成功了,记录一下遇到的问题: 此次部署主要是按照这个博客进行的 https://www.cnblogs.com/Ray-liang/p ...
- web基础笔记
浏览器渲染页面的过程 浏览器渲染页面前需要先构建 DOM 和 CSSOM 树.因此,我们需要确保尽快将 HTML 和 CSS 都提供给浏览器. 参考:https://developers.google ...
- 关于Mybatis的一点小记录(parameterType)
1.Mybatis的parameterType有两个比较常用的,一个是类的对象,还有一个就是Map,然后取值的方法也很简单: 基本数据类型:#{参数} 获取参数中的值 复杂数据类型:#{属性名} ,m ...
- Railroad UVALive - 4888 记忆化搜索
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- vue 引用其他组件
1.在components 目录下新建Test.vue 文件 <template> <div class="test"> <h1>{{ msg ...