81,092
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>jsp页面导入到word</title>
</head>
<script language="javascript">
function tableToWord() {
try
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("", 0, 1);
var oRange = oDC.Range(0, 1);
var sel = document.body.createTextRange();
sel.moveToElementText(theObjTable);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
}
catch (e) {
alert("您的电脑没有安装Microsoft Word软件!")
return false;
}
}
</script>
<body>
<div id="theObjTable" align="center">
<table>
<tr>
<td>
<font><strong>这是表头 </strong> </font>
</td>
</tr>
</table>
<table border="1" bordercolor="black">
<tr>
<td>
a
</td>
<td>
b
</td>
<td>
a
</td>
<td>
b
</td>
</tr>
<tr>
<td>
c
</td>
<td>
d
</td>
<td>
c
</td>
<td>
d
</td>
</tr>
</table>
<div id="img" align="center">
<img height="50%" width="50%" src='Chrysanthemum.jpg'>
</div>
</div>
<div align="center">
<input type="button" value="保存到word" onclick="tableToWord();">
</div>
</body>
</html>
String html = request.getParameter("html");
if(html != null){
byte[] bs = html.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bs);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream("D:\\a.doc");
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}
定义一个form表单,里面有个html隐藏框
提交时document.documentElement.innerHTML把这个值赋值给那个隐藏框,然后提交后台