JavaScript exports the page table as a concrete implementation of Excel

  • 2020-03-30 01:06:13
  • OfStack

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> export Excel</TITLE> 
<script type="text/javascript"> 
var idTmr = ""; 
function killExcelProcess(appExcel_){ 
appExcel_.Quit(); 
appExcel_ = null; 
idTmr = window.setInterval("Cleanup();",1); 
} 

//Export to excel, the parameter tableid is the id of the table to be exported from the JSP page, and the security Settings of IE need to be changed. ActiveX is open. If it still cannot be used, run "regsvr32 scrrun.dll" on the server.
function exportToExcel(tableid,notitleandsearch){ 
try { 
clipboardData.setData('Text',''); 
var appExcel = new ActiveXObject("Excel.Application"); 

killExcelProcess(appExcel); 
appExcel.workbooks.add; 

// The title  
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(" Please make sure the IE Security Settings, ActiveX All enabled! "); 
return false; 
} 
clipboardData.setData('text',''); 
} 
</script> 
</HEAD> 

<BODY> 
<button onclick="javascript:exportToExcel('testList','');"> export </button><br> 

<div id='div_title' > 
<font color='black' size='4'><strong> Exporting the report </strong></font> 
</div> 
<table id = "testList" bordercolor="#000000" border = "1"> 
<tr> 
<td> The sequence </td> 
<td> The name </td> 
<td> The number of </td> 
</tr> 
<tr> 
<td>1</td> 
<td> Zhang SAN 1</td> 
<td>2</td> 
</tr> 
<tr> 
<td>2</td> 
<td> Zhang SAN 2</td> 
<td>2</td> 
</tr> 
<tr> 
<td>3</td> 
<td> Zhang SAN 3</td> 
<td>2</td> 
</tr> 
<tr> 
<td>4</td> 
<td> Zhang SAN 4</td> 
<td>2</td> 
</tr> 
<tr> 
<td>5</td> 
<td> Zhang SAN 5</td> 
<td>2</td> 
</tr> 
<tr> 
<td colspan="2"> A total of: </td> 
<td>10</td> 
</tr> 

</table> 
</BODY> 
</HTML> 

Related articles: