How to import web table contents into excel
- 2020-03-30 01:49:24
- OfStack
Adds the page to the trusted site
//Export to Excel. Note that the table tag should have no comments in it, because childNodes will count the comments in
//If something goes wrong, you need to close Excel, or else the Excel process will keep running
//There can only be one input value in td
//Non-td can not have a component or report a lack of ";" error
//Reference page + <The script type = "text/javascript" SRC = "execl. Js>" </ script>
//Add button <Input style="height:26px" type="button" name="btnExcel" value=" ExportExcel "onClick="ExportExcel(this,'tableNr',' header '); />
//Set the id and class of the table table to tableNr
function ExportExcel(btn,TabId,strCols,sTitle,sHeader,sFooter){
//alert('ssss');
btn.style.cursor = "wait";
event.returnValue = false;
//try{
var tab = document.getElementById(TabId);
// if(tab == null) tab = document.getElementById("dg")
//if(tab == null) tab = document.getElementById("db")
//if(tab == null) tab = document.getElementById(TabId)
if(tab == null){
alert(" Missing table objects ");
btn.style.cursor = "hand";
return;
}
var t = tab.firstChild;
var rows = t.childNodes.length;
//alert(t.nodeName); //test
var tds = t.childNodes[0].childNodes.length;
var cols = 0;
for(var i=0;i<tds;i++){
var td = t.childNodes[0].childNodes[i];
if(parseInt(td.colSpan)>1){
cols += parseInt(td.colSpan);
}
else{
cols++;
}
}
try{
var oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert(" Please confirm that it has been installed Excel And allow it to run Excel!");
alert(" Unable to start Excel Please make sure the computer is installed Excel!nn If installed Excel . "+" Please send ip The address is added to the trust site and adjusted IE Trust the security level of your site. nn Specific operation: nn"+" tool - Internet options - security - Custom level - ActiveX Controls and plug-ins - For those not marked as safe to execute the script ActiveX Control initializes and executes the script - To enable the - determine ");
btn.style.cursor = "hand";
return;
}
oXL.Workbooks.Add();
var obook = oXL.ActiveWorkBook;
var osheets = obook.Worksheets;
var osheet = obook.Sheets(1);
var xlrow = 1;
//Add the title
if((sTitle == "") || (typeof(sTitle)=="undefined") || (sTitle==null)){
var t_tdHeadc = document.getElementById("tdHeadc");
if(t_tdHeadc != null){
sTitle = t_tdHeadc.innerText;
var sk = sTitle.lastIndexOf("-->")+3;
sTitle = sTitle.substring(sk);
}
}
osheet.Cells(1, 1) = sTitle;
osheet.Range(osheet.Cells(xlrow, 1),osheet.Cells(xlrow,cols)).Select(); //Select the column
oXL.Selection.HorizontalAlignment = 3; // In the middle
oXL.Selection.MergeCells = true;
xlrow++;
//headings
if((sHeader == "") || (typeof(sHeader)=="undefined") || (sHeader==null)){
sHeader = "";
}
if(sHeader != ""){
osheet.Cells(2, 1) = sHeader;
osheet.Range(osheet.Cells(xlrow, 1),osheet.Cells(xlrow,cols)).Select(); //Select the column
//oXL.Selection.HorizontalAlignment = 3; // In the middle
oXL.Selection.MergeCells = true;
xlrow++;
}
var winX = (screen.width - 300) / 2;
var winY = (screen.height - 120) / 2;
var win = window.open("","","directories=0,location=0,memubar=0,scrollbars=0,status=0,toolbar=0,width=230,height=75,left=" + winX + ",top=" + winY);
win.document.write('<html><title>' + sTitle + ' export Excel</title><body><div id="m_pub_wzs_progress_x" style="background:white;overflow:hidden;padding-top:0;display:none;position:absolute;left:10px;top:25px;"><table id="m_pub_wzs_progress_tab" border=0 cellspacing=1 bgcolor="#CCCCCC" style="display:inline;border-width:1px;border-style:solid;border-left-color:#333333;border-top-color:#333333;border-right-color:#EEEEEE;border-bottom-color:#EEEEEE;">');
win.document.write('<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>');
win.document.write('</tr></table><br><span id="m_pub_wzs_progress_percent" style="font-size:10pt;vertical-align:middle;color:black;font-family: Song typeface "> A total of ' + rows + ' Ok, exported <font id="sx" color="#cc0000"></font> Line! </span></div><br /><br /><br /><script language="javascript">var osx=document.getElementById("sx");var div = document.getElementById("m_pub_wzs_progress_x");function m_pub_wzs_progress_show(pTotalCount,pCurrCount){osx.innerText = pCurrCount;var m = Math.floor(pCurrCount / pTotalCount * 30);div.style.display = "";var tr = div.firstChild.rows[0];for(var i=0;i<tr.cells.length;i++){var td=tr.cells[i]; if(i<m) td.bgColor="#000088"; else td.bgColor="#CCCCCC";}}function m_pub_wzs_progress_hide(){ var div = document.getElementById("m_pub_wzs_progress_x"); div.style.display = "none";}</script></body></html> ');
//Win. Document. Write (" <Div style = 'the font - size: 10 pt; The font-family: tahoma '> Total "+ rows +" rows, exported <The font id = 'sx' color = '# cc0000> </ font> Line! </ 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++){
//btn.value = i;
if(!isProgressErr){
try{
win.m_pub_wzs_progress_show(rows,i+1);
}
catch(e){
isProgressErr = true;
}
}
var row = t.childNodes[i];
var xlcol =0;
var viwCol = -1; //The position of the table in which the position is displayed will be inconsistent with the column index if rows are merged
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 = t.childNodes[i].childNodes[colInx];
if(td == null) continue;
colSpans = td.colSpan;
var rowSpan = td.rowSpan;
if(isNaN(rowSpan)) rowSpan = 1;
for(var k=0;k<td.colSpan;k++){
viwCol++;
xlcol++;
aRowSpans[viwCol] = rowSpan;
}
h+=td.colSpan-1; //Skip merge column
if(td.className=="hideNode"){
xlcol--;
continue;
}
var s = "";
if(td.hasChildNodes() && td.firstChild.nodeName.toLowerCase()=="input"){
if(td.firstChild.type.toLowerCase()=="text"){
s = td.firstChild.value;
}
else if(td.firstChild.type.toLowerCase()=="radio"){ //If it is a marquee, walk through all the marquee boxes to find the selected value
for(var k=0;k<td.childNodes.length;k++){
var cn = td.childNodes[k];
if(cn.nodeName.toLowerCase()=="input" && cn.type.toLowerCase() == "radio" && cn.checked){
s = cn.value;
break;
}
}
}
}
else{
s = td.innerText;
}
if(strCols.indexOf("," + (xlcol-1) + ",")!=-1){
osheet.Cells(xlrow,xlcol).NumberFormatLocal = '@';
}
if(td.rowSpan>1 || td.colSpan>1){
osheet.range(osheet.cells(xlrow,xlcol),osheet.cells(xlrow-1+td.rowSpan, xlcol-td.colSpan+1)).Select();
oXL.Selection.MergeCells = true;
osheet.Cells(xlrow,xlcol).HorizontalAlignment = 3;
//osheet.cells(i+td.rowSpan, xlcol-td.colSpan+1).value = s;
osheet.Cells(xlrow,xlcol-td.colSpan+1).value = s;//m_splitLen(s,30,'rn');
}else{
osheet.Cells(xlrow,xlcol).value = s;//m_splitLen(s,30,'rn');
}
}
xlrow++;
}
//Add a footer
if((sFooter == "") || (typeof(sFooter)=="undefined") || (sFooter==null)){
sFooter = "";
}
var d = new Date();
var sUser = "";
if((window.parent != null) && (window.parent.parent != null)){
try{
sUser = window.parent.parent.bottomFrame.document.getElementById("labUser").innerText.replace(" User: ","").replace(/ /g,"");
}
catch(ex){
}
}
var sFooterDefault = " TAB: " + sUser + " Tabulation time: " + d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + " ";
osheet.Cells(xlrow, 1) = sFooterDefault + sFooter;
osheet.Range(osheet.Cells(xlrow, 1),osheet.Cells(xlrow,cols)).Select(); // Select the column //oXL.Selection.HorizontalAlignment = 4; // In the middle
oXL.Selection.MergeCells = true;
osheet.Range(osheet.Cells(1, 1),osheet.Cells(1,1)).Select(); //Select the first cell column
osheet.Columns.AutoFit(); //Automatic column width
for(var i=1;i<xlrow;i++){
osheet.Rows(i).RowHeight = osheet.Rows(i).RowHeight + 6; //Automatic size after up and down margin, need to increase the height, or too crowded.
}
if(!isProgressErr){ //Close the progress bar
win.close();
}
oXL.Visible = true;
oXL.UserControl = true;
oXL = null;
obook = null;
osheets = null;
osheet = null;
btn.style.cursor = "hand";
}