Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sheetjs xlsx, How to write merged cells?

I need to create a xlsx with merged cells using sheetjs.

data:

[
  {
    "id": "nick",
    "items": [
      {
        "name": "ball"
      },
      {
        "name": "phone"
      }
    ]
  },
  {
    "id": "jack",
    "items": [
      {
        "name": "pen"
      },
      {
        "name": "doll"
      }
    ]
  }
]

My code:

var ws = XLSX.utils.json_to_sheet(data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "");
var wbout = XLSX.write(wb, {bookType:'xlsx', type:'array'});
saveAs(new Blob([wbout],{type:"application/octet-stream"}), filename + ".xlsx");

The result I want to get:

result

How do I get this result?

... Thank you

like image 522
chacker Avatar asked Nov 28 '18 09:11

chacker


People also ask

How do you write text in merged cells in Excel?

Right-click and then select "Format Cells" from the popup menu. When the Format Cells window appears, select the Alignment tab. Check the "Wrap text" checkbox. Now when you return to the spreadsheet, you will need to manually adjust the height of the row that contains the merged cells.

How do I merge cells in Xlsx?

Highlight or select a range of cells. Right-click on the highlighted cells and select Format Cells.... Click the Alignment tab and place a checkmark in the checkbox labeled Merge cells.

How do I format cells in a merged table?

In the table, drag the pointer across the cells that you want to merge. Click the Layout tab. In the Merge group, click Merge Cells.

How do I create a merged cell in Excel?

You can combine two or more table cells located in the same row or column into a single cell. For example, you can merge several cells horizontally to create a table heading that spans several columns. Select the cells that you want to merge. Under Table Tools, on the Layout tab, in the Merge group, click Merge Cells.


1 Answers

const merge = [
  { s: { r: 1, c: 0 }, e: { r: 2, c: 0 } },{ s: { r: 3, c: 0 }, e: { r: 4, c: 0 } },
];
ws["!merges"] = merge;

Use this code for merge A2:A3 ({ s: { r: 1, c: 0 }, e: { r: 2, c: 0 } }) and A4:A5 ({ s: { r: 3, c: 0 }, e: { r: 4, c: 0 } })

Here s = start, r = row, c=col, e= end

like image 136
Amjad Rehman A Avatar answered Oct 04 '22 16:10

Amjad Rehman A