日期:2014-05-16  浏览次数:20304 次

JS导出table为excel
function exportExcel(){
try   
    {
        var table=document.all("table");
        var oXL = new ActiveXObject("Excel.Application");
        var oSheet=oXL.Workbooks.Add().ActiveSheet;
        for (i=0; i < table.rows.length; i++) {
            for (j=0; j < table.rows(i).cells.length; j++){
              oSheet.Cells(i+1,j+1).Font.Size = 9;
              oSheet.Cells(i+1,j+1).Font.Name = "宋体";   
              oSheet.Cells(i+1,j+1).HorizontalAlignment = 3; //水平对齐方式枚举* (1-常规,2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)
              oSheet.Cells(i+1,j+1).VerticalAlignment = 2; //垂直对齐方式枚举*(1-靠上,2-居中,3-靠下,4-两端对齐,5-分散对齐)
              oSheet.Cells(i+1,j+1).WrapText=true; //自动换行
              oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
            }
            oXL.Visible = false;
            oXL.UserControl = true;
        }
        oSheet.SaveAs("C:\\Documents and Settings\\PC2\\桌面");
        oXL.Quit();
        alert("导出成功!");
    }      
    catch(e)    
    {    
    alert("错误!导致错误的原因可能是您的电脑没有安装Microsoft Excel软件")    
      return false   
    }    
}
参考http://apps.hi.baidu.com/share/detail/137693