Json数据导出生成Excel

  最近在做一个导入导出Excel的功能,导出其他类型的文件都比较熟悉,但是导入跟导出一个Excel还是稍微特殊点。根据这次的经验,写了个导出的小样例。

  总体思路就是json数据的key,value跟Excel的行列转换,还有就是解决数据在Excel表格中存放的位置,区域问题。

  这里要用到的两个小插件,一个是xslx.js,一个是FileSaver.js,前者是来处理生成Excel的,后者是用来把文件下载保存到本地的。

下载地址:

https://github.com/eligrey/FileSaver.js

https://github.com/SheetJS/js-xlsx/tree/028d7010a516383cb9a2fdd0f0a919392e77600a/demos/angular

 1 <!DOCTYPE html>
 2 <html>
 3 
 4 <head>
 5     <meta charset="UTF-8">
 6     <title>导出生成excel</title>
 7     <script src="js/xlsx.full.min.js"></script>
 8     <script src="js/FileSaver.min.js"></script>
 9 </head>
10 
11 <body>
12     <button onclick="downloadExl(students)">导出</button>
13     <script>
14         var students = [{
15             "name": "小明1",
16             "age": "6",
17             "sex": "",
18             "height": "60"
19         }, {
20             "name": "小明2",
21             "age": "7",
22             "sex": "",
23             "height": "70"
24         }, {
25             "name": "小明3",
26             "age": "8",
27             "sex": "",
28             "height": "80"
29         }];
30         function downloadExl(data, type) {
31 
32             var keys = Object.keys(data[0]);
33             var firstRow = {};
34             keys.forEach(function (item) {
35                 firstRow[item] = item;
36             });
37             data.unshift(firstRow);
38 
39             var content = {};
40 
41             // 把json格式的数据转为excel的行列形式
42             var sheetsData = data.map(function (item, rowIndex) {
43                 return keys.map(function (key, columnIndex) {
44                     return Object.assign({}, {
45                         value: item[key],
46                         position: (columnIndex > 25 ? getCharCol(columnIndex) : String.fromCharCode(65 + columnIndex)) + (rowIndex + 1),
47                     });
48                 });
49             }).reduce(function (prev, next) {
50                 return prev.concat(next);
51             });
52 
53             sheetsData.forEach(function (item, index) {
54                 content[item.position] = { v: item.value };
55             });
56 
57             //设置区域,比如表格从A1到D10,SheetNames:标题,
58             var coordinate = Object.keys(content);
59             var workBook = {
60                 SheetNames: ["helloSheet"],
61                 Sheets: {
62                     "helloSheet": Object.assign({}, content, { "!ref": coordinate[0] + ":" + coordinate[coordinate.length - 1] }),
63                 }
64             };
65             //这里的数据是用来定义导出的格式类型
66             var excelData = XLSX.write(workBook, { bookType: "xlsx", bookSST: false, type: "binary" });
67             var blob = new Blob([string2ArrayBuffer(excelData)], { type: "" });
68             saveAs(blob, "hello.xlsx");
69         }
70         //字符串转字符流
71         function string2ArrayBuffer(s) {
72             var buf = new ArrayBuffer(s.length);
73             var view = new Uint8Array(buf);
74             for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
75             return buf;
76         }
77         // 将指定的自然数转换为26进制表示。映射关系:[0-25] -> [A-Z]。
78         function getCharCol(n) {
79             let temCol = "",
80                 s = "",
81                 m = 0
82             while (n > 0) {
83                 m = n % 26 + 1
84                 s = String.fromCharCode(m + 64) + s
85                 n = (n - m) / 26
86             }
87             return s
88         }
89     </script>
90 </body>
91 
92 </html>

 

 

 

posted @ 2017-12-22 00:04  绝对小孩  阅读(16168)  评论(4编辑  收藏  举报