Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rearranging the XLSX.write order #2804

Open
HarshaKosala opened this issue Sep 30, 2022 · 1 comment
Open

Rearranging the XLSX.write order #2804

HarshaKosala opened this issue Sep 30, 2022 · 1 comment

Comments

@HarshaKosala
Copy link

Hi is it possible to write the output to the excel file as shown below? Have the headers in one column (A1) and data in another (B1).

image

Rather than the normal behavior as shown below
image

@itsnoa04
Copy link

itsnoa04 commented Apr 7, 2023

Hey, its possible..

import * as XLSX from "xlsx";

// create a new workbook and worksheet
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.aoa_to_sheet([[]]);

// define the headers and data
const headers: string[] = ["Header1", "Header2", "Header3"];
const data: string[][] = [
  ["Data1", "Data2", "Data3"],
  ["Data4", "Data5", "Data6"],
  ["Data7", "Data8", "Data9"],
];

// add the headers to the worksheet
XLSX.utils.sheet_add_aoa(
  worksheet,
  headers.map((header) => [header]),
  { origin: "A1" }
);

// add the data to the worksheet
XLSX.utils.sheet_add_aoa(worksheet, data, { origin: "B1" });

// add the worksheet to the workbook
XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");

// write the workbook to a file
XLSX.writeFile(workbook, "output.xlsx");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants