解决node.js读取.csv文件中文出现乱码问题

问题:使用nestjs,读取csv文件数据,获取到的中文是乱码

原因:数据生成时是GBK编码,nodejs原生读取文件不支持GBK

解决:使用iconv-lite库

示例:

const fs = require('fs');
// filePath为文件路径
const filePath = 'D:/demo.csv';
const stream = fs.createReadStream(filePath, { encoding: 'binary' });
let data = '';
stream.on('error', err => {
  console.error('读取行错误');
  console.error(err);
});
stream.on('data', item => {
  data += item;
});
stream.on('end', () => {
  const buf = Buffer.from(data, 'binary');
  // 获得正常的字符串,没有乱码
  const str = iconv.decode(buf, 'GBK');
});

业务中使用:

import { Injectable } from '@nestjs/common';
const xlsx2json = require('node-xlsx');
const iconv = require('iconv-lite');
const fs = require('fs');

@Injectable()
export class FileService {
  array = [];
  fileAddress = 'D:/home/file;
  async csvTojson(file: any): Promise<any> {
    this.array = [];
    try {
      const list = xlsx2json.parse(file.path);
      // list[0]为Sheet1
      const data = [...list[0].data];
      const arr = [];
      for (let i = 1; i < data.length; i++) {
        if (data[i][14] !== 0) {
          const type = iconv.decode(data[i][6], 'GBK').split('\t')[0];
          const param = {
            id: data[i][1],
            type: type,
            value: data[i][14],
          };
          arr.push(param);
        }
      }
      this.array = arr;
      const dataJson = JSON.stringify(arr);
      fs.writeFileSync(`${this.fileAddress}/dataJson.json`, `${dataJson}`);
      return {
        code: 200,
        message: '上传成功',
      };
    } catch (err) {
      return {
        code: 503,
        msg: `Service error: ${err}`,
      };
    }
  }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值