最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reactjs - Export to Excel using xlsx.utils.json_to_sheet - Stack Overflow

programmeradmin0浏览0评论

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.

  1. Header should be bold.
  2. Header background should be gray
  3. 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.

  1. Header should be bold.
  2. Header background should be gray
  3. 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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

You 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.

发布评论

评论列表(0)

  1. 暂无评论