<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<script language="javascript">
var idTmr = "";
// 函数功能:复制表格到Excel中
// 参 数:tableID 表的id
function CellToTable(tableID)
{
var tid=document.getElementById(tableID);
// 加载ActiveX控件,获取Excel句柄
var exApp = new ActiveXObject("Excel.Application");
// 创建一个Excel文件
var owb = exApp.WorkBooks.add();
// 获取sheet1句柄CA
var exSheet = exApp.ActiveWorkBook.WorkSheets(1);
// 设置sheet1的名称
exSheet.name="演示复制表格到Excel中";
// copy指定的表格
var sel=document.body.createTextRange();
sel.moveToElementText(tid);
sel.select();
sel.execCommand("Copy");
exSheet.Paste();// 粘贴到sheet中
//exApp.save();// 弹出保存对话框,保存Excel文件
exApp.Visible = false;
var fname = exApp.Application.GetSaveAsFilename("save.xls", "Excel Spreadsheets (*.xls), *.xls");
owb.SaveAs(fname);
exApp.Quit();// 退出Excel实例
exApp = null;
// 调用Cleanup()进行垃圾回收
idTmr = window.setInterval("Cleanup();",10);
}
// 函数功能:杀掉Excel进程
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
</script>
</head>
<body>
<table width="307" border="1" cellspacing="0" cellpadding="0" id="tableToExcel" name="tableName">
<tr bgcolor="#99CCCC">
<td width="66" rowspan="4" bgcolor="#33FF99">吉林的长春</td>
<td width="67" rowspan="4" bgcolor="#33FF99">辽宁的沈阳</td>
<td width="94" rowspan="4" bgcolor="#33FF99">黑龙江的哈尔滨</td>
<td width="30" rowspan="4" bgcolor="#33FF99">北京</td>
<td width="38" bgcolor="#66CC99">海淀</td>
</tr>
<tr bgcolor="#99CCCC">
<td bgcolor="#66CC99">吉林-长春</td>
</tr>
<tr bgcolor="#99CCCC">
<td bgcolor="#66CC99">辽宁-沈阳</td>
</tr>
<tr bgcolor="#99CCCC">
<td bgcolor="#66CC99">黑龙江-哈尔滨</td>
</tr>
<tr bgcolor="#99CCCC">
<td colspan="5">演示javascrīpt对表格copy的处理过程(推荐) </td>
</tr>
<tr bgcolor="#99CCCC">
<td colspan="5"><label>
<p align="center">
<input name="textfield" type="text" value="单行文本框控件" size="30"/>
</p>
</label></td>
</tr>
</table>
<br>
<input type="submit" name="Submit3" value="点击复制表格到Excel中" onclick= "CellToTable('tableToExcel')" />
</body>
</html>