//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>