javascript 将页面上的Table导出保存为Excel (无格式)

 接受参数:Table的 ID

function tableToExcel(tblName)   
{
  
function String.prototype.Trim() {return this.replace(/(^\s*)|(\s*$)/g,"");}
  
try
  
{
    
var e = document.getElementById(tblName);
    
var s = ""
    
for (var j=0; j<e.rows.length; j++)
    
{
      
if (e.rows[j].style.display != "none")
      
{
        
for (var i=1; i<e.rows[j].cells.length; i++)
        
{
          
if (e.rows[j].cells[i].style.display != "none"
            s 
+= e.rows[j].cells[i].innerText.Trim() +"\t";
        }

        s 
+= "\r\n";
      }

    }

    
var xlsWindow = window.open("","_blank","width=100,height=100");
    xlsWindow.document.write(s);
    xlsWindow.document.execCommand('Saveas',
true,'%homeDrive%\\Data.xls');
    xlsWindow.close();
  }

  
catch(e)
  
{}
}

posted on 2007-12-06 14:43  流云之心  阅读(2043)  评论(0编辑  收藏  举报

导航