87,901
社区成员
发帖
与我相关
我的任务
分享
<input type="button" value="导出Excel" onclick="method();" />
<script type="text/javascript">
function method() {//整个表格拷贝到EXCEL中
//检索浏览器
if(navigator.userAgent.indexOf("MSIE")<0){
alert('请用ie浏览器进行表格导出');
return ;
}
var tableid="excel";
var curTbl = document.getElementById(tableid);
var oXL = null;
try {
oXL = GetObject("", "Excel.Application");
}
catch (E) {
try {
oXL = new ActiveXObject("Excel.Application");
}
catch (E2) {
alert("请确认:\n1.Microsoft Excel已被安装.\n2.工具 => Internet 选项=> 安全 => 设置 \"启用不安全的 ActiveX\"");
return;
}
}
//创建AX对象excel
var oWB = oXL.Workbooks.Add();
//获取workbook对象
var oSheet = oWB.ActiveSheet;
//在此进行样式控制
oSheet.Rows(1+":"+35).RowHeight =25;//定义行高
oSheet.Rows(36+":"+37).RowHeight =15;
//定义列宽
oSheet.Columns('A:K').ColumnWidth = 10;
oSheet.Rows(1).HorizontalAlignment=3;
var sel = document.body.createTextRange(); //激活当前sheet
sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中
sel.select(); //全选TextRange中内容
sel.execCommand("Copy"); //复制TextRange中内容
oSheet.Paste(); //粘贴到活动的EXCEL中
oXL.Visible = true; //设置excel可见属性
oXL.document.execcommand('saveas', true, "test");
oSheet.Application.Quit(); //结束当前进程
window.opener=null;
//window.close();//关闭当前窗口
}
</script>