SoFunction
Updated on 2025-02-28

How to import web table content into excel


//Export to Excel, note that there must be no comments in the table mark, because childNodes will also count the comments in it.
//If there is an error, you need to close Excel, otherwise the Excel process will continue to run
// There can only be one input value in td
//There cannot be formed between non-tds. Otherwise, the missing ";" error will be reported.
//Cite the page plus <script type="text/javascript" src=""></script>
//Add button <input style="height:26px" type="button" name="btnExcel" value="Export excel" onClick="ExportExcel(this,'tableNr','','tableheader');"/>
//Set the id and class of the table table to tableNr
function ExportExcel(btn,TabId,strCols,sTitle,sHeader,sFooter){
//alert('ssss');
= "wait";
= false;
//try{
var tab = (TabId);
// if(tab == null) tab = ("dg")
//if(tab == null) tab = ("db")
//if(tab == null) tab = (TabId)
if(tab == null){
alert("Discover table object");
= "hand";
return;
}
var t = ;
var rows = ;
//alert(); //test
var tds = [0].;
var cols = 0;
for(var i=0;i<tds;i++){
var td = [0].childNodes[i];
if(parseInt()>1){
cols += parseInt();
}
else{
cols++;
}
}
try{
var oXL = new ActiveXObject("");
}catch(e){
alert("Please make sure Excel is installed and allow Excel to run!");
alert("Unable to start Excel, please make sure Excel is installed on your computer!\n\nIf Excel is already installed, "+" please add the IP address to the trusted site and adjust the security level of the IE trusted site.\n\nSpecific operations:\n\n"+" Tools → Internet Options → Security → Custom level → ActiveX Controls and Plugins → Initialize and execute scripts for ActiveX controls not marked as safely executing scripts → Enable → OK");
= "hand";
return;
}
();
var obook = ;
var osheets = ;
var osheet = (1);
var xlrow = 1;
//Add a title
if((sTitle == "") || (typeof(sTitle)=="undefined") || (sTitle==null)){
var t_tdHeadc = ("tdHeadc");
if(t_tdHeadc != null){
sTitle = t_tdHeadc.innerText;
var sk = ("-->")+3;
sTitle = (sk);
}
}
(1, 1) = sTitle;
((xlrow, 1),(xlrow,cols)).Select(); //Select this column
= 3; //Center
= true;
xlrow++;
//subtitle
if((sHeader == "") || (typeof(sHeader)=="undefined") || (sHeader==null)){
sHeader = "";
}
if(sHeader != ""){
(2, 1) = sHeader;
((xlrow, 1),(xlrow,cols)).Select(); //Select this column
// = 3; //Center
= true;
xlrow++;
}
var winX = ( - 300) / 2;
var winY = ( - 120) / 2;

var win = ("","","directories=0,location=0,memubar=0,scrollbars=0,status=0,toolbar=0,width=230,height=75,left=" + winX + ",top=" + winY);
('<html><title>' + sTitle + 'Export Excel</title><body><div style="background:white;overflow:hidden;padding-top:0;display:none;position:absolute;left:10px;top:25px;"><table border=0 cellpacing=1 bgcolor="#CCCCCC" style="display:inline;border-width:1px;border-style:solid;border-left-color:#33333;border-top-color:#33333;border-right-color:#EEEEE;border-bottom-color:#EEEEE;">');
('<tr height=17><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#000088"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td><td width=4 bgcolor="#CCCCCC"></td>');
('</tr></table><br><span style="font-size:10pt;vertical-align:middle;color:black;font-family:install">Total' + rows + 'rows, exported<font color="#cc0000"></font> rows!</span></div><br /><br /><script language="javascript">var osx=("sx");var div = ("m_pub_wzs_progress_x");function m_pub_wzs_progress_show(pTotalCount,pCourrCount){ = pCurrCount;var m = (pCurrCount / pTotalCount * 30); = "";var tr = [0];for(var i=0;i<;i++){var td=[i]; if(i<m) ="#000088"; else ="#CCCCCC";}}function m_pub_wzs_progress_hide(){ var div = ("m_pub_wzs_progress_x"); = "none";}</script></body></html> ');

//("<div style='font-size:10pt;font-family:An An'>Total" + rows + "rows, exported <font id='sx' color='#cc0000'></font>rows!</div>");
//win.m_pub_wzs_progress_show(0,rows);
strCols = ","+strCols+",";
var aRowSpans = new Array();
for(var i=0;i<cols;i++){
aRowSpans[i] = 1;
}
var isProgressErr = false;
for(var i = 0; i < rows; i++){
// = i;
if(!isProgressErr){
try{
win.m_pub_wzs_progress_show(rows,i+1);
}
catch(e){
isProgressErr = true;
}
}
var row = [i];
var xlcol =0;
var viwCol = -1; //The position of the table refers to the display position. If there are rows merged, it will be inconsistent with the column index
var colInx = -1;
var colSpans = 1;
for(var h = 0; h < cols; h++){
if(aRowSpans[h]>1){
xlcol++;
aRowSpans[h]--;
viwCol++;
continue;
}
else{
colInx++;
}
var td = [i].childNodes[colInx];
if(td == null) continue;
colSpans = ;
var rowSpan = ;
if(isNaN(rowSpan)) rowSpan = 1;
for(var k=0;k<;k++){
viwCol++;
xlcol++;
aRowSpans[viwCol] = rowSpan;
}
h+=-1; //Skip the merge column
if(=="hideNode"){
xlcol--;
continue;
}
var s = "";
if(() && ()=="input"){
if(()=="text"){
s = ;
}
else if(()=="radio"){ //If it is a radio box, traverse all radio boxes in the box and find the selected value
for(var k=0;k<;k++){
var cn = [k];
if(()=="input" && () == "radio" && ){
s = ;
break;
}
}
}
}
else{
s = ;
}
if(("," + (xlcol-1) + ",")!=-1){
(xlrow,xlcol).NumberFormatLocal = '@';
}
if(>1 || >1){
((xlrow,xlcol),(xlrow-1+, +1)).Select();
= true;
(xlrow,xlcol).HorizontalAlignment = 3;
//(i+, +1).value = s;
(xlrow,+1).value = s;//m_splitLen(s,30,'\r\n');
}else{
(xlrow,xlcol).value = s;//m_splitLen(s,30,'\r\n');
}
}
xlrow++;
}
//Add the end of the table
if((sFooter == "") || (typeof(sFooter)=="undefined") || (sFooter==null)){
sFooter = "";
}
var d = new Date();
var sUser = "";
if(( != null) && ( != null)){
try{
sUser = ("labUser").("User:","").replace(/ /g,"");
}
catch(ex){
}
}
var sFooterDefault = "Tabulator: " + sUser + "Tabular time: " + () + "-" + (() + 1) + "-" + () + " " + () + ":" + () + ":" + () + " ";
(xlrow, 1) = sFooterDefault + sFooter;
((xlrow, 1),(xlrow,cols)).Select(); //Select this column// = 4; //Center
= true;
((1, 1),(1,1)).Select(); //Select the first cell column
(); //Automatic column width
for(var i=1;i<xlrow;i++){
(i).RowHeight = (i).RowHeight + 6; //There is no borderline after automatic size, so the height needs to be increased, so it is not too crowded.
}
if(!isProgressErr){ //Close the progress bar
();
}
= true;
= true;

oXL = null;
obook = null;
osheets = null;
osheet = null;

= "hand";
}