<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD" >
<!--three ways to export excle the "Initialize and script run for ActiveX controls that are not marked as safe"--> to be prompted or enabled by IE browser
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-type" content= "text/html"; charset=gb2312 ">
Methods for exporting <title>web pages to Excel documents </title>
</head>
<body>
<table id= "Tableexcel" width= "100%" border= "1" cellspacing= "0" cellpadding= "0" >
<tr>
<td colspan= "5" align= "Center" >
Web page Export as an Excel document
</td>
</tr>
<tr>
<td>
Column Heading 1
</td>
<td>
column Heading 2
</td>
<td>
column Heading 3
</td>
<td>
column Heading 4
</td>
<td>
column Heading 5
</td>
</tr>
<tr>
<td>
AAA
</td>
<td>
</td>
<td>
CCC
</td>
<td>
DDD
</td>
<td>
Eee
</td>
</tr>
<tr>
<td>
AAA
</td>
<td>
</td>
<td>
CCC
</td>
<td>
DDD
</td>
<td>
EEE
</td>
</tr>
<tr>
<td>
FFF
</td>
<td>
GGG
</td>
<td>
HHH
</td>
<td>
III
</td>
<td>
JJJ
</td>
</tr>
</table>
<input type= "button" onclick= "Javascript:method1 (' Tableexcel ');" value= "first method import to Excel >
<input type= "button" onclick= javascript:method2 (' Tableexcel '); "Value=" The second method is imported to Excel >
<input type= "button" onclick= "Javascript:getxlsfromtbl" (' Tableexcel ', null); "Value=" The third method is imported to Excel >
<script language= "JavaScript" >
function Method1 (tableid) {//Full table Copy to Excel
var curtbl = document.getElementById (TableID);
var oXL = new ActiveXObject ("Excel.Application"); Create an Ax object Excel
var owb = OXL.Workbooks.Add (); Get Workbook Object
var osheet = Owb.activesheet; Activate current sheet
var sel = Document.body.createTextRange ();
Sel.movetoelementtext (CURTBL); Move the contents of the table to TextRange
Sel.select (); Full selection of TextRange contents
Sel.execcommand ("Copy"); Copy content in TextRange
Osheet.paste (); Paste into active Excel
oXL.Visible = true; Set Excel Visible Properties
}
function Method2 (tableid)/read each cell in the table to Excel
{
var curtbl = document.getElementById (TableID);
var oXL = new ActiveXObject ("Excel.Application"); Create an Ax object Excel
var owb = OXL.Workbooks.Add (); Get Workbook Object
var osheet = Owb.activesheet; Activate current sheet
var lenr = curTbl.rows.length; Get the number of rows in the table
for (i = 0; i < lenr; i++) {
var Lenc = curtbl.rows (i). Cells.length; Get the number of columns per row
for (j = 0; J < Lenc; J + +) {
Osheet.cells (i + 1, j + 1). Value = Curtbl.rows (i). Cells (j). innertext; assigning values
}
}
oXL.Visible = true; Set Excel Visible Properties
}
function Getxlsfromtbl (intblid, Inwindow) {
try {
var allstr = "";
var curstr = "";
if (intblid!= null && intblid!= "" && intblid!= "null") {
Curstr = Gettbldata (Intblid, Inwindow);
}
if (curstr!= null) {
Allstr + = Curstr;
}
else {
Alert ("The table you want to export does not exist!") ");
Return
}
var fileName = Getexcelfilename ();
Dofileexport (FileName, ALLSTR);
}
catch (e) {
Alert ("Export exception occurred:" + e.name + "->" + e.description + "!");
}
}
function Gettbldata (INTBL, Inwindow) {
var rows = 0;
var tbldocument = document;
if (!! Inwindow && Inwindow!= "") {
if (!document.all (Inwindow)) {
return null;
}
else {
Tbldocument = eval (inwindow). Document;
}
}
var curtbl = Tbldocument.getelementbyid (INTBL);
var outstr = "";
if (curtbl!= null) {
for (var j = 0; J < CurTbl.rows.length; J + +) {
for (var i = 0; i < curtbl.rows[j].cells.length; i++) {
if (i = = 0 && rows > 0) {
Outstr + = "T";
rows = 1;
}
Outstr + = Curtbl.rows[j].cells[i].innertext + "/t";
if (Curtbl.rows[j].cells[i].colspan > 1) {
for (var k = 0; k < curtbl.rows[j].cells[i].colspan-1; k++) {
Outstr + = "T";
}
}
if (i = = 0) {
if (rows = = 0 && curtbl.rows[j].cells[i].rowspan > 1) {
rows = curtbl.rows[j].cells[i].rowspan-1;
}
}
}
Outstr + = "/r/n";
}
}
else {
OUTSTR = null;
Alert (intbl + "does not exist!");
}
return outstr;
}
function Getexcelfilename () {
var d = new Date ();
var curyear = D.getyear ();
var curmonth = "" + (D.getmonth () + 1);
var curdate = "" + d.getdate ();
var curhour = "" + d.gethours ();
var Curminute = "" + d.getminutes ();
var Cursecond = "" + d.getseconds ();
if (curmonth.length = = 1) {
Curmonth = "0" + curmonth;
}
if (curdate.length = = 1) {
Curdate = "0" + curdate;
}
if (curhour.length = = 1) {
Curhour = "0" + curhour;
}
if (curminute.length = = 1) {
Curminute = "0" + curminute;
}
if (cursecond.length = = 1) {
Cursecond = "0" + cursecond;
}
var fileName = "Leo_zhang" + "_" + curyear + curmonth + curdate + "_"
+ curhour + curminute + cursecond + ". csv";
return fileName;
}
function Dofileexport (inname, inStr) {
var xlswin = null;
if (!! document.all ("Glbhidefrm")) {
Xlswin = glbhidefrm;
}
else {
var width = 6;
var height = 4;
var Openpara = "left=" + (WINDOW.SCREEN.WIDTH/2-WIDTH/2)
+ ", top=" + (WINDOW.SCREEN.HEIGHT/2-HEIGHT/2)
+ ", scrollbars=no,width=" + width + ", height=" + height;
Xlswin = window.open ("", "_blank", Openpara);
}
XlsWin.document.write (INSTR);
XlsWin.document.close ();
XlsWin.document.execCommand (' Saveas ', true, inname);
Xlswin.close ();
}
</script>
</body>