Skip to content
Execel JS실무 경험

How to Make \n Line Breaks Work in ExcelJS

1 min readWorkMoved from my Tistory blogRead the Korean original

First, I made a function to create cells easily.

ts
import ExcelJS from 'exceljs';

export default function ExcelText(
  worksheet: ExcelJS.Worksheet,
  cell: string,
  text: string,
  fontSize: number,
  vertical: any,
  horizontal: any,
  color: string = '',
  bold: boolean = false,
) {
  const cellRef = worksheet.getCell(cell);

  // Add the text
  cellRef.value = {
    richText: [
      {
        text: text,
        font: { size: fontSize, bold, color: { argb: color } },
      },
    ],
  };

  // Apply the styles
  cellRef.alignment = { vertical, horizontal };
}

Then, to make line breaks work in a cell, you need to apply a specific setting.

js
ExcelText(
    worksheet,
    `F${40 + itemLength}`,
    `3F, HANDOK BLDG., 2645, NAMBUSUNHWAN-RO, \nGANGNAM-GU, SEOUL, 06271, SOUTH KOREA`,
    10,
    'middle',
    'left',
    '',
  );
  worksheet.getCell(`E${40 + itemLength}`).alignment = { wrapText: true };
  • #ExcelJS
  • #\n

More in Work