javascript 操作 excel 全攻略
最近做一个项目,用到了javascript操纵excel以生成报表,下面是标有详细注解的实例
<html>
<head>
<script language="javascript" type="text/javascript">
</script><script language="javascript" type="text/javascript">
function MakeExcel(){
var i,j;
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,6)).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="data:images/jiao1.gif" alt="" /></td>
<td class="topMiddle"></td>
<td class="right"><img src="data: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文件" onclick="MakeExcel()" /><input type="submit" name="fors:_id8" value="返回" /></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="data:images/jiao3.gif" alt=""/>
</td>
<td class="bottomMiddle"> </td>
<td class="right">
<img src="data:images/jiao4.gif" alt=""/>
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="fors" value="fors" /></form>
</body>
</html>
javascript 操作 excel 全攻略的更多相关文章
- JavaScript 异步开发全攻略(转)
写了一本介绍 JavaScript 异步开发的小书: https://meathill.gitbooks.io/javascript-async-tutorial/content/ 除了比较详细的介绍 ...
- webBrowser中操作网页元素全攻略
原文 webBrowser中操作网页元素全攻略 1.获取非input控件的值: webBrowser1.Document.All["控件ID"].InnerText; 或webBr ...
- [Perl]Windows 系统 Unicode 文件名操作(新建、重命名、枚举、复制)全攻略
[Perl] Windows 系统 Unicode 文件名操作(新建.重命名.枚举.复制)全攻略 环境 XP/WIN7 Perl v5.16 编辑整理:PerlMonk.523066680 常见的那些 ...
- C++字符串【string】和【char []】操作全攻略
异想之旅:本人博客完全手敲,绝对非搬运,全网不可能有重复:本人无团队,仅为技术爱好者进行分享,所有内容不牵扯广告.本人所有文章发布平台为CSDN.博客园.简书和开源中国,后期可能会有个人博客,除此之外 ...
- QTP DataTable全攻略(1)
上一篇 / 下一篇 2009-07-27 00:14:16 / 个人分类:qtp 查看( 575 ) / 评论( 0 ) / 评分( 0 / 0 ) 下面的代码可能有点乱,基本涉及到常用的datat ...
- VSCode插件开发全攻略(七)WebView
更多文章请戳VSCode插件开发全攻略系列目录导航. 什么是Webview 大家都知道,整个VSCode编辑器就是一张大的网页,其实,我们还可以在Visual Studio Code中创建完全自定义的 ...
- VSCode插件开发全攻略(五)跳转到定义、自动补全、悬停提示
更多文章请戳VSCode插件开发全攻略系列目录导航. 跳转到定义 跳转到定义其实很简单,通过vscode.languages.registerDefinitionProvider注册一个provide ...
- VSCode插件开发全攻略(四)命令、菜单、快捷键
更多文章请戳VSCode插件开发全攻略系列目录导航. 命令 我们在前面HelloWord章节中已经提到了命令写法,这里再重温一下. context.subscriptions.push(vscode. ...
- VSCode插件开发全攻略(二)HelloWord
更多文章请戳VSCode插件开发全攻略系列目录导航. 写着前面 学习一门新的语言或者生态首先肯定是从HelloWord开始. 您可以直接克隆我放在GitHub上vscode-plugin-demo 的 ...
随机推荐
- Hbase shell详情
HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”.HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建.删 ...
- 使用Yii框架自带的CActiveForm实现ajax提交表单
Php代码: <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array ...
- oracle导入导出数据库和创建表空间和用户
直入主题: 首先在本地创建2个文件,D:\oradata\jgszz\temp.dbf和 D:\oradata\jgszz\data.dbf. 然后执行下面的SQL. /*创建临时表空间 */ cre ...
- IT智力题
题目1:你让工人为你工作7天,给工人的回报是一根金条.金条平分成相连的7段,你必须在每天结束时给他们一段金条,如果只许你两次把金条弄断,你 如何给你的工人付费? 答案:两次弄断就应分成三份,我把金条分 ...
- LeetCode Database: Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- android参考
android:使用BaseExpandableListAdapter实现可折叠的列表 Android-ListView实现SectionIndexer SectionIndexer 的使用(联系人分 ...
- 40个超酷的jQuery动画效果教程
自从出现,jQuery就在web领域就引起了轰动,现在它已经成为Web动画效果的最佳解决方案之一.jQuery提供了良好的交叉浏览器支持,并且轻便易用.现在,jQuery在定义和控制小型的Web动画诸 ...
- 调试Python代码的工具
pdb: 首先来说Python里内建的调试器,pdb.它利用一个简单的命令行界面,还有很多你在用调试器时用得上的功能.帮助系统能为你指出你能运行的命令,比如单步调试代码,操纵调用栈和设置断点. 一些它 ...
- 第三百五十一天 how can I 坚持
是应该喜欢还是厌烦这种状态,犹豫不定,毫无目标. 人不贪,谁信,我嘴上说我不贪,可是内心已经把我出卖了,要不怎么股票会被套呢. 别人贪婪时我恐惧,别人恐惧时我贪婪,我成了什么,别人贪婪时,我狂妄,别人 ...
- InterfaceConnect
GUID aguid; _di_IInterface a, c; Calld::TEventSink* FEventSink; Server_tlb::_di_IServerWithEvents FS ...