function
PrintTableToExcelEx(objTab)
... {
try
...{
var xls = new ActiveXObject( "Excel.Application" );
}
catch(e)
...{
alert( "您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法!");
return false;
}
xls.visible = true;
var xlBook = xls.Workbooks.Add;
var xlsheet = xlBook.Worksheets(1);
var CurX = 1;
var CurY = 1;
for (var i = 0; i < objTab.rows.length; i++)
...{
for (var j = 0; j < objTab.rows[i].cells.length; j++)
...{
var cell = objTab.rows[i].cells[j];
var v = cell.outerText;
if ( cell.children != null && cell.children.length != 0 )
...{
v = "";
for ( var k = 0; k < cell.children.length; k++ )
...{
var cc = cell.children[k];
switch (cc.type)
...{
case "select-one":
for ( var l = 0; l < cc.options.length; l++ )
if ( cc.options[l].selected ) v += cc.options[l].text;
break;
case "text":
v += cc.value;
break;
case "checkbox":
v += cc.checked ? "是" : "否";
break;
case undefined:
v += cell.outerText;
break;
case "hidden":
break;
default:
//v += cc.id + "/";
//v += cc.tagname + "/";
//v += cc.type + "/"
v += cc.value;
break;
}
}
}
while ( xlsheet.Cells(CurX,CurY).MergeCells ) CurY++;
xlsheet.Cells(CurX, CurY).HorizontalAlignment = 2;
var align = cell.getAttribute("align");
switch ( align )
...{
case "left":
xlsheet.Cells(CurX, CurY).HorizontalAlignment = 2;//XlHalign.xlHalignLeft;
break;
case "center":
xlsheet.Cells(CurX, CurY).HorizontalAlignment = 3;;//XlHalign.xlHalignCenter;
break;
case "right":
xlsheet.Cells(CurX, CurY).HorizontalAlignment = 4;//XlHalign.xlHalignRight;
break;
}
xlsheet.Cells(CurX, CurY).WrapText = true;
xlsheet.Cells(CurX, CurY).VerticalAlignment = 2;
xlsheet.Cells(CurX, CurY).Value = v;
xlsheet.Cells(CurX, CurY).Borders.LineStyle = 1;
var rowSpan = cell.getAttribute("RowSpan");
var colSpan = cell.getAttribute("ColSpan");
if ( rowSpan >= 2 || colSpan >= 2 )
...{
var R = xls.Range(xls.Cells(CurX, CurY), xls.Cells(CurX + rowSpan - 1, CurY + colSpan - 1));
R.MergeCells = true;
R.Borders.LineStyle = 1;
}
CurY++;
}
CurX++;
CurY = 1;
}
xlsheet.Columns.AutoFit; //自动适应大小
return;
}
... {
try
...{
var xls = new ActiveXObject( "Excel.Application" );
}
catch(e)
...{
alert( "您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法!");
return false;
}
xls.visible = true;
var xlBook = xls.Workbooks.Add;
var xlsheet = xlBook.Worksheets(1);
var CurX = 1;
var CurY = 1;
for (var i = 0; i < objTab.rows.length; i++)
...{
for (var j = 0; j < objTab.rows[i].cells.length; j++)
...{
var cell = objTab.rows[i].cells[j];
var v = cell.outerText;
if ( cell.children != null && cell.children.length != 0 )
...{
v = "";
for ( var k = 0; k < cell.children.length; k++ )
...{
var cc = cell.children[k];
switch (cc.type)
...{
case "select-one":
for ( var l = 0; l < cc.options.length; l++ )
if ( cc.options[l].selected ) v += cc.options[l].text;
break;
case "text":
v += cc.value;
break;
case "checkbox":
v += cc.checked ? "是" : "否";
break;
case undefined:
v += cell.outerText;
break;
case "hidden":
break;
default:
//v += cc.id + "/";
//v += cc.tagname + "/";
//v += cc.type + "/"
v += cc.value;
break;
}
}
}
while ( xlsheet.Cells(CurX,CurY).MergeCells ) CurY++;
xlsheet.Cells(CurX, CurY).HorizontalAlignment = 2;
var align = cell.getAttribute("align");
switch ( align )
...{
case "left":
xlsheet.Cells(CurX, CurY).HorizontalAlignment = 2;//XlHalign.xlHalignLeft;
break;
case "center":
xlsheet.Cells(CurX, CurY).HorizontalAlignment = 3;;//XlHalign.xlHalignCenter;
break;
case "right":
xlsheet.Cells(CurX, CurY).HorizontalAlignment = 4;//XlHalign.xlHalignRight;
break;
}
xlsheet.Cells(CurX, CurY).WrapText = true;
xlsheet.Cells(CurX, CurY).VerticalAlignment = 2;
xlsheet.Cells(CurX, CurY).Value = v;
xlsheet.Cells(CurX, CurY).Borders.LineStyle = 1;
var rowSpan = cell.getAttribute("RowSpan");
var colSpan = cell.getAttribute("ColSpan");
if ( rowSpan >= 2 || colSpan >= 2 )
...{
var R = xls.Range(xls.Cells(CurX, CurY), xls.Cells(CurX + rowSpan - 1, CurY + colSpan - 1));
R.MergeCells = true;
R.Borders.LineStyle = 1;
}
CurY++;
}
CurX++;
CurY = 1;
}
xlsheet.Columns.AutoFit; //自动适应大小
return;
}
调用:
<
INPUT style
=
"
WIDTH: 107px
"
onclick
=
"
javascript:PrintTableToExcelEx(Table1)
"
type
=
"
button
"
value = " 导 出 " class = " input " >
value = " 导 出 " class = " input " >