There are great limitations to exporting excel using JS method to call page tables:
1. I have tried several browsers at present, only IE supports it.
2. Click Tools--Security--Customization Level---ActiveX related options are enabled
Below is the html code
The code copy is as follows:
<%@ 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 = "";
// Function function: Copy tables into Excel
// Parameters: tableID table id
function CellToTable(tableID)
{
var tid=document.getElementById(tableID);
// Load ActiveX control and get Excel handle
var exApp = new ActiveXObject("Excel.Application");
// Create an Excel file
var owb = exApp.WorkBooks.add();
// Get sheet1 handle CA
var exSheet = exApp.ActiveWorkBook.WorkSheets(1);
// Set the name of sheet1
exSheet.name="Demonstrate copy table into Excel";
// The form specified by copy
var sel=document.body.createTextRange();
sel.moveToElementText(tid);
sel.select();
sel.execCommand("Copy");
exSheet.Paste();// Paste in sheet
//exApp.save();// The save dialog box pops up to save the Excel file
exApp.Visible = false;
var fname = exApp.Application.GetSaveAsFilename("save.xls", "Excel Spreadsheets (*.xls), *.xls");
owb.SaveAs(fname);
exApp.Quit();// Exit Excel instance
exApp = null;
// Call Cleanup() for garbage collection
idTmr = window.setInterval("Cleanup();",10);
}
// Function function: Kill Excel process
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
</script>
</head>
<body>
<table cellpacing="0" cellpadding="0" id="tableToExcel" name="tableName">
<tr bgcolor="#99CCCC">
<td rowspan="4" bgcolor="#33FF99">Changchun in Jilin</td>
<td rowspan="4" bgcolor="#33FF99">Shenyang in Liaoning</td>
<td rowspan="4" bgcolor="#33FF99">Harbin in Heilongjiang</td>
<td rowspan="4" bgcolor="#33FF99">Beijing</td>
<td bgcolor="#66CC99">Haidian</td>
</tr>
<tr bgcolor="#99CCCC">
<td bgcolor="#66CC99">Jilin-Changchun</td>
</tr>
<tr bgcolor="#99CCCC">
<td bgcolor="#66CC99">Liaoning-Shenyang</td>
</tr>
<tr bgcolor="#99CCCC">
<td bgcolor="#66CC99">Heilongjiang-Harbin</td>
</tr>
<tr bgcolor="#99CCCC">
<td colspan="5">Demonstrate the process of javascript's copy of tables (recommended) </td>
</tr>
<tr bgcolor="#99CCCC">
<td colspan="5"><label>
<div align="center">
<input name="textfield" type="text" value="Single-line text box control" size="30"/>
</div>
</label></td>
</tr>
</table>
<br>
<input type="submit" name="Submit3" value="Click to copy the table into Excel" onclick= "CellToTable('tableToExcel')" />
</body>
</html>