js代碼
<script>
var idTmr = "";
function killExcelProcess(appExcel_){
appExcel_.Quit();
appExcel_ = null;
idTmr = window.setInterval("Cleanup();",1);
}
//導出到excel,參數tableid為JSP頁面需要導出的table的id,需要更改IE的安全設置,ActiveX都打開,如果還是不能使用,再服務端運行“regsvr32 scrrun.dll”
function exportToExcel(tableid,notitleandsearch){
try {
clipboardData.setData('Text','');
var appExcel = new ActiveXObject("Excel.Application");
killExcelProcess(appExcel);
appExcel.workbooks.add;
//標題
if (notitleandsearch==null||notitleandsearch==false){
var elTable = document.getElementById('div_title');
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText(elTable);
oRangeRef.execCommand( "Copy" );
appExcel.ActiveSheet.Cells(1,3).select();
appExcel.ActiveSheet.Paste();
clipboardData.setData('Text','');
appExcel.ActiveSheet.Cells(2,1).select();
appExcel.ActiveSheet.Paste();
}
var elTable1 = document.getElementById(tableid);
var oRangeRef1 = document.body.createTextRange();
oRangeRef1.moveToElementText(elTable1);
oRangeRef1.execCommand( "Copy" );
appExcel.WorkSheets(1).Activate;
if (notitleandsearch==null||notitleandsearch==false){
appExcel.ActiveSheet.Cells(3,1).select();
}else{
appExcel.ActiveSheet.Cells(1,1).select();
}
appExcel.WorkSheets(1).Activate;
appExcel.ActiveSheet.Paste();
appExcel.Visible = true;
} catch(e) {
alert("請確認IE安全設置,ActiveX都啟用!");
return false;
}
clipboardData.setData('text','');
}
</script>
table
var table = $('#mainTable').DataTable(.......);
引用
<span style="float:right"><button onclick="javascript:exportToExcel('mainTable','');">導出</button></span>
現在一直再提示 "請確認IE安全設置,ActiveX都啟用!“ 可是我已經打開了ActiveX,怎麼回事?
去掉try..catch看哪行報錯,不一定是acx問題,你訪問剪貼板也可能會報錯,ie only的少用。。。