从 Javascript 变量创建 xls 或 csv 文件

发布于 2024-12-06 09:03:00 字数 258 浏览 0 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

始于初秋 2024-12-13 09:03:00

打开一个窗口并将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 the Content-Disposition: header to give your file a name.

你没皮卡萌 2024-12-13 09:03:00

是的,但您还需要使用服务器端代码。使用 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 of attachment; filename="fileName.csv".

乖乖 2024-12-13 09:03:00

不,您不能直接从 JavaScript 创建和/或保存文件。在某些浏览器/平台 (IE/Windows) 上,您可以通过 ActiveX 对象创建并写入文件:

function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\temp\\Test.txt", true);
s.WriteLine('Hello');
s.Close();
}

另一种解决方案是使用客户端 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:

function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\temp\\Test.txt", true);
s.WriteLine('Hello');
s.Close();
}

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.

不如归去 2024-12-13 09:03:00

如果你想以浏览器网站风格来完成它可能会很困难。但 Javascript 是一种很好的语言来做到这一点,但您需要使用 .hta 而不是普通的 .html。创建 .hta 会创建一个独立的应用程序,就像普通的 .exe 一样。

这是您想要查找的 ActiveXObject("Excel.Application")

为了将 html 转换为 hta,这里是标签

<HTA:APPLICATION
  id="SomeId"
  border="thin"
  borderStyle="normal"
  caption="yes"
  maximizeButton="yes"
  minimizeButton="yes"
  showInTaskbar="yes"
  windowState="yes"
  innerBorder="yes"
  navigable="yes"
  scroll="auto"
  scrollFlat="yes"
  singleinstance="yes"
/>

供进一步阅读 htaExcel 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

<HTA:APPLICATION
  id="SomeId"
  border="thin"
  borderStyle="normal"
  caption="yes"
  maximizeButton="yes"
  minimizeButton="yes"
  showInTaskbar="yes"
  windowState="yes"
  innerBorder="yes"
  navigable="yes"
  scroll="auto"
  scrollFlat="yes"
  singleinstance="yes"
/>

For futher reading on hta and the excel active X

海之角 2024-12-13 09:03:00

您当然可以使用 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.

究竟谁懂我的在乎 2024-12-13 09:03:00

I think that it would be possible to do this (up to a certain size limit) with data URIs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文