您的位置:首页 > Web前端 > JavaScript

纯js不经后台导出excel实例

2014-05-06 10:48 337 查看

---------------------------------------------------------纯js不经后台导出excel实例 --------------------------------------------------------

一:代码如下

[html] view
plaincopy

<HTML >   

<HEAD >   

<TITLE >js导出excel </TITLE >   

<SCRIPT LANGUAGE="javascript" >   

function AutomateExcel() {   

    var oXL = new ActiveXObject("Excel.Application"); //创建应该对象   

    var oWB = oXL.Workbooks.Add();//新建一个Excel工作簿   

    var oSheet = oWB.ActiveSheet;//指定要写入内容的工作表为活动工作表   

    var table = document.all.data;//指定要写入的数据源的id   

    var hang = table.rows.length;//取数据源行数   

    var lie = table.rows(0).cells.length;//取数据源列数   

    // Add table headers going cell by cell.   

    for (i=0;i <hang;i++){//在Excel中写行   

        for (j=0;j <lie;j++){//在Excel中写列   

            //定义格式   

            oSheet.Cells(i+1,j+1).Font.Bold = true;//加粗   

            oSheet.Cells(i+1,j+1).Font.Size = 10;//字体大小   

            if(table.rows(i).cells(j).innerHTML.toLowerCase().indexOf( " <img ")!=-1){//如果其HTML代码包括 <img   

                oSheet.Cells(i+1,j+1).Select();//选中Excel中的单元格   

                oSheet.Pictures.Insert(table.rows(i).cells(j).getElementsByTagName( "img ")[0].src);//插入图片   

            }else{   

                oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;//向单元格写入值   

            }   

        }   

    }   

    oXL.Visible = true;   

    oXL.UserControl = true;   

}   

</script>  

</HEAD >   

<BODY >   

<table border="0" width="96%" cellspacing="0" cellpadding="0" align="center" class="Noprint">  

<tr>  

<td width="50%" height="13">·营运商信息管理</td>  

<form method="post" action="?mydo=search" name="Form1">  

<td width="50%" height="13" align="right">信息主题:<input type="text" name="KeyWord" size="20" value="" class="face">  

<select size="1" name="types">  

<option value="1" selected>编号</option>  

<option value="2">单位名称</option>  

<option value="3">单位地址</option>  

<option value="4">法定代表人</option>  

<option value="5">管理人员</option>   

<option value="6">联系电话</option>   

</select> <input type="submit" name="Submit" value="搜" class="button"></td>  

</form>  

</tr>  

</table>  

<div align="center">  

  

<table width="98%" border=1 cellpadding=0 bordercolor="#799AE1" class=tableBorder id="data" >  

<tbody>  

<tr>   

<th height=21 width="87">编号</th>  

<th width="87">单位名称</th>  

<th width="301">地址</th>  

<th height=21 width="121">法定代表人</th>  

<th height=21 width="92">管理人员</th>  

<th height=21 width="119">联系电话</th>  

</tr>   

<tr>   

<td height=21 align="left" class=forumRow sytle="line-height:150%">50011500006</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">移动</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">http://www.52hai.com/2005</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某1</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">25254255</td>  

</tr>  

  

<tr>   

<td height=21 align="left" class=forumRow sytle="line-height:150%">50011500005</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">铁通</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">铁通公司</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">25254254</td>  

</tr>  

  

<tr>   

<td height=21 align="left" class=forumRow sytle="line-height:150%">50011500004</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">广电</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">广播电视局</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某1</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">25254254254</td>  

</tr>  

  

<tr>   

<td height=21 align="left" class=forumRow sytle="line-height:150%">50011500002</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%"
4000
>电信</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">电信公司</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">68331018</td>  

</tr>  

  

<tr>   

<td height=21 align="left" class=forumRow sytle="line-height:150%">50011500001</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">网通</td>  

<td height=21 align="left" class=forumRow sytle="line-height:150%">网通公司</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">某某</td>  

<td class=forumRow height=21 sytle="line-height:150%" align="left">25254255</td>  

</tr>  

</table>   

<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" >   

</BODY >   

</HTML >  

页面展示如下





3、导出excel如下



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: