使用 JavaScript 构建 XLSX 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21264165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:30:38  来源:igfitidea点击:

Build XLSX File With JavaScript

javascriptexcelxlsx

提问by Michael McCauley

I'm trying to build out an XLSX file using JavaScript using some database query results. I have to use JavaScript as the server back end only provides a JavaScript interface to work with (and it doesn't like jQuery). Now, I seem to have uncovered something to actually create the base64 encoded data to write to the file (the server interface provides an API for creating files using base64 encoded strings and defining a file type). XLSX.js looks like it will work well for that purpose, since it looks like it can read in some form of JS object and convert it to a base64 string. However, I am rather unclear about how to generate the worksheet XML data in the first place, or how I would want to construct a representative JS object. The only useful information I can seem to find doesn't really give me a good idea how to build it out, if I can even locate information on structuring. Most I find is about reading the files, not creating them. Also, I thought I found some simple to implement methods, but then they devolve into a mess of libraries and I get lost trying to understand which files are actually necessary and how to correctly include them in the work.

我正在尝试使用一些数据库查询结果使用 JavaScript 构建一个 XLSX 文件。我必须使用 JavaScript 作为服务器后端只提供一个 JavaScript 接口来使用(它不喜欢 jQuery)。现在,我似乎发现了一些实际创建 base64 编码数据以写入文件的内容(服务器接口提供了一个 API,用于使用 base64 编码字符串创建文件并定义文件类型)。XLSX.js 看起来很适合这个目的,因为它看起来可以读取某种形式的 JS 对象并将其转换为 base64 字符串。但是,我不太清楚如何首先生成工作表 XML 数据,或者我想如何构造具有代表性的 JS 对象。我似乎能找到的唯一有用信息并没有真正让我知道如何构建它,如果我什至可以找到有关结构的信息。我发现大多数是关于读取文件,而不是创建它们。此外,我认为我找到了一些简单的方法来实现,但后来它们变成了一堆库,我在试图了解哪些文件实际上是必要的以及如何正确地将它们包含在工作中时迷失了方向。

回答by Iman Sedighi

You can export data from js to XLS, XLSX and CSV formats with Alasqllibrary.

您可以使用Alasql库将数据从 js 导出为 XLS、XLSX 和 CSV 格式。

For example we want to write a data which we saved in test variable in an XLSX file through click a button. To do this just use following steps:

例如,我们想通过单击按钮将我们保存在测试变量中的数据写入 XLSX 文件。为此,只需使用以下步骤:

1- Include following files in your page:

1- 在您的页面中包含以下文件:

 <script src="http://alasql.org/console/alasql.min.js"></script> 
 <script src="http://alasql.org/console/xlsx.core.min.js"></script> 

2- So Then make a function in controller to save the test array as a xlsx file:

2-然后在控制器中创建一个函数以将测试数组保存为 xlsx 文件:

function saveAsXlsx(){
   alasql('SELECT * INTO XLSX("output.xlsx",{headers:true}) FROM ?',[test]);
}

So We save the data which we had in variable test into a file which we named it output.xlsx here.

所以我们将变量 test 中的数据保存到一个文件中,我们将其命名为 output.xlsx 在这里。

3- The last part is the easiest part. Run the function on click on a button:

3- 最后一部分是最简单的部分。单击按钮运行该功能:

 <button onclick="saveAsXlsx()" >Save as XLSX</button>

回答by Michael McCauley

I've abandoned trying to create an XLSX document in the environment I'm working in. I've been provided with a way to instead create an XLS file using the XML formatting for Excel 2003 that seems to work except for a warning when trying to open files generated in this fashion stating that the file is in a different format than what is specified by the extension. Other than that warning, I have been able to create workable files that open in Excel and behave pretty well.

我已经放弃了在我正在工作的环境中创建 XLSX 文档的尝试。我已经获得了一种使用 Excel 2003 的 XML 格式创建 XLS 文件的方法,除了尝试时出现警告外,该文件似乎有效打开以这种方式生成的文件,说明文件的格式与扩展名指定的格式不同。除了那个警告之外,我已经能够创建在 Excel 中打开并且表现良好的可行文件。

I guess maybe I'll have to leave generating XLSX files for any future .NET projects I may have. This kind of sucks as a resolution, but thank you everyone for your responses.

我想也许我将不得不为我可能拥有的任何未来 .NET 项目生成 XLSX 文件。这种决议很糟糕,但感谢大家的回应。

回答by Aaron Digulla

Excel supports two forms of XML documents as input. One is a ZIP archive which contains various XML documents and other data. This is the standard XLSX file format.

Excel 支持两种形式的 XML 文档作为输入。一个是包含各种 XML 文档和其他数据的 ZIP 存档。这是标准的 XLSX 文件格式。

The second form is just the sheet data itself as an XML document. So all you need is to create the XML as string. Wikipedia has an example for the Excel XML format. You can find links to the reference documentation under "External links"

第二种形式只是作为 XML 文档的工作表数据本身。因此,您只需将 XML 创建为字符串。维基百科有一个Excel XML 格式例子。您可以在“外部链接”下找到参考文档的链接

If you create a file in this format and save it as .xslx, then Excel should be able to open it. If not, then try the .xslextension. If you have trouble formatting something properly, do it in Excel, save and look at the resulting file.

如果您以此格式创建文件并将其另存为.xslx,则 Excel 应该能够打开它。如果没有,请尝试.xsl扩展。如果您在正确格式化某些内容时遇到问题,请在 Excel 中进行设置,保存并查看生成的文件。

I'm not sure XLSX.jswill be much help since it's a browser / client framework that depends on jQuery and jszip and other things. The "base64 and back" option refers to adding an Excel sheet to a HTML document as base64 string. The framework can then open this virtual file and examine it. I think it's not meant to generate Excel files for download.

我不确定XLSX.js会有多大帮助,因为它是一个依赖于 jQuery 和 jszip 以及其他东西的浏览器/客户端框架。“base64 and back”选项是指将 Excel 工作表作为 base64 字符串添加到 HTML 文档中。然后框架可以打开这个虚拟文件并检查它。我认为这不是为了生成 Excel 文件以供下载。