Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exportToExcel(): void {
try {
/* generate worksheet */
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(this.data);
/* generate workbook and add the worksheet */
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
/* save to file */
XLSX.writeFile(wb, 'file.xlsx');
} catch (err) {
console.error('export error', err);
}
}
function get_file(file) {
const wb = make_book();
/* write using XLSX.writeFile */
XLSX.writeFile(wb, file);
process.send('wrote to ' + file + '\n');
}
workbook = {
SheetNames: [...new Set(excel.SheetNames)],
Sheets: Object.assign(excel.Sheets,{
[sheetName]: Object.assign({},{'!ref':ref},newSheet)
})
}
} else {
workbook = {
SheetNames: [sheetName],
Sheets: {
[sheetName]: Object.assign({},{'!ref':ref},newSheet)
}
}
}
try {
XLSX.writeFile(workbook,this.excel)
} catch(err){
if(err){
console.error(err)
reject();
}
}
resolve();
console.log('写入excel表格成功!')
});
})
var wb = fs.existsSync(filename) ? xlsx.readFile(filename) : new Workbook();
for (ws_name in o) {
var sheetdispname = sheetname || ws_name;
var twodarr = o[ws_name];
if (!twodarr[0]) continue;
wb.SheetNames.push(sheetdispname);
if (!twodarr[0].push)
twodarr = convertObjArray(twodarr);
var ws = sheet_from_array_of_arrays(twodarr);
wb.Sheets[sheetdispname] = ws;
if (process.env.debug)
console.log(filename, '/', sheetdispname);
}
xlsx.writeFile(wb, filename);
}
const write_opts: XLSX.WritingOptions = {
type: "buffer",
cellDates: false,
bookSST: false,
bookType: "xlsx",
sheet: "Sheet1",
compression: false,
Props: {
Author: "Someone",
Company: "SheetJS LLC"
}
};
const wb1 = XLSX.readFile("sheetjs.xls", read_opts);
XLSX.writeFile(wb1, "sheetjs.new.xlsx", write_opts);
read_opts.type = "binary";
const wb2 = XLSX.read("1,2,3\n4,5,6", read_opts);
write_opts.type = "binary";
const out2 = XLSX.write(wb2, write_opts);
read_opts.type = "buffer";
const wb3 = XLSX.read(fs.readFileSync("sheetjs.xlsx"), read_opts);
write_opts.type = "base64";
const out3 = XLSX.write(wb3, write_opts);
write_opts.type = "array";
const out4 = XLSX.write(wb3, write_opts);
const ws1 = XLSX.utils.aoa_to_sheet([
"SheetJS".split(""),
[1,2,3,4,5,6,7],
]).mergeCells("Main", {
"s": {"c": 0, "r": 0 },
"e": {"c": 2, "r": 0 }
})
.setColWidthChars('Main', 1,72)
.finalize();
//workbook.Sheets['Main']['!cols']=[{wch:24}]
var OUTFILE = '/tmp/wb.xlsx';
var OUTFILE1 = '/tmp/wb1.xlsx';
XLSX.writeFile(workbook, OUTFILE);
console.log("Results written to " + OUTFILE)
var workbook1 = XLSX.readFile(OUTFILE, {cellStyles: true, cellNF: true});
XLSX.writeFile(workbook1, OUTFILE1);
console.log("Results written to " + OUTFILE1)
});
const data = table.records.map((record) => {
return record._fields.map((field) => { return field._value; });
});
let wb = xlsx.utils.book_new();
const ws = xlsx.utils.json_to_sheet([headers].concat(data), {
'skipHeader': true
});
xlsx.utils.book_append_sheet(wb, ws);
try {
xlsx.writeFile(wb, options.outputFilePath);
}
catch (err) {
reject(err);
}
resolve();
});
};
export const export_array_to_excel = ({key, data, title, filename, autoWidth}) => {
const wb = XLSX.utils.book_new();
const arr = json_to_array(key, data);
arr.unshift(title);
const ws = XLSX.utils.aoa_to_sheet(arr);
if(autoWidth){
auto_width(ws, arr);
}
XLSX.utils.book_append_sheet(wb, ws, filename);
XLSX.writeFile(wb, filename + '.xlsx');
}
exportFile(type: 'csv' | 'xlsx', all = true) {
const wb = utils.book_new();
let ws: WorkSheet;
if (all) {
ws = utils.json_to_sheet(this.rows);
} else {
ws = utils.json_to_sheet(this.nzTable.data);
}
utils.book_append_sheet(wb, ws, 'Sheet1');
writeFile(wb, `export.${type}`);
}
case 'csv':
default: {
const csvString = csvFormatRows(array)
file = new File(
[csvString],
`${fileName}.${ext}`,
{ type: 'text/csv;charset=utf-8' }
)
FileSaver.saveAs(file)
break
}
case 'xlsx': {
const sheet = XLSX.utils.aoa_to_sheet(array, {header});
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, sheet, 'SheetJS');
XLSX.writeFile(wb, `${fileName}.${ext}`);
break
}
}
}