3
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 5 years have passed since last update.

Node.jsでエクセルファイルを書き込み

Last updated at Posted at 2017-12-24

この投稿 の続き

Node.jsでエクセルを扱いたい。「ya-csv」でcsvを読み書きするみたいにエクセルを読み書きしたい。
今度は書き込む。使うのは同じく exceljs

exports.writeOnXlsx=(filePath, sheetName, dataArr)=>{
    const workbook = new excel.Workbook();
    workbook.creator = "作者名";
    workbook.created = new Date();
    const sheet =  workbook.addWorksheet(sheetName);

    dataArr.forEach((data, rowIndex)=>{
      sheet.addRow(data);
      //data.forEach((d, colIndex)=>{
      //  const targetCell = sheet.getCell(rowIndex+1, colIndex+1);
      //});
    });

    workbook.xlsx.writeFile(filePath).then(()=>{
      console.log('written down=> ' + filePath);
    });
};

読むより書くほうが楽。

フォントも変えられる。
以下だと、フォント:Meiryo UI, サイズ:12

  dataArr.forEach((data, rowIndex)=>{
    sheet.addRow(data);
    data.forEach((d, colIndex)=>{
      const targetCell = sheet.getCell(rowIndex+1, colIndex+1);

      targetCell.style.font ={
        size: 12,
        name: "Meiryo UI"
      };

    });
  });

セルの色も変えられる。
以下だと、黄色塗りつぶし

  dataArr.forEach((data, rowIndex)=>{
    sheet.addRow(data);
    data.forEach((d, colIndex)=>{
      const targetCell = sheet.getCell(rowIndex+1, colIndex+1);

      targetCell.style.fill ={
        stype: "pattern",
        pattern: "solid",
        fgColor: {argb: "FFFFFF00"}
        //赤にする
                //fgColor: {theme: 5}
      };
    });
  });
3
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
3
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?