function toExcel()
        {
            var objTable = document.getElementById("<%=gdvSchedulePlan.ClientID %>");
            if (objTable == null)
            {
                objTable = document.getElementById("gdvSchedulePlan");
            }
            if (objTable == null)
            {
                alert("找不到数据源!");
                return;
            }
           
            var tempTable = objTable.cloneNode(true);
            tempTable.id = "tempTable1119";
            document.body.insertBefore(tempTable);
           
            //cleanup
            for(var i=0; i<tempTable.rows.length; i++)
            {
                for(var j=tempTable.rows[i].cells.length-1; j>=0; j--)
                {
                    var cell = tempTable.rows[i].cells[j];
                    if (cell.style.display.toLowerCase() == "none")
                    {
                        tempTable.rows[i].removeChild(cell);
                        continue;
                    }
                    removeHiddenElement(tempTable.rows[i].cells[j]);
                }
            }

            var oControlRange = document.body.createControlRange();
            oControlRange.add(tempTable,0);                
            oControlRange.select();
            document.execCommand("Copy");
           
            document.body.removeChild(tempTable);

            var xls;
            try
            {
                xls = new ActiveXObject( "Excel.Application" );
            }
            catch(e)
            {
                ShowHtmlString("提示","<div style='padding:15px;'>你的浏览器设置不正确或Excel没有安装!<br />请启用Active控件和插件.<br />可以点击以下链接查看参考设置步骤:<br /><a href='ActiveX.htm' target='_blank'>ActiveX设置说明</a></div>",330,120);
                return false;
            }

            xls.visible = true;
            var xlBook = xls.Workbooks.Add;
            var xlsheet = xlBook.Worksheets(1);
            xlBook.Worksheets(1).Activate;
            xlsheet.Paste;
            xls=null;
        }   
       
        //Add by bisdev01(luqingfei)#2010-11-19 只保留文字资料,删除input,image,以及隐藏的元素
        function removeHiddenElement(parentElement)
        {
            if (parentElement != null)
            {
                for(var i=parentElement.children.length-1; i >= 0; i--)
                {
                    var cld = parentElement.children(i);
                    if (cld.tagName.toLowerCase() == "input"
                        || cld.tagName.toLowerCase() == "img" )
                    {
                        parentElement.removeChild(cld);
                        continue;
                    }
                   
                    if (cld.style.display.toLowerCase() == "none")
                    {
                        parentElement.removeChild(cld);
                        continue;
                    }
                   
                    cld.style.color = "black";
                   
                    if (cld.children.length > 0)
                    {
                        removeHiddenElement(cld);
                    }
                }
            }
        }

Posted on 2010-12-17 14:12  jasonC  阅读(165)  评论(0编辑  收藏  举报