search

Home  >  Q&A  >  body text

node.js - node 5.5 导出csv文件,文件名中文显示正确,内容中文乱码

node 5.5 导出csv文件,文件名中文显示正确,内容中文乱码

尝试使用 iconv-liteiconv.encode(str, 'GBK') 进行GBK编码不起作用。
求问如何解决啊~~

高洛峰高洛峰2778 days ago586

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 15:33:20

    I can do this, but then I found that the difference seems to be iconv.decode?

         var result = [];
                var bin = fs.readFileSync(file);
                var data = iconv.decode(bin, 'gbk');
                data = data.replace(/\t/g, "");
                data = data.replace(/\"([^\"]*)\"/g, function (a) {
                    return a.replace(/,|"/g, "")
                })
                var lines = data.split("\r\n");
                for (let i in lines) {
                    var rows = lines[i].split(",");
                    for (let j in rows) {
                        rows[j] = rows[j].trim();
                    }
                    result.push(rows)
                }
                this.xlsxData = result;

    reply
    0
  • 黄舟

    黄舟2017-04-17 15:33:20

    My solution is to output utf-8 directly without GBK encoding, but I need to add a BOM header.
    new Buffer('xEFxBBxBF','binary') + utf-8字符串

    reply
    0
  • Cancelreply