此方法最开始的想法是要将datagrid的数据连格式一起导入到excel
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<script>
function ClipBoard()
{
document.getElementById("holdtext").innerText= copytext.innerHTML;
Copied = document.getElementById("holdtext").createTextRange();
Copied.execCommand("Copy");
}
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
oSheet.Cells(2,1).select();
oSheet.Paste;
oXL.Visible = true;
oXL.UserControl = true;
}
</script>
</head>
<body>
<form>
<span ID="copytext">
<table border="1" width="100%" id="table1">
<tr>
<td><font color="#FF00FF">qw</font></td>
<td><font color="#FF00FF">w</font></td>
<td>e3</td>
</tr>
<tr>
<td>a</td>
<td>a</td>
<td>d</td>
</tr>
</table>
</span>
<TEXTAREA ID="holdtext" STYLE="display:none;">
</TEXTAREA>
<input type=button onclick ="ClipBoard()" value="copy"><input type=button onclick ="AutomateExcel()" value="Export"></form>
</body>
</html>
下面是从网上找到的调用clipboard的示例
//Copytext to clipboard- by Gennero (patrice.gennero@voila.fr)
//Submited to DynamicDrive.com
//Visit http://www.dynamicdrive.com for this script
bBool=false
var copiedtext=""
var tempstore=""
function initiatecopy() {
bBool=true;
}
function copyit() {
if (bBool) {
tempstore=copiedtext
document.execCommand("Copy")
copiedtext=window.clipboardData.getData("Text");
if (tempstore!=copiedtext) {
alert(copiedtext);
}
bBool=false;
}
}
document.onselectionchange = initiatecopy
document.onmouseup = copyit