- Mainly use itbetter-xlsxfile-savertwo libraries
- yarn add better-xlsx file-saver
Import {file} from 'better-xlsx';
Import {Saveas} from 'file-saver';
Function Exportexcel (Column, DataSource, FILENAME = 'Example') {{
// New working spectrum
const file = new file ();
// New table
let sheet = file.addsheet ('sheet-test');
// Get the number of heads
let depth = getdepth (colorn);
// Get the number of columns of the header
let colornum = getcolumns (colorn);
//
let rowarr = [];
for (let k = 0; k <depth; k ++) {
rowarr.push (sheet.adddrow ());
}
// Fill in the charging unit according to the number of columns
rowarr.map (eLE => {{
for (let j = 0; j <columnnum; j ++) {
let cell = ele.addcell ();
cell.value = j;
}
});
// Initialize the header
Init (column, 0, 0);
// Ping the column in order
let colornlinearr = [];
columnline (colorn);
// Based on color, sort the data in DataAource and convert it into a two -dimensional array
let datasourcearr = [];
dataSource.map (eLE => {
let datatemp = [];
columnlinearr.map (item => {{
datatemp.push ({
[item.dataINDEX]: ele [item.dataindex],
Value: ELE [Item.dataINDEX],
});
});
DataSourcearr.push (datatemp);
});
// debugger;
// Painting form data
DataSourcearr.Foreach (item, index) => {{
// Create the corresponding number of rows according to data
let row = sheet.addrow ();
row.Setheightcm (0.8);
// Create the corresponding number of cells
item.map (eLE => {{
let cell = row.addcell ();
if (Ele.hasownproperty ('NUM')) {{
cell.value = index + 1;
} Else {
cell.value = ele.value;
}
cell.style.align.v = 'Center';
cell.style.align.h = 'Center';
});
});
// Set the width of each column
for (var I = 0; i <4; i ++) {
sheet.col (i). Width = 20;
}
file.saveas ('blob'). Then (function (content) {
Saveas (content, filename + '.xlsx');
});
// Ping the column in order
Function colornline (column) {{
column.map (eLE => {{
If (Ele.children === Undefined || Ele.children.Length === 0) {{
columnlinearr.push (eLE);
} Else {
columnline (ele.children);
}
});
}
// Initialize the header
Function Init (Column, RowIndex, ColumNindex) {{
column.map ((Item, index) => {
Let HCell = Sheet.cell (Rowindex, ColumNindex);
// If there is no sub -element, full column
if (item.tital === 'operation') {{
hcell.value = '';
Return;
} Else if (item.children === Undefined || item.children.length === 0) {{
// Add a cell in the first line
hcell.value = item.title;
hcell.vmerge = depth -rowindex -1;
hcell.style.align.h = 'Center';
hcell.style.align.v = 'Center';
columNindex ++;
// rowindex ++
} Else {
Let Childrennum = 0;
Function getcolumns (ARR) {
Arr.Map (ELE => {
if (ele.children) {
getcolumns (ele.children);
} Else {
Childrennum ++;
}
});
}
getcolumns (item.children);
hcell.hmerge = childrennnum -1;
hcell.value = item.title;
hcell.style.align.h = 'Center';
hcell.style.align.v = 'Center';
let rowcopy = rowindex;
rowcopy ++;
init (item.children, rowcopy, columNindex);
// The starting point of the next cell
columNindex = columNindex + Childrennum;
}
});
}
// Get the header ROWS
Function getdepth (ARR) {
const eledepths = [];
Arr.Foreach (ELE => {{
let depth = 0;
If (Array.isarray (Ele.Children)) {{
depth = getdepth (ele.children);
}
eledepths.push (depth);
});
Return 1 + MAX (ELEDEPTHS);
}
Function Max (ARR) {
Return arr.ReDuce ((accu, curr) => {{
if (Curr> Accu) Return Curr;
Return accu;
});
}
// Calculate the number of header columns
Function getcolumns (ARR) {
Let columnnum = 0;
Arr.Map (ELE => {
if (ele.children) {
getcolumns (ele.children);
} Else {
columnnum ++;
}
});
Return columnnum;
}
}
export default exportexcel;