Home >Web Front-end >JS Tutorial >A simple example of Js exporting table contents to Excel_javascript skills

A simple example of Js exporting table contents to Excel_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:14:011382browse

Copy code The code is as follows:

function ExportToExcel(tableId) //读取表格中每个单元到EXCEL中 
{
    try{      
            var curTbl = document.getElementById(tableId);
            var oXL = new ActiveXObject("Excel.Application");
            //创建AX对象excel 
            var oWB = oXL.Workbooks.Add();
            //获取workbook对象 
            var oSheet = oWB.ActiveSheet;

            var lenRow = curTbl.rows.length;
            //取得表格行数 
            for (i = 0; i < lenRow; i++)
            {
                var lenCol = curTbl.rows(i).cells.length;
                //取得每行的列数 
                for (j = 0; j < lenCol; j++)
                {
                    oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText; 

                }
            }
            oXL.Visible = true;
            //设置excel可见属性 
      }catch(e){
            if((!+'/v1')){ //ie浏览器 
              alert("无法启动Excel,请确保电脑中已经安装了Excel!/n/n如果已经安装了Excel,"+"请调整IE的安全级别。/n/n具体操作:/n/n"+"工具 → Internet选项 → 安全 → 自定义级别 → ActiveX 控件和插件 → 对未标记为可安全执行脚本的ActiveX 控件初始化并执行脚本 → 启用 → 确定");
           }else{
               alert("请使用IE浏览器进行“导入到EXCEL”操作!");  //方便设置安全等级,限制为ie浏览器 
           }
       }

function ExportToExcel(tableId) //Read each cell in the table into EXCEL
{
try{
var curTbl = document.getElementById(tableId);
var oXL = new ActiveXObject ("Excel.Application");
                                                                                            et;

var lenRow = curTbl.rows.length;
//Get the number of table rows
for (i = 0; i < lenRow; i )
{
var lenCol = curTbl .rows(i).cells.length;
                                                                                                                                     oSheet.Cells(i 1 , j 1).value = curTbl.rows(i).cells(j).innerText;

                                                                       //Set excel visible attributes
}catch(e){
                                                                                                                                                                                                                                                                                     nIf Excel is already installed," "Please adjust the security level of IE. /n/nSpecific operation: /n/n" "Tools → Internet Options → Security → Custom Level → ActiveX Controls and Plug-ins → Mark Not as OK. The ActiveX control of the security execution script initializes and executes the script → enable → determine "); "); // Conveniently set the security level, limited to IE browser
                                                                                                              

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn