LoginSignup
0
0

More than 1 year has passed since last update.

Node.jsでExcelファイルのデータを取得する

Last updated at Posted at 2022-12-31

触る機会がありましたので、忘れないようにまとめておきます。

ファイル構成

└── test
    ├── Book1.xlsx
    ├── package.json
    └── excel.js

作成したファイル

Book1.xlsx

日付 名前
2022/4/11 河森
2022/4/17 田中
2022/4/29 山田
2022/5/6 新岡
2022/5/19 満島
2022/6/5 阿島

excel.js

excel.js
const xlsx = require('xlsx');

// エクセルファイル
const excelFile = xlsx.readFile('./Book1.xlsx');
const shName = 'Sheet1';
let sh = excelFile.Sheets[shName];

// A1の記述を取得する
const a1 = sh['A1'].w.replace(/(\t|\n)/g, '');
console.log(a1);
/* 出力結果
  日付
 */

// シートをまるごと取得し、json化する
const sheet1 = xlsx.utils.sheet_to_json(sh);
console.log(sheet1);
/* 出力結果
  [
    { '日付': 44662, '名前': '河森' },
    { '日付': 44668, '名前': '田中' },
    { '日付': 44680, '名前': '山田' },
    { '日付': 44687, '名前': '新岡' },
    { '日付': 44700, '名前': '満島' },
    { '日付': 44717, '名前': '阿島' }
  ]
 */

困ったこと

xlsx導入時にエラーが出た

npm install xlsx実行時に以下のエラーが出た

npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY
npm ERR! errno UNABLE_TO_GET_ISSUER_CERT_LOCALLY
npm ERR! request to https://registry.npmjs.org/xlsx failed, reason: unable to get local issuer certificate

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/xxx/.npm/_logs/2022-12-20T16_09_00_486Z-debug.log

以下のコマンドで解決しました

npm config set registry http://registry.npmjs.org/ 
0
0
2

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