How do I import a page table contents into Excel

Source: Internet
Author: User

  This article mainly describes how to import Web page content into Excel, you need friends can refer to the following

  Code as follows://Export to Excel, note that there is no comment in the form tag, because ChildNodes will also include the comments in  //If there is an error, you need to close Excel, otherwise the Excel process will always run  // TD can only have one input value  //non-TD cannot have a build otherwise reported missing ";" error  //reference page plus <script type= "Text/javascript" src= "Execl.js" >< /script> //Add button <input style= "height:26px" type= "button" Name= "Btnexcel" value= "Export Excel" onclick= " Exportexcel (This, ' tablenr ', ', ' Table Head '); /> //Set the table's ID and class to tablenr  function Exportexcel (btn,tabid,strcols,stitle,sheader,sfooter) {  //alert (' ssss ');  btn.style.cursor = "Wait";  Event.returnvalue = false; //try{  var tab = doc Ument.getelementbyid (TabID); //if (tab = = null) tab = document.getElementById ("DG")  //if (tab = = null) Tab = Do Cument.getelementbyid ("db")  //if (tab = = null) tab = document.getElementById (tabid)   if (tab = = null) {  Alert ("Missing Table object");  Btn.style.cursor = "Hand";  return; }  var t = tab.firstchild;  var rows = t.c hildnodes.length; //alert (T.nodenAME); test  var tds = t.childnodes[0].childnodes.length;  var cols = 0;  for (var i=0;i<tds;i++) {  var t D = t.childnodes[0].childnodes[i];  if (parseint (Td.colspan) >1) {  cols + = parseint (Td.colspan); }   else{  cols++; } }  try{  var oxl = new ActiveXObject ("Excel.Application"); }CATC H (E) {  alert ("Please confirm that Excel is installed and allow excel! to run");   Alert ("Unable to start Excel, make sure the computer is already installed EXCEL!NN if Excel is installed," + "Add the IP address to the trusting site and adjust the security level of IE Trust site.) NN specific operations: NN "+" tool →internet options → security → custom level →activex controls and Plug-ins → Initialize and execute script → enable → OK for ActiveX controls not marked as safe to execute;  btn.style.cu Rsor = "Hand";  return; }  oXL.Workbooks.Add ();  var obook = oxl.activeworkbook;  var osheets = obook. worksheets;  var osheet = obook. Sheets (1);  var xlrow = 1; //Add 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; Center   oXL.Selection.MergeCells = true;  xlrow++; //Small title   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; Center   oXL.Selection.MergeCells = true;  xlrow++; }  var WinX = (screen.width-300)/2;  var wi NY = (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 Body" > Total ' + rows + ' row, 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= ' font-size:10pt; Font-family: Song body ' > Total "+ Rows +" row, exported <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;  v Ar viwcol =-1; The location of the table, indicating where the row is merged, inconsistent with the column index   var colinx = -1;  var Colspans = 1;  for (var h = 0; h < cols; h++) {&NB Sp 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; } ; Skip Merge Columns   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 radio box, traverse the box for all the radio boxes, 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; } } } }  s = t d.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{ . Cells (xlrow,xlcol). Value = S;//m_splitlen (s,30, ' rn '); } }  xlrow++; } //Add 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 =" Watchmaker: "+ 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; Center   oXL.Selection.MergeCells = true;  osheet. Range (osheet. Cells (1, 1), osheet. Cells (1,1)). Select (); Select the first cell   osheet. Columns.AutoFit (); Auto column width   for (var i=1;i<xlrow;i++) {  osheet. Rows (i). RowHeight = osheet. Rows (i). RowHeight + 6; Automatic size after the upper and lower margin, you need to increase the height, or too crowded.  }  if (!isprogresserr) {//Close progress bar   Win.close (); }  oxl.visible = true;  Oxl.usercontrol = t rue;    oXL = null;  obook = null;  Osheets = null;  osheet = null;    Btn.style.cursor = "Hand"; }     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.