从Javascript变量创建xls或csv文件 [英] Creating an xls or csv file from a Javascript variable

查看:139
本文介绍了从Javascript变量创建xls或csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Javascript进行一些计算然后绘制数据的应用程序,但是我想为用户添加选项,使其能够将数据实际下载到csv或xls文件中。

I have an app that uses Javascript to perform some calculations and then plot the data, but I'd like to add the option for the user to be able to actually download the data into a csv or xls file.

Javascript中是否有一种方法(或其他方法)让用户按下按钮,然后提示他们输入文件名以将其另存为然后会创建一个逗号分隔或excel电子表格?

Is there a way in Javascript (or some other method) to have the user press a button, then it will prompt them for the name of the file to save it as, and it will then create a comma-delimited or excel spreadsheet?

谢谢!

编辑:

感谢大家的所有建议。希望我能将所有人标记为答案,但是上船者现在必须要做

Thanks for all the suggestions everyone. Wish I could mark you all as answers, but upboats will have to do for now

推荐答案

打开一个窗口并将csv写入其中。但是我不知道javascript更改 Content-Type:标头的任何方法。否则,它将不会提示您保存或打开。

It's not hard to open a window and write the csv into it. But I don't know of any way for javascript to change the Content-Type: header. And without that it won't prompt to save or open.

您将需要服务器的帮助来执行此操作。您可以使用表单变量将数据发送到服务器,并让服务器使用正确的标题 Content-type:text / csv 将其发送回,您可能还希望 Content-Disposition:标头为文件命名。

You'll need assistance from the server to do this. You can send the data to the server in a form variable and have the server send it right back with the correct header Content-type: text/csv you may also want the Content-Disposition: header to give your file a name.

这篇关于从Javascript变量创建xls或csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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