2023-04-12

Convert xlsx to csv with Node.js

Searching such a package directed to npm package 'xlsx'. However, this package has different repo name, sheetjs, and 'npm i xlsx' is not the recommended way (registry out of date). wth.

https://github.com/SheetJS/sheetjs

Install

npm i -D https://cdn.sheetjs.com/xlsx-0.19.2/xlsx-0.19.2.tgz

xlsx -> csv

import fs from "fs"
import { readFile, writeFile, set_fs } from "xlsx"
set_fs(fs) // still works without this line... idk
 
const wb = readFile("schools.xlsx")
writeFile(wb, "schools.csv", { bookType: "csv" })

My csv file had extra information on the first line. Here's a snippet to remove the first line from the csv file

const data = fs.readFileSync("schools.csv", "utf-8")
const data2 = data.split("\n").splice(1).join("\n")
fs.writeFileSync("schools.csv", data2)

Related

convert csv to json