分享一个javascript操作excel文件的例子,使用activexobject组件操作excel文件,通过本实例掌握javascript操作excel的方法,感兴趣的朋友参考下。
以下方法生成的分析图与数据是分离的,当数据区的值改变时,分析图是不会随之改变的,如果要一起改变,就只能全部用无所不能的javascript来excel了。
例子:
复制代码 代码示例:
<html>
<body>
<script lanage="javascript">
function createexcel()
{
var exceldemo = new activexobject("excel.application");
exceldemo.visible = true;
var workbook = exceldemo.workbooks.add();
var sheet = workbook.activesheet;
sheet.cells(1,1).value = "1月";
sheet.cells(2,1).value = "2月";
sheet.cells(3,1).value = "3月";
sheet.cells(4,1).value = "4月";
sheet.cells(1,2).value = "120";
sheet.cells(2,2).value = "250";
sheet.cells(3,2).value = "310";
sheet.cells(4,2).value = "80";
exceldemo.visible = true;
exceldemo.usercontrol = true;
oresizerange = sheet.range("b1:b4");
var chart = sheet.parent.charts.add();
chart.chartwizard(oresizerange, -4100, null, 2);
chart.seriescollection(1).xvalues = sheet.range("a1","a4");
chart.seriescollection(1).name = '月报';
chart.location(2, sheet.name);
sheet.shapes("chart 1").top = sheet.rows(10).top;
sheet.shapes("chart 1").left = sheet.columns(2).left;
}
</script>
<p><input id=button1 type=button value="生成excel" onclick="createexcel()"></p>
</body>
</html>