admin管理员组文章数量:1355758
I am using xlsx npm package to export data to excel. The following code is working as expected and data is exported. I need to apply some style as below. Please help.
- Header should be bold.
- Header background should be gray
- Apply border
Import statement
import * as XLSX from 'xlsx';
Code:
var data = [
{"name":"John", "city": "Seattle"},
{"name":"Mike", "city": "Los Angeles"},
{"name":"Zach", "city": "New York"}
];
let header = ["Name", "City"];
const ws = XLSX.utils.book_new();
XLSX.utils.sheet_add_aoa(ws, [header]);
XLSX.utils.sheet_add_json(ws, data, { origin: 'A2', skipHeader: true });
const wb = { Sheets: { 'data': ws }, SheetNames: ['data'] };
const excelBuffer = XLSX.write(wb, { bookType: fileType, type: 'array', cellStyles:true });
const finalData = new Blob([excelBuffer], { type: fileFormat });
FileSaver.saveAs(finalData, "Data.xlsx");
Actual Output:
Expected Output:
I am using xlsx npm package to export data to excel. The following code is working as expected and data is exported. I need to apply some style as below. Please help.
- Header should be bold.
- Header background should be gray
- Apply border
Import statement
import * as XLSX from 'xlsx';
Code:
var data = [
{"name":"John", "city": "Seattle"},
{"name":"Mike", "city": "Los Angeles"},
{"name":"Zach", "city": "New York"}
];
let header = ["Name", "City"];
const ws = XLSX.utils.book_new();
XLSX.utils.sheet_add_aoa(ws, [header]);
XLSX.utils.sheet_add_json(ws, data, { origin: 'A2', skipHeader: true });
const wb = { Sheets: { 'data': ws }, SheetNames: ['data'] };
const excelBuffer = XLSX.write(wb, { bookType: fileType, type: 'array', cellStyles:true });
const finalData = new Blob([excelBuffer], { type: fileFormat });
FileSaver.saveAs(finalData, "Data.xlsx");
Actual Output:
Expected Output:
Share Improve this question edited Mar 29, 2021 at 11:58 Ayaz asked Mar 29, 2021 at 11:21 AyazAyaz 2,1314 gold badges17 silver badges20 bronze badges1 Answer
Reset to default 4You cannot do this with xlsx
aka SheetJS
because it is probably a SheetJS Pro
feature.
You can do it with xlsx-populate.
Demo at CodeSandbox.
本文标签: javascriptReactjsExport to Excel using xlsxutilsjsontosheetStack Overflow
版权声明:本文标题:javascript - Reactjs - Export to Excel using xlsx.utils.json_to_sheet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743972790a2570756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论