将javascript中的变量转换为CSV文件 [英] Converting variable in javascript to a CSV file

查看:233
本文介绍了将javascript中的变量转换为CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个字符串变量,由两个其他变量由逗号分隔组成:

Basically I have a string variable that is composed of two other variables separated by a comma:

item = "Pencil";
amount = 5;
entry = item + "," + amount;
*export entry to csv

所以entry变量应该是正确的格式保存为逗号分隔文件。是否有一些命令将采用这个变量并将其保存为csv或任何其他格式轻松地在电子表格中打开以供以后使用?条目变量将更改,并且如果csv文件已存在,则需要将新信息附加到csv文件。所以,让我们说如果我们也有:

So the "entry" variable should be in the correct format to save as a comma separated file. Is there some command that will take this variable and save it as a csv, or any other format easily opened in a spreadsheet for later use? The entry variable will change and the new information will need to be appended to the csv file if it already exists. So let's say if we also had:

item = "Paper";
amount = 25;
entry = item + "," + amount;
*export entry to csv

生成的csv文件应为:

The resulting csv file should be:

铅笔,5

纸,25

Pencil,5
Paper,25

我已经做了一些搜索其他问题,但大多数人似乎正在尝试做更复杂的事情(例如,处理服务器与客户端问题),而我只是想知道如何获取数据我在自己的计算机上工作在javascript到可保存文件。似乎这不是这样的问题,但许多问题。我肯定有一个简单的答案,有希望只有一个命令或两个。但是,我一直在通过很多半相关的职位,不太清楚,所以我想这将更快。

I've done a bit of searching through other questions, but most folks seem to be trying to do more complex things (e.g., dealing with server vs. client-side issues) while I'm just trying to figure out how to get data I'm working on my own computer in javascript to a saveable file. Seems like that isn't the case for many questions being asked though. I'm sure there's a simple answer out there and hopefully just a single command or two. However, I've been wading through a lot of semi-related posts that aren't too clear, so I figured this would be quicker.

推荐答案

这是完全可能的,使用一些 FileSaver 实现。我相信它在IE10本机存在,但仍需要一个垫片在其他浏览器。

It is perfectly possible, using some FileSaver implementation. I believe it exists natively on IE10, but still needs a shim on other browsers.

我写了一个轻量级的客户端CSV生成器库,可能会进来便利。请访问 http://atornblad.se/github/ (向下滚动到标题说客户端CSV文件生成器

I've written a light-weight client-side CSV generator library that might come in handy. Check it out on http://atornblad.se/github/ (scroll down to the headline saying Client-side CSV file generator)

正如我所说,它需要一个有效的 FileSaver 实现处理对 window.saveAs()的调用。查看Eli Gray在 http://eligrey.com上的解决方案/ blog / post / saving-generated-files-on-the-client-side

As I said, it requires a functioning FileSaver implementation handling calls to window.saveAs(). Check out Eli Grey's solution on http://eligrey.com/blog/post/saving-generated-files-on-the-client-side

到位时,您只需生成并保存CSV文件飞如此:

When in place, you simply generate and save CSV files on the fly like this:

var propertyOrder = ["name", "age", "height"];

var csv = new Csv(propertyOrder);

csv.add({ name : "Anders",
          age : 38,
          height : "178cm" });

csv.add({ name : "John Doe",
          age : 50,
          height : "184cm" });

csv.saveAs("people.csv");

这篇关于将javascript中的变量转换为CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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