萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> 如何將網頁表格內容導入excel

如何將網頁表格內容導入excel

 這篇文章主要介紹了如何將網頁表格內容導入excel,需要的朋友可以參考下

 代碼如下: //導出到Excel,注意,表格標記內不得有注釋,因為childNodes會把注釋也算進去  //如果出錯,需要關閉Excel,否則Excel進程會一直運行著  //td中只能有一個input值  //非td間不可以有組建 否則報缺少“;”錯誤  //引用頁面加上<script type="text/javascript" src="execl.js"></script>  //添加按鈕<input style="height:26px" type="button" name="btnExcel" value="導出excel" onClick="ExportExcel(this,'tableNr','','表頭');"/>  //將table表的id和class設置為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("缺少表格對象");  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("請確認已經安裝了Excel並允許運行Excel!");  alert("無法啟動Excel,請確保電腦中已經安裝了Excel!nn如果已經安裝了Excel,"+"請將ip地址加入信任站點,並調整IE信任站點的安全級別。nn具體操作:nn"+"工具 → Internet選項 → 安全 → 自定義級別 → ActiveX 控件和插件 → 對未標記為可安全執行腳本的ActiveX 控件初始化並執行腳本 → 啟用 → 確定");  btn.style.cursor = "hand";  return;  }  oXL.Workbooks.Add();  var obook = oXL.ActiveWorkBook;  var osheets = obook.Worksheets;  var osheet = obook.Sheets(1);  var xlrow = 1;  //添加標題  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(); //選擇該列  oXL.Selection.HorizontalAlignment = 3; //居中  oXL.Selection.MergeCells = true;  xlrow++;  //小標題  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(); //選擇該列  //oXL.Selection.HorizontalAlignment = 3; //居中  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 + '導出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:宋體">總計' + rows + '行,已導出<font id="sx" color="#cc0000"></font>行!</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:宋體'>總共" + rows + "行,已導出<font id='sx' color='#cc0000'></font>行!</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; //所在表格的位置,指顯示位置,如果有行合並時,會與列索引不一致  var colInx = -1;  var colSpans = 1;  for(var h = 0; h < cols; h++){  if(aRowSpans[h]>1){&n
copyright © 萬盛學電腦網 all rights reserved