从 Javascript 变量创建 xls 或 csv 文件
我有一个使用 Javascript 执行一些计算然后绘制数据的应用程序,但我想添加一个选项,以便用户能够实际将数据下载到 csv 或 xls 文件中。
Javascript(或其他方法)中是否有一种方法可以让用户按下按钮,然后它会提示他们输入要另存为的文件名,然后创建一个逗号分隔或 Excel 电子表格?
谢谢!
编辑:
感谢大家的所有建议。希望我能将你们全部标记为答案,但现在必须做upboats
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.
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!
EDIT:
Thanks for all the suggestions everyone. Wish I could mark you all as answers, but upboats will have to do for now
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
打开一个窗口并将csv写入其中并不难。但我不知道 javascript 有什么方法可以更改
Content-Type:
标头。如果没有它,它不会提示保存或打开。您需要服务器的帮助才能执行此操作。您可以将数据以表单变量的形式发送到服务器,然后让服务器使用正确的标头将其发送回来
Content-type: text/csv
您可能还需要Content-Disposition:
标头为您的文件命名。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.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 theContent-Disposition:
header to give your file a name.是的,但您还需要使用服务器端代码。使用 JavaScript 构建指向页面的链接,该页面将 csv 数据作为附件流式传输回来。服务器输出应包含
attachment 的
。content-disposition
标头;文件名=“文件名.csv”Yes, but you'll need to use server-side code as well. Use JavaScript to construct a link to a page that streams the csv data back as an attachment. The server output should contain a
content-disposition
header ofattachment; filename="fileName.csv"
.不,您不能直接从 JavaScript 创建和/或保存文件。在某些浏览器/平台 (IE/Windows) 上,您可以通过 ActiveX 对象创建并写入文件:
另一种解决方案是使用客户端 JavaScript(在浏览器内)将 CSV 数据输出到单独的窗口(或弹出窗口) )并让用户将其复制/粘贴到 Excel 中。
No, you can't create and/or save a file directly from JavaScript. On some browsers/platforms (IE/Windows), you could create and write to a file via ActiveX object:
Another solution is to use client-side JavaScript (inside a browser) to output CSV data into a separate window (or pop-up) and have a user to copy/paste it into Excel.
如果你想以浏览器网站风格来完成它可能会很困难。但 Javascript 是一种很好的语言来做到这一点,但您需要使用
.hta
而不是普通的.html
。创建.hta
会创建一个独立的应用程序,就像普通的.exe
一样。这是您想要查找的
ActiveXObject("Excel.Application")
为了将 html 转换为 hta,这里是标签
供进一步阅读 hta 和 Excel Active X
If you want to do it in a browser website style it might be hard. But Javascript is a good language to do this, but you will need to use
.hta
instead of a normal.html
. Creating an.hta
creates a stand alone application just like a normal.exe
.Here is what you want to look for
ActiveXObject("Excel.Application")
In order to transform a html into an hta, here is the tag
For futher reading on hta and the excel active X
您当然可以使用 FireBreath 编写一个浏览器插件(IE 上的 ActiveX 控件,其他的 NPAPI)来执行此操作;你必须用 C++ 编写它。老实说,我同意其他人建议你在服务器端进行此操作,但你可以使用插件来完成,而且不会太困难。
You could certainly write a browser plugin (ActiveX control on IE, NPAPI on others) with FireBreath that would do this; you'd have to write it in C++. Honestly, I agree with others in suggesting that you do this server-side instead, but you can do it with a plugin and it wouldn't be too difficult.
我认为可以使用 数据 URI 来做到这一点(达到一定的大小限制)
I think that it would be possible to do this (up to a certain size limit) with data URIs