在节点中的服务器上将XLS转换为CSV [英] Convert XLS to CSV on the server in Node

查看:121
本文介绍了在节点中的服务器上将XLS转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端Web应用程序,具有非常小的节点服务器来访问客户端不能访问的一些数据。其中之一就是使用.xls扩展名的excel电子表格。

I have a client-side web application, with a very minimal node server to access some data that the client can't. One of these things is excel spreadsheets with .xls extensions.

我试图让我的服务器设置下载xls,将其转换为csv,然后发送它回到客户端。我已经完成了下载部分,我确定我可以弄清楚发回部分,但是我的生活中找不到一个很好的图书馆,可以从xls转换为csv。

I'm trying to get my server set up to download the xls, convert it to csv, and then send it back to the client. I've got the download part done, and I'm sure I can figure out the "send back" part, but I can't for the life of me find a good library to convert from xls to csv.

有人可以将我指向一个能够以简单的方式做的图书馆吗? excel文件只是一张,没有复杂的工作簿或任何东西。

Can someone point me to a library that can do that in a simple fashion? The excel file is just one sheet, no complicated workbooks or anything.

还有另一种做法我不在想吗?

Or is there another way of doing this I'm not thinking of?

推荐答案

没有我知道的图书馆,但你可以使用 node-xlsx 来解析excel文件,自行获取行并生成CSV。这里有一个例子:

There is no library that I am aware of, but you could use node-xlsx to parse the excel file, get the rows and make the CSV yourself. Here's an example:

var xlsx = require('node-xlsx');
var fs = require('fs');
var obj = xlsx.parse(__dirname + '/test.xls'); // parses a file
var rows = [];
var writeStr = "";

//looping through all sheets
for(var i = 0; i < obj.length; i++)
{
    var sheet = obj[i];
    //loop through all rows in the sheet
    for(var j = 0; j < sheet['data'].length; j++)
    {
            //add the row to the rows array
            rows.push(sheet['data'][j]);
    }
}

//creates the csv string to write it to a file
for(var i = 0; i < rows.length; i++)
{
    writeStr += rows[i].join(",") + "\n";
}

//writes to a file, but you will presumably send the csv as a      
//response instead
fs.writeFile(__dirname + "/test.csv", writeStr, function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("test.csv was saved in the current directory!");
});

这篇关于在节点中的服务器上将XLS转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