Posted on 2012-02-28 18:16
哈希 阅读(303)
评论(0) 编辑 收藏 所属分类:
Js and Jquery 常用总结
apache-comcat配置虚拟主机和虚拟目录 jquery autocomplete 自动填充文本框、文本域
JS 导出excel,word实例 2011-10-10 11:55:00| 分类: 默认分类 | 标签:js excel word |字号大
中
小 订阅
用js操作offace中的word,excel 必须首先确保你已经安装office,用js操作word,excel 用到了ActiveXObject类,它是专门用来调用windows操作中的程序的,下面是例子,直接复制到文本文件里,把后缀名改了就可以运行!
<!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=UTF-8" />
<title>无标题文档</title>
<script type="text/javascript" language="javascript">
function ExcelExport(tableid)
{
//检索浏览器
if(navigator.userAgent.indexOf("MSIE")<0){
alert('请用ie浏览器进行表格导出');
return ;
}
var oXL = null;
try {
oXL = GetObject("", "Excel.Application");
}
catch (E) {
try {
oXL = new ActiveXObject("Excel.Application");
}
catch (E2) {
alert("请确认:\n1.Microsoft Excel已被安装.\n2.工具 => Internet 选项=> 安全 => 设置 \"启用不安全的 ActiveX\"");
return;
}
}
var table = document.getElementById(tableid);
var workbook = oXL.Workbooks.Add();
var sheet = workbook.ActiveSheet;
var sel = document.body.createTextRange(); //激活sheet
//把table中的数据移到sel中
sel.moveToElementText(table);
sel.select(); //选中sel中所有数据
sel.execCommand("Copy");//复制sel中的数据
sheet.Columns("A:Z").ColumnWidth =20;//设置列宽
// sheet.Columns("B").ColumnWidth =35;
sheet.Rows(1).RowHeight = 35;//设置表头高
//将sel中数据拷贝到sheet工作薄中
sheet.Paste();
oXL.Visible = true;
//sheet.Save("F:\\TEST.XLS" );
//通过打印机直接将Excel数据打印出来
//sheet.Printout;
//ax.UserControl = true;
oXL.Quit();
oXL=null;
}
</script>
</head>
<body>
<table width="100%" border="1" id="mytable">
<tr>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
</tr>
<tr>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
</tr>
<tr>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
</tr>
<tr>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
</tr>
<tr>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
<td>nihao</td>
</tr>
</table>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="button" name="Submit" value="导出excel" onclick="ExcelExport('mytable')" />
</label>
</form>
<p> </p>
</body>
</html>