如何将XLSX转换为JSON从FTP读取它?
我正在尝试从FTP下载.xlsx
文件,然后使用 XSLX 模块将其转换为.json
,然后编写它到文件。 All this using Node.js
const fs = require('fs');
const Client = require('ftp');
const XLSX = require('xlsx');
const c = new Client();
c.connect({
host: '***.***.***.**',
user: '*********',
password: '*********',
});
c.on('ready', () => {
c.get('/market.xlsx', (err, stream) => {
if (err) throw err;
stream.once('close', () => c.end());
let content = '';
stream.on('data', (chunk) => {
content += chunk;
});
stream.on('end', () => {
//I guess something is wrong here
const json = XLSX.utils.sheet_to_json(content);
fs.writeFileSync(
'./files/market-to-json.json',
JSON.stringify(json, null, 2),
'utf-8'
);
});
});
});
My actual output in .json
[]
I struggled with this for a week and can't find solution, please help!.
I'm trying to download a .xlsx
file from a ftp, and converting it to a .json
using XSLX module, and then writing it to a file. All this using Node.js
const fs = require('fs');
const Client = require('ftp');
const XLSX = require('xlsx');
const c = new Client();
c.connect({
host: '***.***.***.**',
user: '*********',
password: '*********',
});
c.on('ready', () => {
c.get('/market.xlsx', (err, stream) => {
if (err) throw err;
stream.once('close', () => c.end());
let content = '';
stream.on('data', (chunk) => {
content += chunk;
});
stream.on('end', () => {
//I guess something is wrong here
const json = XLSX.utils.sheet_to_json(content);
fs.writeFileSync(
'./files/market-to-json.json',
JSON.stringify(json, null, 2),
'utf-8'
);
});
});
});
My actual output in .json
[]
I struggled with this for a week and can't find solution, please help!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sheet_to_json
需要一个工作表对象,因此您实际上需要先读取内容,然后传递所需的工作表,然后尝试将内容读取作为缓冲区,然后通过所需的表格完成:
sheet_to_json
requires a worksheet object, so you actually need to read the contents first, and then pass the desired worksheettry reading the contents as a buffer, and then pass the desired sheet once the stream is finished: