Hi, sorry if this question have been answered somewhere else but i have an array of arrays that is perfectly exported to xlsx file. What I need is to merge the first 2 cells. to make a sort of title for the table
Here is my code so far :
exportFile() {
/* Format Raw Data reveived */
let formatedData = this.formatData();
/* convert state to workbook */
const ws = XLSX.utils.aoa_to_sheet(formatedData);
const wb = XLSX.utils.book_new();
wb = {
SheetNames:["sheet1"],
sheet1:{
"!merges":[
{ s:{r:0,c:0}, e:{r:0,c:1}}
]
}
}
XLSX.utils.book_append_sheet(wb, ws, this.state.sheetName);
/* generate XLSX file */
const wbout = XLSX.write(wb, { type: "binary", bookType: "xlsx" });
/* send to client */
saveAs(
new Blob([this.s2ab(wbout)], { type: "application/octet-stream" }),
this.state.fileName
);
}
Im using React btw
You need to build up the worksheet first, then add the merges to the worksheet, then add the worksheet to the workbook: https://jsfiddle.net/1ny97xrb/
/* notice the hole where cell "B1" would be */
var data = [
["Merged", "", "C", "D"],
[1,2,3,4],
["a","b","c","d"]
];
/* merge cells A1:B1 */
var merge = { s: {r:0, c:0}, e: {r:0, c:1} };
//var merge = XLSX.utils.decode_range("A1:B1"); // this is equivalent
/* generate worksheet */
var ws = XLSX.utils.aoa_to_sheet(data);
/* add merges */
if(!ws['!merges']) ws['!merges'] = [];
ws['!merges'].push(merge);
/* generate workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "sheet1");
/* generate file and download */
const wbout = XLSX.write(wb, { type: "array", bookType: "xlsx" });
saveAs(new Blob([wbout], { type: "application/octet-stream" }), "issue964.xlsx");
And if this project has been helpful for you, a star would show support :)
waw thanks for the quick answer !
@reviewher It works, thank you!
Amazing
Most helpful comment
You need to build up the worksheet first, then add the merges to the worksheet, then add the worksheet to the workbook: https://jsfiddle.net/1ny97xrb/
And if this project has been helpful for you, a star would show support :)