js導出到word excel .

 

       //js導出到Excel  (此種導出excel方式不推薦, 薦:點此連接 方法2,js獲取table標籤傳入後臺即可
       function OpenExcel(tableid) {//整個表格拷貝到EXCEL中

            var curTbl = document.getElementById(tableid);
            if(curTbl==null)
            {
                alert("沒有數據");
            }else
            {
                var oXL = new ActiveXObject("Excel.Application");
                //創建AX對象excel
                var oWB = oXL.Workbooks.Add();
                //獲取workbook對象
                var oSheet = oWB.ActiveSheet;
                //激活當前sheet
                var sel = document.body.createTextRange();
                sel.moveToElementText(curTbl);
                //把表格中的內容移到TextRange中

                sel.select();
                //全選TextRange中內容

                sel.execCommand("Copy");
                //複製TextRange中內容

                oSheet.Paste();
                //粘貼到活動的EXCEL中

                oXL.Visible = true;
                //設置excel可見屬性

            }
        }
        //js導出到word
        function OpenWord(tableid)
        {
            var curTbl = document.getElementById(tableid);
            if(curTbl==null)
            {
                alert("沒有數據");
            }else
            {
                var word = new ActiveXObject("Word.Application");
                var doc = word .Documents.Add("",0,1);//不打開模版直接加入內容
                var   Range=doc.Range(); 

                var sel = document.body.createTextRange();
                sel.moveToElementText(curTbl);
                sel.select();
                sel.execCommand("Copy");
                Range.Paste();
                word .Application.Visible = true;
            }
           
        }

 

 

//實例

function OutTable()
{
    try
    {
        var OutType=window.document.getElementById("drpOutType").options[window.document.getElementById("drpOutType").selectedIndex].value;
        var obj=window.document.getElementById("PrintTable");
     
        if(OutType=="2")
        {
             var Width=window.document.getElementById("txtWidth").value+"px";
            if(obj!=null)
            {
                obj.width=Width;
                obj.align="center";
            }
         
            window.document.getElementById("outDiv").style.display="none"; 
             var oXL = new ActiveXObject("Excel.Application");   
                
             var oWB = oXL.Workbooks.Add();   
             var oSheet = oWB.ActiveSheet;   
             var sel=document.body.createTextRange();   
             sel.moveToElementText(PrintTable);   
             sel.select();   
             sel.execCommand("Copy");   
             oSheet.Paste();   
             oXL.Visible = true;
         }
         else
         {
            var obj=window.document.getElementById("PrintTable");
            var Width=window.document.getElementById("txtWidth").value+"px";
            window.document.getElementById("outDiv").style.display="none"; 

            if(obj!=null)
            {
                var table =null;
                if(window.document.getElementById("report")!=null)
                {
                    table= window.document.getElementById("report").firstChild;
                }
                obj.width=Width;
                obj.align="center";
             
                if(window.document.getElementById("txtRowPage")!=null&&RowPage!=""&&isNaN(RowPage))
                {
                  var RowPage=window.document.getElementById("txtRowPage").value;
                    for(var i=0;i<table.rows.length;i++)
                    {
                        if(i!=0&&i%parseInt(RowPage)-1==0)
                        {alert(RowPage);
                            table.rows[i].style.pageBreakAfter="always";
                        }
                    }
                }
            } 
             var oWD = new ActiveXObject("Word.Application");
             var oDC = oWD.Documents.Add("",0,1); 
             var oRange =oDC.Range(0,1); 
             var sel = document.body.createTextRange();
             sel.moveToElementText(PrintTable);
             sel.select();   
             sel.execCommand("Copy"); 
             oRange.Paste();  
             oWD.Application.Visible = true;
             obj.width="100%";
         }
        
     }
     catch(ex)
     {
        alert(ex);
     }
}

 

 

--Html--

          <div id="outDiv" style="position:absolute;display:none;background-color:White; width: 241px; height: 116px; ">
            <table cellpadding="0" cellspacing="0" width="100%" class="center_tab" style="margin-top:10px;">
                <tr>
                    <td class="ta_bg" colspan="2" >導出</td>
                </tr>
                <tr>
                    <td width="30%" align="right" >導出類別:</td>
                     <td  >
                        <asp:DropDownList ID="drpOutType" CssClass="DropDownList" runat="server">
                            <asp:ListItem Text="word" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Excel" Value="2"></asp:ListItem>
                        </asp:DropDownList>
                     </td>
                </tr>
              
                 <tr>
                    <td width="30%" align="right">寬度:</td>
                     <td>
                         <asp:TextBox ID="txtWidth" runat="server" CssClass="TextBox" >800</asp:TextBox></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input id="btnOutTable" type="button" value="導出" οnclick="OutTable();" /></td>
                </tr>
            </table>
            </div>

 

                    <table id="PrintTable" cellpadding="0" cellspacing="0" align="center" width="100%">
                        <tr>
                            <td class="ta_bg12" align="Center" style="font-size:20pt; height: 39px;" >
                                TableName</td>
                        </tr>
                       
                       
                        <tr>
                            <td valign="top" id="report" >
                            
                            </td>
                        </tr>
                       
                    </table>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章