半年以前做过server端生成excel的简单引擎,总感觉不够轻便,尤其在一些固定格式,数据量又不是很大的情况下,上周写了一
个根据表单数据在client端用js生成excle的demo,令我我激动了半天------js太强了!
下面分享一下这段js:
1var excel = new ActiveXObject("Excel.Application"); //创建AX对象excel
2excel.visible =true; //设置excel可见属性
3var workbook = excel.Workbooks.Add; //获取workbook对象
4var sheet1 = xlBook.Worksheets(2); //创建sheet1
5var sheet2 = xlBook.Worksheets(1); //创建sheet2
6sheet1.Range(sheet1.Cells(1,1),sheet1.Cells(1,14)).mergecells=true; //合并单元格
7sheet1.Range(sheet1.Cells(1,1),sheet1.Cells(1,14)).value="员工月考核成绩"; //设置单元格内容
8sheet1.Range(sheet1.Cells(1,1),sheet1.Cells(1,14)).Interior.ColorIndex=6;//设置底色
9sheet1.Range(sheet1.Cells(1,1),sheet1.Cells(1,14)).Font.ColorIndex=5;//设置字体色
10sheet1.Rows(1).RowHeight = 20; //设置列高
11sheet1.Rows(1).Font.Size=16; //设置文字大小
12sheet1.Rows(1).Font.Name="宋体"; //设置字体
13//设置每一列的标题
14sheet1.Cells(2,1).Value="工程师考核项";
15sheet1.Cells(2,2).Value="总分";
16sheet1.Cells(2,3).Value="研发进度";
17sheet1.Cells(2,4).Value="出勤率";
18sheet1.Cells(2,5).Value="执行力";
19sheet1.Cells(2,6).Value="责任心";
20sheet1.Cells(2,7).Value="工作规范";
21sheet1.Cells(2,8).Value="协作精神";
22sheet1.Cells(2,9).Value="进取性";
23sheet1.Cells(2,10).Value="工作合理性";
24sheet1.Cells(2,11).Value="解决问题能力";
25sheet1.Cells(2,12).Value="应变能力";
26sheet1.Cells(2,13).Value="人际技能";
27sheet1.Cells(2,14).Value="理解能力";
28//从表单循环控件中取出数据逐行插入对应列的数据
29var count = sfform.GetAttributeValue('Repeat','Count');
30for(var line=1;line<=count;line++){ //begin for
31 var name = sfform.GetValue('Repeat['+line+'].name');
32 var total= sfform.GetValue('Repeat['+line+'].total');
33 var yfjd = sfform.GetValue('Repeat['+line+'].yfjd');
34 var jh = sfform.GetValue('Repeat['+line+'].jh');
35 var gcgj = sfform.GetValue('Repeat['+line+'].gcgj');
36 var cql = sfform.GetValue('Repeat['+line+'].cql');
37 var zxl = sfform.GetValue('Repeat['+line+'].zxl');
38 var gzgf = sfform.GetValue('Repeat['+line+'].gzgf');
39 var zrx = sfform.GetValue('Repeat['+line+'].zrx');
40 var xzjs = sfform.GetValue('Repeat['+line+'].xzjs');
41 var jqx = sfform.GetValue('Repeat['+line+'].jqx');
42 var gzhl = sfform.GetValue('Repeat['+line+'].gzh');
43 var jjwt = sfform.GetValue('Repeat['+line+'].jjwt');
44 var ybnl = sfform.GetValue('Repeat['+line+'].ybnl');
45 var rjjn = sfform.GetValue('Repeat['+line+'].rjjn');
46 var ljnl = sfform.GetValue('Repeat['+line+'].ljnl');
47 sheet1.Cells(2+line,1).Value=name;
48 sheet1.Cells(2+line,2).Value=total;
49 sheet1.Cells(2+line,3).Value=yfjd;
50 sheet1.Cells(2+line,4).Value=cql;
51 sheet1.Cells(2+line,5).Value=zxl;
52 sheet1.Cells(2+line,6).Value=gzgf;
53 sheet1.Cells(2+line,7).Value=zrx;
54 sheet1.Cells(2+line,8).Value=xzjs;
55 sheet1.Cells(2+line,9).Value=jqx;
56 sheet1.Cells(2+line,10).Value=gzhl;
57 sheet1.Cells(2+line,11).Value=jjwt;
58 sheet1.Cells(2+line,12).Value=ybnl;
59 sheet1.Cells(2+line,13).Value=rjjn;
60 sheet1.Cells(2+line,14).Value=ljnl;
61
62}//end for
63
64 基本的代码已经实现了,生成excel的格式和一些统计计算,用js写应该是很方便的,以后有例子再作补充。
从代码角度来看这种写法不是很灵活,但在能满足用户的需求前提下,这种生成方式还是很受欢迎的,给用户的感觉就是轻
便。个人认为简单就是美!