|
@@ -4,7 +4,7 @@ require('script-loader!file-saver');
|
|
|
require('./Blob.js');//转二进制用 这边要写你的blob的实际地址
|
|
|
require('script-loader!xlsx/dist/xlsx.core.min');
|
|
|
|
|
|
-function generateArray (table) {
|
|
|
+function generateArray(table) {
|
|
|
var out = [];
|
|
|
var rows = table.querySelectorAll('tr');
|
|
|
var ranges = [];
|
|
@@ -54,13 +54,13 @@ function generateArray (table) {
|
|
|
return [out, ranges];
|
|
|
};
|
|
|
|
|
|
-function datenum (v, date1904) {
|
|
|
+function datenum(v, date1904) {
|
|
|
if (date1904) v += 1462;
|
|
|
var epoch = Date.parse(v);
|
|
|
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
|
|
|
}
|
|
|
|
|
|
-function sheet_from_array_of_arrays (data, opts) {
|
|
|
+function sheet_from_array_of_arrays(data, opts) {
|
|
|
var ws = {};
|
|
|
var range = {
|
|
|
s: {
|
|
@@ -102,29 +102,27 @@ function sheet_from_array_of_arrays (data, opts) {
|
|
|
return ws;
|
|
|
}
|
|
|
|
|
|
-function Workbook () {
|
|
|
+function Workbook() {
|
|
|
if (!(this instanceof Workbook)) return new Workbook();
|
|
|
this.SheetNames = [];
|
|
|
this.Sheets = {};
|
|
|
}
|
|
|
|
|
|
-function s2ab (s) {
|
|
|
+function s2ab(s) {
|
|
|
var buf = new ArrayBuffer(s.length);
|
|
|
var view = new Uint8Array(buf);
|
|
|
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
|
|
|
return buf;
|
|
|
}
|
|
|
|
|
|
-export function export_table_to_excel (id) {
|
|
|
+export function export_table_to_excel(id) {
|
|
|
var theTable = document.getElementById(id);
|
|
|
- console.log('a')
|
|
|
var oo = generateArray(theTable);
|
|
|
var ranges = oo[1];
|
|
|
|
|
|
/* original data */
|
|
|
var data = oo[0];
|
|
|
var ws_name = "风机数据";
|
|
|
- console.log(data);
|
|
|
|
|
|
var wb = new Workbook(),
|
|
|
ws = sheet_from_array_of_arrays(data);
|
|
@@ -148,10 +146,9 @@ export function export_table_to_excel (id) {
|
|
|
}), "test.xlsx")
|
|
|
}
|
|
|
|
|
|
-function formatJson (jsonData) {
|
|
|
- console.log(jsonData)
|
|
|
+function formatJson(jsonData) {
|
|
|
}
|
|
|
-export function export_json_to_excel (th, jsonData, defaultTitle) {
|
|
|
+export function export_json_to_excel(th, jsonData, defaultTitle) {
|
|
|
|
|
|
/* original data */
|
|
|
|
|
@@ -178,51 +175,100 @@ export function export_json_to_excel (th, jsonData, defaultTitle) {
|
|
|
}), title + ".xlsx")
|
|
|
};
|
|
|
|
|
|
-export function export_blob (th, jsonData,defaultTitle) {
|
|
|
+export function export_blob(th, jsonData, defaultTitle) {
|
|
|
var data = jsonData;
|
|
|
data.unshift(th);
|
|
|
var ws_name = "风机数据";
|
|
|
var wb = new Workbook(),
|
|
|
- ws = sheet_from_array_of_arrays(data);
|
|
|
+ ws = sheet_from_array_of_arrays(data);
|
|
|
/* add worksheet to workbook */
|
|
|
wb.SheetNames.push(ws_name);
|
|
|
wb.Sheets[ws_name] = ws;
|
|
|
- var wbout = XLSX.write(wb, {bookType: 'xlsx',bookSST: false,type: 'binary'});
|
|
|
-
|
|
|
- if(defaultTitle){//只渲染一个excel
|
|
|
- saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}),defaultTitle+".xlsx")
|
|
|
- }else{//返回多个blob
|
|
|
- var aaa = new Blob([s2ab(wbout)], {type: 'text/plain'});
|
|
|
- return aaa;
|
|
|
+ var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' });
|
|
|
+
|
|
|
+ if (defaultTitle) {//只渲染一个excel
|
|
|
+ saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), defaultTitle + ".xlsx")
|
|
|
+ } else {//返回多个blob
|
|
|
+ var aaa = new Blob([s2ab(wbout)], { type: 'text/plain' });
|
|
|
+ return aaa;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
export function export_more_blob(th, jsonData, defaultTitle) {
|
|
|
- var data = jsonData;
|
|
|
- //添加标题
|
|
|
- for (var i = 0; i < th.length; i++) {
|
|
|
- data[i].unshift(th[i])
|
|
|
- }
|
|
|
- //这里是定义sheet的名称 有几个sheet就加几个
|
|
|
- var ws_name = ["日上网电量","上网功率","测风塔数据"];
|
|
|
- var wb = new Workbook(),ws=[];
|
|
|
- //数据转换
|
|
|
- for (var j = 0; j < th.length; j++) {
|
|
|
- ws.push(sheet_from_array_of_arrays(data[j]))
|
|
|
- }
|
|
|
- /* add worksheet to workbook */
|
|
|
- //生成多个sheet
|
|
|
- for (var k = 0; k < th.length; k++) {
|
|
|
- wb.SheetNames.push(ws_name[k])
|
|
|
- wb.Sheets[ws_name[k]] = ws[k]
|
|
|
- }
|
|
|
-
|
|
|
- var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'});
|
|
|
-
|
|
|
- if(defaultTitle){//只渲染一个excel
|
|
|
- saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}),defaultTitle+".xlsx")
|
|
|
- }else{//返回多个blob
|
|
|
- var aaa = new Blob([s2ab(wbout)], {type: 'text/plain'});
|
|
|
- return aaa;
|
|
|
- }
|
|
|
-}
|
|
|
+ var data = jsonData;
|
|
|
+ //添加标题
|
|
|
+ for (var i = 0; i < th.length; i++) {
|
|
|
+ data[i].unshift(th[i])
|
|
|
+ }
|
|
|
+ //这里是定义sheet的名称 有几个sheet就加几个
|
|
|
+ var ws_name = ["日上网电量", "上网功率", "测风塔数据"];
|
|
|
+ var wb = new Workbook(), ws = [];
|
|
|
+ //数据转换
|
|
|
+ for (var j = 0; j < th.length; j++) {
|
|
|
+ ws.push(sheet_from_array_of_arrays(data[j]))
|
|
|
+ }
|
|
|
+ /* add worksheet to workbook */
|
|
|
+ //生成多个sheet
|
|
|
+ for (var k = 0; k < th.length; k++) {
|
|
|
+ wb.SheetNames.push(ws_name[k])
|
|
|
+ wb.Sheets[ws_name[k]] = ws[k]
|
|
|
+ }
|
|
|
+
|
|
|
+ var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' });
|
|
|
+
|
|
|
+ if (defaultTitle) {//只渲染一个excel
|
|
|
+ saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), defaultTitle + ".xlsx")
|
|
|
+ } else {//返回多个blob
|
|
|
+ var aaa = new Blob([s2ab(wbout)], { type: 'text/plain' });
|
|
|
+ return aaa;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function exportMultiple(thArray, dataArray, sheetNameArray, fileName = "导出的Excel") {
|
|
|
+
|
|
|
+ let ws_name = sheetNameArray;
|
|
|
+ let wb = new Workbook();
|
|
|
+ let ws = [];
|
|
|
+ let filterArray = [];
|
|
|
+
|
|
|
+ const formatJson = function (filterVal, jsonData) {
|
|
|
+ return jsonData.map(v => filterVal.map(j => v[j]));
|
|
|
+ };
|
|
|
+
|
|
|
+ thArray.forEach((pEle, pIndex) => {
|
|
|
+ ws.push([]);
|
|
|
+ dataArray[pIndex].unshift(pEle);
|
|
|
+ filterArray.push([]);
|
|
|
+ pEle.forEach(cEle => {
|
|
|
+ filterArray[pIndex].push(cEle.field);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ thArray.forEach((pEle, pIndex) => {
|
|
|
+ const data = formatJson(filterArray[pIndex], dataArray[pIndex]);
|
|
|
+ ws[pIndex].push(sheet_from_array_of_arrays(data));
|
|
|
+ wb.SheetNames.push(ws_name[pIndex]);
|
|
|
+ wb.Sheets[ws_name[pIndex]] = ws[pIndex][0];
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ let cellKes = []
|
|
|
+ for (var i = 0; i < 26; i++) {
|
|
|
+ cellKes.push(String.fromCharCode(65 + i));
|
|
|
+ }
|
|
|
+
|
|
|
+ let idx = 0;
|
|
|
+ ws.forEach((pEle, pIndex) => {
|
|
|
+ idx = 0
|
|
|
+ thArray[pIndex].forEach(thEle => {
|
|
|
+ pEle.forEach(cEle => {
|
|
|
+ cEle[cellKes[idx] + 1] = { v: thEle.name, t: "s" }
|
|
|
+ idx++;
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' });
|
|
|
+ saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), fileName + ".xlsx");
|
|
|
+
|
|
|
+}
|