在JavaScript中利用ActiveXObject控制Excel的方法

直接将html中的table拷贝出来:
1.整个表格拷贝到EXCEL中 
function CopyTable(tableid) 
{
//整个表格拷贝到EXCEL中 
var curTbl = document.getElementById(tableid); 
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可见属性 
} 
利用ActiveXObject导出excel
<html> 
<head> 
<script language="javascript" type="text/javascript"> 
function MakeExcel() { 
var i, j, n; 
try { 
var xls = new ActiveXObject("Excel.Application"); 
} 
catch(e) { 
alert( "要打印该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。请点击【帮助】了解浏览器设置方法!"); 
return ""; 
} 
xls.visible =true; // 设置excel为可见 
var xlBook = xls.Workbooks.Add; 
var xlsheet = xlBook.Worksheets(1); 
<!--合并--> 
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).mergecells=true; 
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).value="发卡记录"; 
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,3)).Interior.ColorIndex=5; // 设置底色为蓝色 
// xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Font.ColorIndex=4; // 设置字体色 
// xlsheet.Rows(1). Interior .ColorIndex = 5 ;//设置底色为蓝色 设置背景色 Rows(1).Font.ColorIndex=4 
<!--设置行高--> 
xlsheet.Rows(1).RowHeight = 25; 
<!--设置字体 ws.Range(ws.Cells(i0+1,j0), ws.Cells(i0+1,j1)).Font.Size = 13 --> 
xlsheet.Rows(1).Font.Size=14; 
<!--设置字体 设置选定区的字体 xlsheet.Range(xlsheet.Cells(i0,j0), ws.Cells(i0,j0)).Font.Name = "黑体" --> 
xlsheet.Rows(1).Font.Name="黑体"; 
<!--设置列宽 xlsheet.Columns(2)=14;--> 
xlsheet.Columns("A:D").ColumnWidth =18; 
<!--设置显示字符而不是数字--> 
xlsheet.Columns(2).NumberFormatLocal="@"; 
xlsheet.Columns(7).NumberFormatLocal="@"; 
//设置单元格内容自动换行 range.WrapText = true ; 
//设置单元格内容水平对齐方式 range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//设置单元格内容竖直堆砌方式 
//range.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter 
//range.WrapText = true; xlsheet.Rows(3).WrapText=true 自动换行 
//设置标题栏 
xlsheet.Cells(2, 1).Value = "卡号"; 
xlsheet.Cells(2, 2).Value = "密码"; 
xlsheet.Cells(2, 3).Value = "计费方式"; 
xlsheet.Cells(2, 4).Value = "有效天数"; 
xlsheet.Cells(2, 5).Value = "金额"; 
xlsheet.Cells(2, 6).Value = "所属服务项目"; 
xlsheet.Cells(2, 7).Value = "发卡时间"; 
var oTable = document.all['fors:data']; 
var rowNum = oTable.rows.length; 
for(i = 2; i <= rowNum; i++) { 
for (j = 1; j <= 7; j++) { 
//html table类容写到excel 
xlsheet.Cells(i + 1, j).Value = oTable.rows(i - 1).cells(j - 1).innerHTML; 
} 
} 
<!-- xlsheet.Range(xls.Cells(i+4,2),xls.Cells(rowNum,4)).Merge; --> 
// xlsheet.Range(xlsheet.Cells(i, 4), xlsheet.Cells(i-1, 6)).BorderAround , 4 
// for(mn=1,mn<=6;mn++) . xlsheet.Range(xlsheet.Cells(1, mn), xlsheet.Cells(i1, j)).Columns.AutoFit; 
xlsheet.Columns.AutoFit; 
xlsheet.Range( xlsheet.Cells(1,1),xlsheet.Cells(rowNum+1,7)).HorizontalAlignment =-4108;//居中 
xlsheet.Range( xlsheet.Cells(1,1),xlsheet.Cells(1,7)).VerticalAlignment =-4108; 
xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Font.Size=10; 
xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(3).Weight = 2; //设置左边距 
xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(4).Weight = 2;//设置右边距 
xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(1).Weight = 2;//设置顶边距 
xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(2).Weight = 2;//设置底边距 
xls.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制 
xls=null; 
xlBook=null; 
xlsheet=null; 
} 
</script> 
<link href="css/styles3.css" rel="stylesheet" type="text/css"/> 
<title>ziyuanweihu</title> 
</head> 
<body> 
<form id="fors" method="post" action="/WebModule/admins/card/showcard.faces" enctype="application/x-www-form-urlencoded"> 
<table id="fors:top" border="0" cellpadding="0" cellspacing="0" width="100%"> 
<tbody> 
<tr> 
<td class="left"><img src="images/jiao1.gif" alt="" /></td> 
<td class="topMiddle"></td> 
<td class="right"><img src="images/jiao2.gif" alt="" /></td> 
</tr> 
</tbody> 
</table> 
<table border="0" cellpadding="0" cellspacing="0" width="100%"> 
<tbody> 
<tr> 
<td class="middleLeft"></td> 
<td class="btstyle"> 
<table id="fors:sort" border="0" cellpadding="0" cellspacing="0" style="valign:center" width="100%"> 
<tbody> 
<tr> 
<td class="btstyle"> 
<input type="button" name="fors:_id7" value="生成excel文件" /> 
</td> 
</tr> 
</tbody> 
</table> 
<table id="fors:data" border="1" cellpadding="0" cellspacing="1" width="100%"> 
<thead> 
<tr> 
<th scope="col"><span id="fors:data:headerText1">卡号</span></th> 
<th scope="col"><span id="fors:data:headerText2">密码</span></th> 
<th scope="col"><span id="fors:data:headerText3">计费方式</span></th> 
<th scope="col"><span id="fors:data:headerText4">有效天数</span></th> 
<th scope="col">金额</th> 
<th scope="col"><span id="fors:data:headerText6">所属服务项目</span></th> 
<th scope="col"><span id="fors:data:headerText7">发卡时间</span></th> 
</tr> 
</thead> 
<tbody> 
<tr> 
<td>h000010010</td> 
<td>543860</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010011</td> 
<td>683352</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010012</td> 
<td>433215</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010013</td> 
<td>393899</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010014</td> 
<td>031736</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010015</td> 
<td>188600</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010016</td> 
<td>363407</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010017</td> 
<td>175315</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010018</td> 
<td>354437</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
<tr> 
<td>h000010019</td> 
<td>234750</td> 
<td>计点</td> 
<td></td> 
<td>2.0</td> 
<td>测试项目</td> 
<td>2006-06-23 10:14:40.843</td> 
</tr> 
</tbody> 
</table> 
</td> 
<td class="middleRight"></td> 
</tr> 
</tbody> 
</table> 
<table id="fors:bottom" border="0" cellpadding="0" cellspacing="0" width="100%"> 
<tbody> 
<tr> 
<td class="left"> 
<img src="images/jiao3.gif" alt=""/> 
</td> 
<td class="bottomMiddle"> </td> 
<td class="right"> 
<img src="images/jiao4.gif" alt=""/> 
</td> 
</tr> 
</tbody> 
</table> 
<input type="hidden" name="fors" value="fors" /> 
</form> 
</body> 
</html> 


