当前位置:网站首页>How to merge tables when exporting excel tables with xlsx

How to merge tables when exporting excel tables with xlsx

2022-06-23 14:11:00 Swing a knife North

In the use of xlsx export excel When it comes to forms , Sometimes we need to merge certain tables , How to do it , The code is as follows :

import XLSX from 'xlsx';
// ...
// xlsxData  yes  Excel  The content of 
const workSheet = XLSX.utils.aoa_to_sheet(xlsxData);
const workbook = XLSX.utils.book_new();
//  Set the recording range of the worksheet 
// [ Column number ][ Line number ],A1  Then represent  A  In the column 1 That's ok 
//  The number of columns is generally known ( When unknown, it can be set to ZZ)
//  The number of rows is expressed in  xlsxData  The length of the content ends 
workSheet['!ref'] = `A1:AI${xlsxData.length}`;
// s  Meaning for  start , The starting cell 
// r  yes  row , Indicates line number , from  0  Count up 
// c  yes  col , Indicates column number , from  0  Count up 
const merge = [
  //  Vertical merger , The scope is the second 1 Row of column 1 Go to line 2
  { s: { r: 0, c: 0 }, e: { r: 1, c: 0 } },
  //  Vertical merger , The scope is the second 2 Row of column 1 Go to line 2
  { s: { r: 0, c: 1 }, e: { r: 1, c: 1 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 3 To column 5
  { s: { r: 0, c: 2 }, e: { r: 0, c: 4 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 6 To column 11
  { s: { r: 0, c: 5 }, e: { r: 0, c: 10 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 12 To column 17
  { s: { r: 0, c: 11 }, e: { r: 0, c: 16 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 18 To column 23
  { s: { r: 0, c: 17 }, e: { r: 0, c: 22 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 24 To column 29
  { s: { r: 0, c: 23 }, e: { r: 0, c: 28 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 30 To column 35
  { s: { r: 0, c: 29 }, e: { r: 0, c: 34 } }
];
workSheet['!merges'] = merge;
// sheet0  Is the name of the worksheet 
XLSX.utils.book_append_sheet(workbook, workSheet, 'sheet0');
//  Perform data conversion file writing 
XLSX.writeFileSync(workbook, ' Document name ', {
  bookType: 'xlsx',
  bookSST: true,
  type: 'array'
});

Decoding code , We use XLSX.utils.aoa_to_sheet After generating a table , You only need to set the ['!merges'] attribute , The settings are as follows :

const merge = [
  //  Vertical merger , The scope is the second 1 Row of column 1 Go to line 2
  { s: { r: 0, c: 0 }, e: { r: 1, c: 0 } },
  //  Vertical merger , The scope is the second 2 Row of column 1 Go to line 2
  { s: { r: 0, c: 1 }, e: { r: 1, c: 1 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 3 To column 5
  { s: { r: 0, c: 2 }, e: { r: 0, c: 4 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 6 To column 11
  { s: { r: 0, c: 5 }, e: { r: 0, c: 10 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 12 To column 17
  { s: { r: 0, c: 11 }, e: { r: 0, c: 16 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 18 To column 23
  { s: { r: 0, c: 17 }, e: { r: 0, c: 22 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 24 To column 29
  { s: { r: 0, c: 23 }, e: { r: 0, c: 28 } },
  //  Horizontal merger , The scope is the second 1 Columns of rows 30 To column 35
  { s: { r: 0, c: 29 }, e: { r: 0, c: 34 } }
];

This is an array , Each item in the array is an object , Object has two properties ,s and e, Represent the start and end, The values are one object ,r On behalf of the line ,c Representative column , It can be understood as coordinates , stay excel As long as the start and end coordinates of the region to be merged are determined, the merge is unknown .

The above is the use of xlsx export excel How to merge cells when using tables , I hope it helped you .

原网站

版权声明
本文为[Swing a knife North]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201071937245151.html