Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

この投稿 の続き

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}
      };
    });
  });
kai-punipuni
このサイトの掲載内容は私自身の見解であり、必ずしも所属会社の立場、戦略、意見を代表するものではありません。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away