1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptでCSVダウンロードを実現する

Posted at

タイトルの通り

// ---------------------------------
// CSVダウンロード処理
// data : csvの中身(カンマ区切り、改行はセルフで)
// filaname : ダウンロードするファイル名
// ---------------------------------
const csvDownload = function(data, filename) {
    
    // UTF BOM
    var bom = new Uint8Array([0xEF, 0xBB, 0xBF]);
    // リンククリエイト
    var downloadLink = document.createElement("a");
    downloadLink.download = filename + ".csv";
    // ファイル情報設定
    downloadLink.href = URL.createObjectURL(new Blob([bom, data], { type: "text/csv" }));
    downloadLink.dataset.downloadurl = ["text/csv", downloadLink.download, downloadLink.href].join(":");
    // イベント実行
    downloadLink.click();
}

ajaxで書き込むデータ取得して、dataとして渡してあげる等で使ったり

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?