Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments/note can not export or export very slow, without comments, it will export very quick #167

Open
FulenQi opened this issue Mar 16, 2022 · 0 comments

Comments

@FulenQi
Copy link

FulenQi commented Mar 16, 2022

import { saveAs } from 'file-saver';
import XLSX from 'xlsx-style-tw';
// import XLSX from 'xlsx-style-tw';
// import XLSX from 'xlsx';
// import XLSX from 'xlsx-style-correct';

const cellDefaultHeight = 25;
const cellDefaultWidth = 10;
function Workbook() {
if (!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}

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 exportJsonToExcel({
multiHeader = [],
header,
data,
filename,
merges = [],
autoWidth = true,
autoHeight = true,
bookType = 'xlsx'
} = {}) {
/* original data */
filename = filename || 'excel';
const tempData = [];
if (data && data.length) {
data.forEach((elements) => {
const newDatas = [];
elements.forEach((data) => {
newDatas.push(data.value);
});
tempData.push(newDatas);
});
}
const newData = [...tempData];
if (header && header.length) {
newData.unshift(header);
}
if (multiHeader && multiHeader.length) {
newData.unshift(multiHeader);
}

var ws_name = "SheetJS";
var wb = new Workbook(),

ws = sheet_from_array_of_arrays(newData, data);
if (merges.length > 0) {
ws['!merges'] = merges;
}

if (autoWidth) {
const colWidth = newData.map(row => row.map(val => {
if (val == null) {
return {
'wch': cellDefaultWidth,
};
}
else if (val.toString().charCodeAt(0) > 255) {
return {
'wch': val.toString().length * 2
};
} else {
return {
'wch': val.toString().length
};
}
}))
let result = colWidth[0];
for (let i = 1; i < colWidth.length; i++) {
for (let j = 0; j < colWidth[i].length; j++) {
if (result[j]['wch'] < colWidth[i][j]['wch']) {
result[j]['wch'] = colWidth[i][j]['wch'];
}
}
}
ws['!cols'] = result;
}

if (autoHeight) {
const colHeight = data.map(row => row.map(val => {
if (val == null) {
return {
'hpx': cellDefaultHeight,
};
}
else if (val.toString().charCodeAt(0) > 255) {
return {
'hpx': cellDefaultHeight
};
} else {
return {
'hpx': cellDefaultHeight,
};
}
}));
/以第一行为初始值/
let result = colHeight[0];
for (let i = 1; i < colHeight.length; i++) {
for (let j = 0; j < colHeight[i].length; j++) {
if (result[j]['hpx'] < colHeight[i][j]['hpx']) {
result[j]['hpx'] = colHeight[i][j]['hpx'];
}
}
}
ws['!rows'] = result;
}

wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;

var wbout = XLSX.write(wb, {
bookType: bookType,
bookSST: false,
type: 'binary',
cellStyles: true,
raw:true,
});

saveAs(new Blob([s2ab(wbout)], {
type: "application/octet-stream"
}), ${filename}.${bookType});
}

function sheet_from_array_of_arrays(valueData, data) {
var ws = {};
var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
for(var R = 0; R != data.length; ++R) {
for(var C = 0; C != data[R].length; ++C) {
if(range.s.r > R) range.s.r = R;
if(range.s.c > C) range.s.c = C;
if(range.e.r < R) range.e.r = R;
if(range.e.c < C) range.e.c = C;
var item = data[R][C];
var tempCell = item.value;
// if(tempCell == null) continue;
if(tempCell == null) tempCell = '';
if(tempCell.f == undefined) {
var cell = {v: tempCell };
if(typeof cell.v === 'number') cell.t = 'n';
else if(typeof cell.v === 'boolean') cell.t = 'b';
else if(cell.v instanceof Date) {
cell.t = 'n'; cell.z = XLSX.SSF._table[14];
cell.v = datenum(cell.v);
}
else cell.t = 's';
} else {
var cell = tempCell;
}
var cell_ref = XLSX.utils.encode_cell({c:C,r:R});
cell.s = {
font: item.font,
alignment: { horizontal: "center", vertical: "center", wrap_text: true },
fill: { fgColor: { rgb: item.color }, bgColor: { rgb: 'bfaf00' } }
}
if(!cell.c && item.comment) {
cell.c = [];
cell.c.push({a:"Comment", t: item.comment });
cell.c.hidden = true;
}
ws['row'] = 50;
ws[cell_ref] = cell;
}
}
if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
}

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant