2010.03.03——html导出为word文档,添加表格后
我发现,那个方法,好像是把页面上的东西都放到word里面了,如果 我仿照word在页面上写个表格,它会把页面的所有元素都放到word里面,比如说input,select,radio,checkbox等等,但是我只是想要用户最后的结果,所以,我设想,当用户输入完后,单击确定,我执行把input等等元素都变为文本对象,这样在导入到word就容易一些了,所以,我用jquery来操纵dom元素
如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<title>WEB页面导出为EXCEL文档的方法
</title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<SCRIPT LANGUAGE="javascript">
//指定页面区域内容导入Excel
function AllAreaExcel(tid)
{
var excel = document.getElementById(tid);
var oXL = null;
try{
oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert('ERROR!! 原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件');
idTmr = window.setInterval("Cleanup();",1);
return;
}
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(excel);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
idTmr = window.setInterval("Cleanup();",1);
}
//指定页面区域“单元格”内容导入Excel,只有文本 不带背景
function CellAreaExcel(tid)
{
var excel = document.getElementById(tid);
var oXL = null;
try{
oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert('ERROR!! 原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件');
idTmr = window.setInterval("Cleanup();",1);
return;
}
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = excel.rows.length;
for (i=0;i<Lenr;i++)
{
var Lenc = excel.rows(i).cells.length;
for (j=0;j<Lenc;j++)
{
oSheet.Cells(i+1,j+1).value = excel.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
idTmr = window.setInterval("Cleanup();",1);
}
//指定页面区域内容导入Word
function AllAreaWord(tid)
{
var word = document.getElementById(tid);
var oWD = null;
try{
oWD = new ActiveXObject('Word.Application');
}catch(e){
alert('ERROR!!原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件');
idTmr = window.setInterval("Cleanup();",1);
return;
}
var oDC = oWD.Documents.Add("",0,1);
var orange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(word);
sel.select();
sel.execCommand("Copy");
orange.Paste();
oWD.Application.Visible = true;
//oDC.saveAs(false,"ba.doc");
idTmr = window.setInterval("Cleanup();",1);
/*
var wordApp = null;
try{
wordApp = new ActiveXObject('Word.Application'); }catch(e)
{
alert(e+', 原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件');
return;
}
var oDC = wordApp.Documents.Add("",0,1);
wordApp.Application.Visible = false;
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(word);
sel.select();
sel.execCommand("Copy");
wordApp.Application.Visible = false;
oRange.Paste();
wordApp.visible = false;
wordApp.ActiveDocument.printout();
wordApp.Application.ActiveDocument.SaveAs("c:/tempSample.doc",true);
wordApp.ActiveDocument.close();
ActiveWindow.Close();
wordApp=null;
wordApp.Quit();
idTmr = window.setInterval("Cleanup();",1);
window.close();
*/
}
//解决上面几个方法execl word对象无法关闭的问题
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
//修改dom树
$(function(){
$("#btn").click(createDom);
});
function createDom(){
var str = $("#cid").val();
$("#cid").prev().remove();
$("#cid").remove();
$("#divid").text($("#divid").text()+str);
str = $("#text1").val();
$("#div1 span").remove();
$("#text1").remove();
$("#td1").text(str);
str = $("#text2").val();
$("#div2 span").remove();
$("#text2").remove();
$("#td2").text(str);
str = $("#text3").val();
$("#div3 span").remove();
$("#text3").remove();
$("#td3").text(str);
str = $("#text4").val();
$("#div4 span").remove();
$("#text4").remove();
$("#td4").text(str);
str = $("#select").val();
$("#div5 span").remove();
$("#select").remove();
$("#td5").text(str);
str = "" ;
$("input[name='checkbox']:checked").each(function(){
str += $(this).val();
str += "<br />";
});
$("#td6 input").remove();
$("#td6").text("");
$("#td6").append(str);
str = $("input[name='radio']:checked").val();
$("#div7 span").remove();
$("#td7 input").remove();
$("#td7").next().remove();
$("#td7").attr("colspan",2).text(str);
$("#btn").attr("disabled","disabled");
}
</SCRIPT>
</HEAD>
<body style="text-align: center;">
<div align="center" id="PrintA" style="width: 400px;height: 500px;">
<h2>工程信息表</h2><br />
<div align="right" id="divid">工程编号:<span style="color:#FF0000;"> *</span><input type="text" id="cid" size="10"></div>
<table width="417" border="1" cellspacing="0" bordercolor="#666666">
<tr>
<td width="32" rowspan="4"><div align="center">工<br />
程<br />
基<br />
本<br />
情<br />
况</div></td>
<td width="124" height="34"><div align="center" id="div1">工程名称 <span style="color:#FF0000;">*</span>
</div></td>
<td colspan="2" id="td1" align="center">
<input name="textfield" type="text" id="text1" size="35" /> </td>
</tr>
<tr>
<td height="36"><div align="center" id="div2">工程地址 <span style="color:#FF0000;">*</span></div></td>
<td colspan="2" align="center" id="td2">
<input name="textfield2" type="text" id="text2" size="35" /> </td>
</tr>
<tr>
<td height="29"><div align="center" id="div3">建设单位名称<span style="color:#FF0000;"> *</span></div></td>
<td colspan="2" align="center" id="td3">
<input name="textfield3" type="text" id="text3" size="35" /> </td>
</tr>
<tr>
<td height="31"><div align="center" id="div4">施工时间 <span style="color:#FF0000;">*</span></div></td>
<td colspan="2" align="center" id="td4">
<input name="textfield4" type="text" id="text4" size="35" /> </td>
</tr>
<tr>
<td rowspan="3"><div align="center">工<br />
程<br />
其<br />
他<br />
情<br />
况</div></td>
<td height="34"><div align="center" id="div5">建设单位性质 <span style="color:#FF0000;">*</span></div></td>
<td colspan="2" align="center" id="td5">
<select name="select" size="1" id="select" style="width:245px">
<option>个人</option>
<option>公司</option>
<option>企业</option>
</select> </td>
</tr>
<tr>
<td><div align="center">初审情况</div></td>
<td colspan="2" align="center" id="td6">
材料完整
<input type="checkbox" name="checkbox" id="checkbox1" value="材料完整"/>
<br />
手续合理
<input type="checkbox" name="checkbox" id="checkbox2" value="手续合理"/>
<br />
人员齐备
<input type="checkbox" name="checkbox" id="checkbox3" value="人员齐备"/>
</td>
</tr>
<tr align="center">
<td><div align="center" id="div7">初审意见 <span style="color:#FF0000;">*</span></div></td>
<td width="106" id="td7">
<input type="radio" name="radio" id="radio1" value="合格"/>
合格 </td>
<td width="137">
<input type="radio" name="radio" id="radio2" value="不合格"/>
不合格</td>
</tr>
</table>
<input type="button" value="确定" id="btn" >
</div>
<input type="button" onclick="javascript:AllAreaWord('PrintA');" value="导出页面指定区域内容到Word">
<input type="button" onclick="javascript:AllAreaExcel('PrintA');" value="导出页面指定区域内容到Excel">
<input type="button" onclick="javascript:CellAreaExcel('PrintA');" value="导出表单单元格内容到Excel">
</body></html>