javascript 如何使用table2excel.js插件将HTML表格下载到Excel中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30107922/
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-28 11:41:28  来源:igfitidea点击:

How to use table2excel.js plugin to download HTML table into Excel

javascripthtmlexcel

提问by SlimJim

I'm using the table2excel.js plugin to download an HTML table to Excel, in JavaScript. When I download the table, I get the message: "Excel cannot open the file 'Test.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file." When I manually change the name of the downloaded file to 'Test.xls', I'm able to open the file fine (with a small warning that the file format and extension don't match).

我正在使用 table2excel.js 插件在 JavaScript 中将 HTML 表格下载到 Excel。当我下载表格时,我收到消息:“Excel 无法打开文件 'Test.xlsx',因为文件格式或文件扩展名无效。请确认文件没有损坏,并且文件扩展名与文件。” 当我手动将下载的文件的名称更改为“Test.xls”时,我可以很好地打开文件(带有文件格式和扩展名不匹配的小警告)。

Here is the link to table2excel.js: https://github.com/rainabba/jquery-table2excel/blob/377b933ae6b04f4c1826acc24a2bb0a049933f8b/dist/jquery.table2excel.js

这是 table2excel.js 的链接:https: //github.com/rainabba/jquery-table2excel/blob/377b933ae6b04f4c1826acc24a2bb0a049933f8b/dist/jquery.table2excel.js

Some of the things I've tried:
1. I changed e.uri from "e.uri = data:application/vnd.ms-excel;base64," to "e.uri = "data:application/msexcel;base64,"
2. I changed
. . . 'return ( settings.filename ? settings.filename : "table2excel") + ".xlsx"'; to
. . . 'return ( settings.filename ? settings.filename : "table2excel") + ".xls"';
(when making this change, the file was saved as Test.xls.xlsx and still had the same problem opening the .xlsx file).

我尝试过的一些事情:
1. 我将 e.uri 从“e.uri = data:application/vnd.ms-excel;base64,”更改为“e.uri =”data:application/msexcel;base64, “
2.我改变
“返回(settings.filename settings.filename:table2excel ”)+“的.xlsx。”“ ';对
'返回(settings.filename settings.filename:?。 ”table2excel“)+ ".xls"';
(进行此更改时,文件被另存为 Test.xls.xlsx 并且在打开 .xlsx 文件时仍然存在相同的问题)。

How can I get the file to save as .xls rather than xlsx? Or is there a way to make this work while still saving the file as .xlsx (presumably by matching the format with the .xlsx extension)?

如何将文件另存为 .xls 而不是 xlsx?或者有没有办法在将文件保存为 .xlsx 的同时进行这项工作(大概是通过将格式与 .xlsx 扩展名匹配)?

Note: if it matters, the file Test.xlsx is "plain vanilla" with just text in a bunch of cells. There's no fancy formatting, characters, etc. The text is all Alphanumeric, with just a few special characters such as : ",.#'/@"

注意:如果重要的话,文件 Test.xlsx 是“普通的”,在一堆单元格中只有文本。没有花哨的格式、字符等。文本都是字母数字,只有一些特殊字符,例如:“,.#'/@”

Many thanks if any ideas!

如果有任何想法,非常感谢!

回答by Rain

I have same problem too, I changed the following method in jquery.table2excel.jsto:

我也有同样的问题,我将以下方法更改jquery.table2excel.js为:

function getFileName(settings) {
    return ( settings.filename ? settings.filename : "table2excel") + ".xls";
}

I am able to open the xls file after that but a different format file extension message will prompt, perhaps changing the header will do.

之后我可以打开 xls 文件,但会提示不同格式的文件扩展名消息,也许更改标题就可以了。

回答by NicholasByDesign

Inside the source js file, I had to change

在源js文件里面,我不得不改变

function getFileName(settings) {
    return ( settings.filename ? settings.filename : "table2excel") + ".xls";
}

To

function getFileName(settings) {
    return ( settings.filename ? settings.filename : "table2excel");
}

回答by WonderWorker

This is a great plugin, but it has a bug that stops it working.

这是一个很棒的插件,但它有一个阻止它工作的错误。

Thankfully, since it's javascript, you can correct it yourself.

值得庆幸的是,由于它是 javascript,您可以自己更正。

Load up the jquery.table2excel.js file into a text editor.

将 jquery.table2excel.js 文件加载到文本编辑器中。

Around line 130 you'll find the function called, getFileName.

在第 130 行左右,您将找到名为 getFileName 的函数。

function getFileName(settings) {
    return ( settings.filename ? settings.filename : "table2excel") + ".xls";
}

Correct it to:

更正为:

function getFileName(settings) {
    return ( settings.name ? settings.name : "Untitled") + ".xls";
}

I guess the author couldn't decide what to name their variables.

我猜作者无法决定如何命名他们的变量。

Sadly, you will always get a warning about the file format being incorrect for the extension. This is because an xls file is usually a binary file, but table2excel exploits the feature of excel where it can load html.

遗憾的是,您总是会收到有关扩展名的文件格式不正确的警告。这是因为 xls 文件通常是二进制文件,而 table2excel 利用了 excel 可以加载 html 的特性。