js生成Excel

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>生成Excel</title>
<style>
<!--
table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:1.0in .75in 1.0in .75in;
mso-header-margin:.5in;
mso-footer-margin:.5in;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
.style0
{mso-number-format:General;
text-align:general;
vertical-align:middle;
white-space:nowrap;
mso-rotate:0;
mso-background-source:auto;
mso-pattern:auto;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
border:none;
mso-protection:locked visible;
mso-style-name:常规;
mso-style-id:0;}
td
{mso-style-parent:style0;
padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:general;
vertical-align:middle;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl24
{mso-style-parent:style0;
font-size:24.0pt;
font-family:"Lucida Sans Unicode", sans-serif;
mso-font-charset:0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid black;
border-right:none;
border-bottom:none;
border-left:.5pt solid black;}
.xl25
{mso-style-parent:style0;
font-size:24.0pt;
font-family:"Lucida Sans Unicode", sans-serif;
mso-font-charset:0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid black;
border-right:none;
border-bottom:none;
border-left:none;}
.xl26
{mso-style-parent:style0;
font-size:20.0pt;
font-weight:700;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid black;
border-right:none;
border-bottom:none;
border-left:none;}
.xl27
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid black;
border-right:none;
border-bottom:none;
border-left:none;}
.xl28
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid black;
border-right:.5pt solid black;
border-bottom:none;
border-left:none;}
.xl29
{mso-style-parent:style0;
mso-number-format:"\@";}
.xl30
{mso-style-parent:style0;
font-size:24.0pt;
font-family:"Lucida Sans Unicode", sans-serif;
mso-font-charset:0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:none;
border-bottom:none;
border-left:.5pt solid black;}
.xl31
{mso-style-parent:style0;
font-size:24.0pt;
font-family:"Lucida Sans Unicode", sans-serif;
mso-font-charset:0;
mso-number-format:"\@";
text-align:center;}
.xl32
{mso-style-parent:style0;
font-size:20.0pt;
font-weight:700;
mso-number-format:"\@";
text-align:center;}
.xl33
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;}
.xl34
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid black;
border-bottom:none;
border-left:none;}
.xl35
{mso-style-parent:style0;
font-size:20.0pt;
font-weight:700;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:none;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl36
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:none;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl37
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid black;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl38
{mso-style-parent:style0;
font-size:22.0pt;
font-weight:700;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:.5pt solid black;
layout-flow:vertical;}
.xl39
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid windowtext;
border-right:none;
border-bottom:none;
border-left:.5pt solid windowtext;
white-space:normal;}
.xl40
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid windowtext;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:none;
white-space:normal;}
.xl41
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid windowtext;
border-right:none;
border-bottom:none;
border-left:.5pt solid windowtext;}
.xl42
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid windowtext;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:none;}
.xl43
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:.5pt solid windowtext;
border-right:.5pt solid black;
border-bottom:none;
border-left:none;}
.xl44
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:none;
border-bottom:none;
border-left:.5pt solid windowtext;
white-space:normal;}
.xl45
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:none;
white-space:normal;}
.xl46
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:none;
border-bottom:none;
border-left:.5pt solid windowtext;}
.xl47
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:none;}
.xl48
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:none;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;
white-space:normal;}
.xl49
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:none;
white-space:normal;}
.xl50
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:none;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;}
.xl51
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl52
{mso-style-parent:style0;
font-size:22.0pt;
font-weight:700;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid black;
border-left:.5pt solid black;
layout-flow:vertical;}
.xl53
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:none;
border-bottom:.5pt solid black;
border-left:.5pt solid windowtext;}
.xl54
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid black;
border-left:none;}
.xl55
{mso-style-parent:style0;
mso-number-format:"\@";
text-align:center;
border-top:none;
border-right:.5pt solid black;
border-bottom:.5pt solid black;
border-left:none;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>

<script language="jscript">
function replaceHtml(replacedStr,repStr,endStr){
var replacedStrF = "";
var replacedStrB = "";
var repStrIndex = replacedStr.indexOf(repStr);
while(repStrIndex != -1){
replacedStrF = replacedStr.substring(0,repStrIndex);
replacedStrB = replacedStr.substring(repStrIndex,replacedStr.length);
replacedStrB = replacedStrB.substring(replacedStrB.indexOf(endStr)+1,replacedStrB.length);
replacedStr = replacedStrF + replacedStrB;
repStrIndex = replacedStr.indexOf(repStr);
}
return replacedStr;
}
//elTalbeOut 这个为导出内容的外层表格,主要是设置border之类的样式,elDiv则是整个导出的html部分
function htmlToExcel(elTableOut,elDiv){
try{
//设置导出前的数据,为导出后返回格式而设置
var elDivStrBak = elDiv.innerHTML;
//设置table的border=1,这样到excel中就有表格线 ps:感谢双面提醒
elTableOut.border=1;
//过滤elDiv内容
var elDivStr = elDiv.innerHTML;
elDivStr = replaceHtml(elDivStr,"<A",">");
elDivStr = replaceHtml(elDivStr,"</A",">");
elDiv.innerHTML=elDivStr;

var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText( elDiv );
oRangeRef.execCommand("Copy");

//返回格式变换以前的内容
elDiv.innerHTML = elDivStrBak;
//内容数据可能很大,所以赋空
elDivStrBak = "";
elDivStr = "";

var oXL = new ActiveXObject("Excel.Application")
var oWB = oXL.Workbooks.Add ;
var oSheet = oWB.ActiveSheet ;
oSheet.Paste();
oSheet.Cells.NumberFormatLocal = "@";
//oSheet.Columns("D:D").Select
oXL.Selection.ColumnWidth =7
oXL.Visible = true;
oSheet = null;
oWB = null;
appExcel = null;
}catch(e){
alert(e.description)
}
}
</script>
</head>


<body link=blue vlink=purple class=xl29>
<p>
<INPUT type="button" value="导出" id="dcExcel" onClick="htmlToExcel(document.getElementById('elTableOut'),document.getElementById('elDiv'));">
</p>
<div id="elDiv">
<table x:str border=0 cellpadding=0 cellspacing=0 width=915 id="elTableOut" style='border-collapse:
collapse;table-layout:fixed;width:690pt'>
<col class=xl29 width=61 span=15 style='mso-width-source:userset;mso-width-alt:
1952;width:46pt'>
<tr height=19 style='height:14.25pt'>
<td colspan=3 rowspan=3 height=57 class=xl24 width=183 style='height:42.75pt;
width:138pt'>Ebase</td>
<td colspan=9 rowspan=3 class=xl26 width=549 style='border-bottom:.5pt solid black;
width:414pt'>2011年春夏2月浪游法国·法式田园表-梭织</td>
<td colspan=3 rowspan=2 class=xl27 width=183 style='border-right:.5pt solid black;
width:138pt'> </td>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
<td colspan=3 height=19 class=xl36 style='border-right:.5pt solid black;
height:14.25pt'>2010-年10月18日</td>
</tr>
<tr height=19 style='height:14.25pt'>
<td rowspan=29 height=494 class=xl38 style='border-bottom:.5pt solid black;
height:370.5pt'>斯文系列</td>
<td colspan=2 rowspan=14 class=xl39 width=122 style='border-right:.5pt solid black;
border-bottom:.5pt solid black;width:92pt'> </td>
<td colspan=2 rowspan=14 class=xl39 width=122 style='border-right:.5pt solid black;
border-bottom:.5pt solid black;width:92pt'> </td>
<td colspan=2 rowspan=14 class=xl39 width=122 style='border-right:.5pt solid black;
border-bottom:.5pt solid black;width:92pt'> </td>
<td colspan=2 rowspan=14 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
<td colspan=2 rowspan=14 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
<td colspan=2 rowspan=14 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
<td colspan=2 rowspan=14 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
<td colspan=2 rowspan=15 height=266 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black;height:199.5pt'> </td>
<td colspan=2 rowspan=15 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
<td colspan=2 rowspan=15 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
<td colspan=2 rowspan=15 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
<td colspan=2 rowspan=15 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
<td colspan=2 rowspan=15 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
<td colspan=2 rowspan=15 class=xl41 style='border-right:.5pt solid black;
border-bottom:.5pt solid black'> </td>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=19 style='height:14.25pt'>
</tr>
<tr height=0 style='display:none'>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
<td width=61 style='width:46pt'></td>
</tr>
</table>

</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值