-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel.service.ts
77 lines (68 loc) · 2.09 KB
/
excel.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Injectable } from '@angular/core';
import {saveAs} from 'file-saver';
import { WorkBook, read, utils, write, readFile } from 'xlsx';
@Injectable()
export class FileExcelService {
wbout = [];
table = [];
ws: any;
constructor() {
this.setExcelProperties('');
}
s2ab(s) {
const buf = new ArrayBuffer(s.length);
const view = new Uint8Array(buf);
for (let i = 0; i !== s.length; ++i) {
view[i] = s.charCodeAt(i) & 0xFF;
};
return buf;
}
SaveToExcel(tableData, fileName: string= 'QuestionSheet') {
this.setTableData(tableData,fileName);
saveAs(new Blob([this.s2ab(this.wbout)], { type: 'application/octet-stream' }), fileName+'.xlsx');
}
getTableData()
{
return this.table;
}
setTableData(tableData, fileName: string)
{
this.table = tableData;
this.setExcelProperties(fileName);
}
// excel Detail
setExcelProperties(fileName: string)
{
const ws_name = fileName.substr(0, 25); //'QuestionSheet'
// const ws_name = ''; // worksheet name cannot exceed 31 chracters length
const wb: WorkBook = { SheetNames: [], Sheets: {} };
this.ws = utils.json_to_sheet(this.getTableData());
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = this.ws;
this.wbout = write(wb, { bookType: 'xlsx', bookSST: true, type: 'binary' });
}
convertExcelToJson(file)
{
let reader = new FileReader();
let workbookkk;
let XL_row_object;
let json_object;
reader.readAsBinaryString(file);
return new Promise((resolve, reject) => {
reader.onload = function(){
// alert(reader.result);
let data = reader.result;
workbookkk=read(data,{type: 'binary'});
console.log(workbookkk);
workbookkk.SheetNames.forEach(function(sheetName) {
// Here is your object
XL_row_object = utils.sheet_to_json(workbookkk.Sheets[sheetName]);
json_object = JSON.stringify(XL_row_object);
// console.log(json_object);
// console.log(XL_row_object);
resolve(XL_row_object);
});
};
});
}
}