脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-jqurey异步导出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>
<p id="BTns" class="form-btns">
<INPUT value="导出word文档" type="button" class="btn" onclick="exportWord();">
</p>
<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>
<p id="btns" class="form-btns">
<INPUT value="导出word文档" type="button" class="btn" onclick="exportWord();">
</p>
2.导出用异步的方法
[htML]
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;
}
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接收值,并传递到模板
[html]
<%@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);
%>
<%@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 的方法
[java]
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;
}
}
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;
}
}
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-jqurey异步导出word全部内容,希望文章能够帮你解决javascript代码实例教程-jqurey异步导出word所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。