<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>WEB頁面導(dǎo)出為EXCEL文檔的方法</title>
</head>
<body>
<BR>
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">
<TR style="text-align : center;">
<TD>單元格A</TD>
<TD>單元格A</TD>
<TD>單元格A</TD>
<TD>單元格A</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">單元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">單元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">單元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">單元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">單元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">單元格合并行A</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">
<TR style="text-align : center;">
<TD>單元格B</TD>
<TD>單元格B</TD>
<TD>單元格B</TD>
<TD>單元格B</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;">單元格合并行B</TD>
</TR>
</TABLE>
<br>
<br>
<br>
<input type="button" onClick="javascript:AllAreaWord();" value="導(dǎo)出頁面指定區(qū)域內(nèi)容到Word">
<input type="button" onClick="javascript:CellAreaExcel();" value="導(dǎo)出頁面指定區(qū)域內(nèi)容到Excel">
<input type="button" onClick="javascript:AllAreaExcel2();" value="導(dǎo)出表單單元格內(nèi)容到Excel">
<SCRIPT LANGUAGE="javascript">
//----------------------------------------------------------------
//------------ 隨即函數(shù)
function genRandNumber(startNum,endNum){
var randomNumber;
randomNumber = Math.round(Math.random()*(endNum-startNum))+startNum;
return randomNumber;
}
//----------------------------------------
var now= new Date();
var year=now.getYear();
var month=now.getMonth()+1;
var day=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
var oHtmlNow = year+''+month+''+day+''+hour+''+minute+''+second+''+genRandNumber(999999,111111);
//----------------------------------------------------------------
function AllAreaExcel() {
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel = document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true
}
function AllAreaExcel2() {
window.clipboardData.setData("Text", document.all('PrintA').outerHTML);
try {
var ExApp = new ActiveXObject("Excel.Application");
var ExWBk = ExApp.workbooks.add();
var ExWSh = ExWBk.worksheets(1);
ExApp.DisplayAlerts = false;
ExApp.visible = true;
} catch(e) {
alert("您的電腦沒有安裝Microsoft Excel軟件!");return false;
}
ExWBk.worksheets(1).Paste;
ExApp.Visible = true;
//設(shè)置excel可見屬性
try{
var fname = ExApp.Application.GetSaveAsFilename(oHtmlNow+".xls", "Excel Spreadsheets (*.xls), *.xls");
if(fname){
ExWBk.SaveAs(fname);
}
}catch(e){
print("Nested catch caught " + e);
}finally{
ExWBk.Close(savechanges=false);
ExApp.Quit();
ExApp=null;
//結(jié)束excel進(jìn)程,退出完成
alert('已經(jīng)成功導(dǎo)入到Excel:'+oHtmlNow+'.xls');
idTmr = window.setInterval("Cleanup();",1);
}
}
function CellAreaExcel() {
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = PrintA.rows.length;
for (i = 0; i < Lenr; i++) {
var Lenc = PrintA.rows(i).cells.length;
for (j = 0; j < Lenc; j++) {
oSheet.Cells(i + 1, j + 1).value = PrintA.rows(i).cells(j).innerText
}
}
oXL.Visible = true;
}
function AllAreaWord() {
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(PrintA);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true
}
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
//-----------------------------------------------------------------
</script>
</body>
</html>
在線導(dǎo)出Excel,錯(cuò)誤信息:Automation服務(wù)器不能創(chuàng)建對象解決方案
問題描述:
在部分以IE為內(nèi)核的瀏覽器下,按鈕“導(dǎo)出為Excel”不起作用,錯(cuò)誤信息為Automation服務(wù)器不能創(chuàng)建對象;
官方(微軟)解釋:
http://support.microsoft.com/kb/828550/zh-cn
解決方案:
- 單擊 IE 菜單:工具;
- 選擇 Internet選項(xiàng);
- 切換為 安全選項(xiàng)卡;
- 單擊自定義級別按鈕;
- 更改 對未標(biāo)記為可安全執(zhí)行腳本的ActiveX控件初始化并執(zhí)行 為 提示或者 啟用 ;
- 單擊確定,重啟瀏覽器即可。
該文章在 2010/8/18 1:09:28 編輯過