[개발일기] 엑셀로 데이터 내려받기 (json to excel)

Keunyeong Lee·2022년 4월 26일
0

[개발일기]

목록 보기
14/14
post-thumbnail

Json to Excel

json 데이터를 정리하여 excel 로 내려받기

npm install write-excel-file

활용

구조

const data = [
  // Row #1
  [
    // Column #1
    {
      value: "Name",
      fontWeight: "bold",
    },
    // Column #2
    {
      value: "Date of Birth",
      fontWeight: "bold",
    },
    // Column #3
    {
      value: "Cost",
      fontWeight: "bold",
    },
    // Column #4
    {
      value: "Paid",
      fontWeight: "bold",
    },
  ],
  // Row #2
  [
    // Column #1
    {
      // `type` is optional
      type: String,
      value: "John Smith",
    },
    // Column #2
    {
      // `type` is optional
      type: Date,
      value: new Date(),
      format: "mm/dd/yyyy",
    },
    // Column #3
    {
      // `type` is optional
      type: Number,
      value: 1800,
    },
    // Column #4
    {
      // `type` is optional
      type: Boolean,
      value: true,
    },
  ],
  // Row #3
  [
    // Column #1
    {
      // `type` is optional
      type: String,
      value: "Alice Brown",
    },
    // Column #2
    {
      // `type` is optional
      type: Date,
      value: new Date(),
      format: "mm/dd/yyyy",
    },
    // Column #3
    {
      // `type` is optional
      type: Number,
      value: 2600,
    },
    // Column #4
    {
      // `type` is optional
      type: Boolean,
      value: false,
    },
  ],
];

다운로드 동작 함수

const excelWirte = async (data) => {
    await writeXlsxFile(data, {
      fileName: "file.xlsx", // 파일명
    });
  };

만들어진 함수를 실행시키면 data를 갖는 지정된 파일명으로 excel 파일을 다운로드 하게 된다!!

profile
🏃🏽 동적인 개발자

0개의 댓글