来源:http://www.jb51.net/article/20605.htm

常用属性方法:

使用JavaScript中的ActiveXObject填充并设置Excel格式
1.创建实例并创建工作表
var XLObj = new ActiveXObject("Excel.Application");//创建Excel实例
var xlBook = XLObj.Workbooks.Add; //新增工作簿
var ExcelSheet = xlBook.Worksheets(1); //创建工作表
2.保存表格
ExcelSheet.SaveAs("C:\\TEST.XLS");
3.使 Excel 通过 Application 对象可见
ExcelSheet.Application.Visible = true;或者ExcelSheet.Visible=true;
4.打印
xlBook.PrintOut;或者:ExcelSheet.PrintOut;
5.关闭
xlBook.Close(savechanges=false);或者ExcelSheet.Close(savechanges=false);
6.结束进程
ExcelSheet.Application.Quit()或者XLObj.Quit(); XLObj=null;
7.页面设置
ExcelSheet.ActiveSheet.PageSetup.LeftMargin= 2/0.035; //页边距 左2厘米
ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035; //页边距 右3厘米,
ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035; //页边距 上4厘米,
ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035; //页边距 下5厘米
ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035; //页边距 页眉1厘米
ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035; //页边距 页脚2厘米
ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "页眉中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "页眉左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightHeader = "页眉右部内容";
ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "页脚中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "页脚左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightFooter = "页脚右部内容";
8.对单元格操作,带*部分对于行,列,区域都有相应属性
ExcelSheet.ActiveSheet.Cells(row,col).Value = "内容"; //设置单元格内容
ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1; //设置单元格边框*()
ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1; //设置单元格底色*(1-黑色,2-白色,3-红色,4-绿色,5-蓝色,6-黄色,7-粉红色,8-天蓝色,9-酱土色..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1; //设置单元格背景样式*(1-无,2-细网格,3-粗网格,4-斑点,5-横线,6-竖线..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1; //设置字体颜色*(与上相同)
ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10; //设置为10号字*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = "黑体"; //设置为黑体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Italic = true; //设置为斜体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Bold = true; //设置为粗体*
ExcelSheet.ActiveSheet.Cells(row,col).ClearContents; //清除内容*
ExcelSheet.ActiveSheet.Cells(row,col).WrapText=true; //设置为自动换行*
ExcelSheet.ActiveSheet.Cells(row,col).HorizontalAlignment = 3; //水平对齐方式枚举* (1-常规,2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)
ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2; //垂直对齐方式枚举*(1-靠上,2-居中,3-靠下,4-两端对齐,5-分散对齐)
//行,列有相应操作:
ExcelSheet.ActiveSheet.Rows(row).
ExcelSheet.ActiveSheet.Columns(col).
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow). //如Rows("1:5")即1到5行
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol). //如Columns("1:5")即1到5列
//区域有相应操作:
XLObj.Range(startcell+":"+endcell).Select;
//如Range("A2:H8")即A列第2格至H列第8格的整个区域
XLObj.Selection.
//合并单元格
XLObj.Range(startcell+":"+endcell).MergeCells = true;
//如Range("A2:H8")即将A列第2格至H列第8格的整个区域合并为一个单元格
或者:XLObj.Range("A2",XLObj.Cells(8, 8)).MergeCells = true;
9.设置行高与列宽
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;//设置从firstcol到stopcol列的宽度为22
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight = 22;//设置从firstrow到stoprow行的宽度为22
Var myrange oSheet.Range(oSheet.Cells(2,1),oSheet.Cells(2,6));
myRange.Columns.AutoFit()
myRange.Rows.AutoFit()//好像不太管用



来源:http://www.cnblogs.com/ForeverSoft/archive/2011/08/31/2160658.html

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 除了使用ActiveXObject函数来判断浏览器是否支持ActiveX控件外,还有其他替代方法。一种常见的替代方法是使用XMLHttpRequest对象来进行AJAX请求。在较新的IE版本,可以使用var xml = new ActiveXObject("Msxml2.XMLHTTP")来创建XMLHttpRequest对象。而在IE7及非IE浏览器,可以使用var xml = new XMLHttpRequest()来创建XMLHttpRequest对象。这样可以实现跨浏览器的兼容性。另外,还可以通过开发本地的Web服务程序来代替ActiveX控件,将原来的ActiveX控件的COM接口变成本地的Web服务接口,从而实现相同的功能。\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [javascript的window.ActiveXObject对象,区别浏览器的方法](https://blog.csdn.net/wangchuanbaodhcc/article/details/6706308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [非IE浏览器下ActiveX技术的替代方案](https://blog.csdn.net/weixin_45303938/article/details/108911609)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值