<---------------------- htmltoexcel.jsp ------------>
/**
* @param arg[0]:报表的标题;arg[1]:打印table的id;arg[2]:隐藏列时需要,值为要显示列的数组
*/
function htmlToExcel(){
var args=htmlToExcel.arguments;
var tables = document.getElementsByTagName('TABLE');
var printStr = "<head><style>.form_font{text-align:center;font-size:9.0pt;}.form_td2{text-align:center;font-size:9.0pt;color: #297949;}"
printStr =printStr +".form_td7{text-align:center;font-size:9.0pt;}.form_td10{text-align:right;font-size:9.0pt;color: #297949;}.form_td11{text-align:right;font-size:9.0pt;color: #0000CD}</style></head>";
printStr = printStr + "<table>";
var yx = args[2];
if(!yx){
for(var i = 1;i < args.length;i++){
for(var j = 0;j < tables.length;j++){
if(tables[j].id == args[i]){
printStr = printStr + tables[j].innerHTML;
}
}
}
}
else{
var cols = document.getElementsByTagName('col');
var ta = getObject(args[1]);
var arr = new Array();
arr = convertStrToArr(yx, ',', false);
if(cols.length < arr.length){
for( i = 1;i < args.length;i++){
for( j = 0;j < tables.length;j++){
if(tables[j].id == args[i]){
printStr = printStr + tables[j].innerHTML;
}
}
}
}
else{
for( j=0;j<ta.rows.length;j++){
n=0;
printStr = printStr + '<tr>';
while(arr[n]){
k=arr[n]-1;
printStr = printStr + ta.rows[j].cells[k].outerHTML;
n = n+1;
}
printStr = printStr + '</tr>';
}
}
}
printStr = printStr + "</table>";
var pattern = /(width=)+("?)(\d{1,3})%("?)/;
var pattern1 = /(width=)+("?)(\d{1,3})%("?)/g;
var pattern2 = /(\w=)+"?(\d{1,3})%"?/;
if(pattern.test(printStr)){ //含有匹配此模式的字符串
var all_result = printStr.match(pattern1); //所有匹配此模式的字符串
for(var i = 0;i < all_result.length;i++){
var _percent = all_result[i].match(pattern2);//取_percent[2],百分比值
printStr = printStr.replace(pattern,"$1$2"+ Math.round(parseFloat(_percent[2])/100 * 1024) +"$4");
}
}
if (typeof(EXPORT_OBJECT)!="object"){
document.body.insertAdjacentHTML("afterBegin","<OBJECT style='display:none' classid=clsid:0002E510-0000-0000-C000-000000000046 id=EXPORT_OBJECT></Object>");
}
with (EXPORT_OBJECT){
DataType = "HTMLData";
HTMLData = printStr;
try{
ActiveSheet.Export(args[0], 0);
alert('汇出完毕');
}
catch (e){
if(e.message!=''){
alert('汇出Excel表失败,请确定已安装Excel2000的Office工具->Office Web Components,并且没打开同名xls文件');
}else if(e.message==''){
//alert('用户取消操作!');
}
}
}
}
<------------------- END ---------------------------------->
<------------------- html ---------------------------------->
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<SCRIPT language=JavaScript src="print.js"></SCRIPT>
<body>
<button class="form_button" onClick="htmlToExcel('C:\\出口.xls','t')">导出</button>
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="t">
<tr>
<td>sdfsd f
</td>
<td>sdfsdfsdf
</td>
</tr>
</body>
</html>
<------------------- END ---------------------------------->
来源:https://www.cnblogs.com/bisonjob/archive/2005/12/08/292921.html