0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Angular】Excelのダウンロード機能を実装!

Last updated at Posted at 2024-08-09

Angularって情報少ないよなっていつも思う泉(@izumin_0401)です。

ブログ記事はこちら

AngularでExcelをダウンロードしたい

早速ですが、AngularでExcelのダウンロード機能を実装してみましたよん。

Excelのダウンロード機能を実装する手順

「xlsx」のインストール

$ npm install xlsx

ソースコード

import * as XLSX from 'xlsx';

.
.
.

download() {
  const header = [
    "ウンコ",
    "ウンコ数"
  ];

  const data = [
    [
      "大きいウンコ",
      1
    ],
    [
      "小さいウンコ",
      2
    ]
  ];

  // ヘッダを先頭に追加
  data.unshift(header);

  // aoa_to_sheetはデータが配列の配列の場合に使う
  const workSheet: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(data);
  const workBook: XLSX.WorkBook = XLSX.utils.book_new();
  
  XLSX.utils.book_append_sheet(workBook, workSheet, 'sheet1');
  XLSX.writeFile(workBook, 'unko.xlsx');
}

上記のコードでExcelファイルをダウンロードできます。

意外と簡単だった。

まとめ

なんでExcelをダウンロードしたいのかって?

...大人の事情だよ!!

最後に

暇つぶしにTwitterブログもやってるので見てね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?