将 HTML 表格导出到 Excel - 使用 jQuery 或 Java

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

Export HTML table to Excel - using jQuery or Java

javajqueryhtmlexceljsp

提问by Pritish

I've a HTML table on my JSP page, that I want to be exported to Excel on a button click.

我的 JSP 页面上有一个 HTML 表,我想通过单击按钮将其导出到 Excel。

What would be the best way of going about this?

解决这个问题的最佳方法是什么?

(For ex., how would I do this using may be a jQuery function?)

(例如,我将如何使用 jQuery 函数执行此操作?)

Any code examples for demo purposes should be great.

任何用于演示目的的代码示例都应该很棒。

采纳答案by Tommy

I would recommend Apache POI, we've been using it for years, never had any problems.

我会推荐Apache POI,我们已经使用它多年了,从来没有遇到任何问题。

Alot of examples online to get a good start, and the documentation on the site is also good: http://poi.apache.org/spreadsheet/quick-guide.html

网上有很多例子可以很好地开始,网站上的文档也很好:http: //poi.apache.org/spreadsheet/quick-guide.html

回答by Pablo Santa Cruz

Exporting to Excel file format with JQuery is impossible.

使用 JQuery 导出为 Excel 文件格式是不可能的。

You can try with Java. There are a lot of libraries to do that.

你可以试试用Java。有很多图书馆可以做到这一点。

回答by KLee1

You would have to create something on the server-side (like a servlet) to read the html and create the excel file and serve it back to the user.

您必须在服务器端创建一些东西(如 servlet)来读取 html 并创建 excel 文件并将其提供给用户。

You could use this libraryto help you do the transformation.

您可以使用此库来帮助您进行转换。

回答by BalusC

Rather export to CSVformat. It's supported by Excel as well. Exporting to a fullworthy XLS(X) format using for example Apache POI HSSFor JExcepAPIis slow and memory hogging.

而是导出为CSV格式。Excel 也支持它。使用例如Apache POI HSSFJExcepAPI导出为完全值得的 XLS(X) 格式很慢并且占用内存。

Exporting to CSV is relatively simple. You can find a complete code example in this answer.

导出为 CSV 相对简单。您可以在此答案中找到完整的代码示例。



As to exporting to files using JavaScript, this is not possible without interaction of Flash or the server side. In Flash there's as far only the Downloadifylibrary which can export to simple txt files. Further, ExtJs seems to have a CSV export library, but I can't find any feasible demo page.

对于使用 JavaScript 导出到文件,如果没有 Flash 或服务器端的交互,这是不可能的。在 Flash 中,目前只有Downloadify库可以导出为简单的 txt 文件。此外,ExtJs 似乎有一个CSV 导出库,但我找不到任何可行的演示页面。

回答by dmott

Excel can load CSV (comma-separated value) files, which are basically just files with everything that would go into separate Excel cells separated by comma.

Excel 可以加载 CSV(逗号分隔值)文件,这些文件基本上只是将所有内容放入以逗号分隔的单独 Excel 单元格中的文件。

I don't know enough about how jQuery can handle pushing information into a file that you would download, but it seems a jQuery library has been written that at least transforms html tables to CSV format, and it is here: http://www.kunalbabre.com/projects/table2CSV.php

我不太了解 jQuery 如何处理将信息推送到您要下载的文件中,但似乎已经编写了一个 jQuery 库,至少将 html 表转换为 CSV 格式,它在这里: http://www .kunalbabre.com/projects/table2CSV.php

Edit (February 29, 2016):You can use the table2csv implementation above in conjunction with FileSaver.js(which is a wrapper for the HTML5 W3C saveAs()spec).

编辑(2016 年 2 月 29 日):您可以将上面的 table2csv 实现与FileSaver.js(它是 HTML5 W3CsaveAs()规范的包装器)结合使用。

The usage will end up looking something like:

用法最终看起来像:

var resultFromTable2CSV = $('#table-id').table2CSV({delivery:'value'});

var blob = new Blob([resultFromTable2CSV], {type: "text/csv;charset=utf-8"});

saveAs(blob, 'desiredFileName.csv');

回答by Wajdy Essam

You can parse the table using a library like http://jsoup.org/

您可以使用像http://jsoup.org/这样的库来解析表

After you get the data, you can store it in Excel-compatible format (CSV), or using Java Excel library for that like POI, or using JDBC to write data into Excel sheet, see this example: Password Protected Excel File

获取数据后,您可以将其存储为 Excel 兼容格式 (CSV),或使用 Java Excel 库用于 POI,或使用 JDBC 将数据写入 Excel 表,请参阅此示例: 密码保护的 Excel 文件

回答by Fran?ois Wauquier

I can suggest you to try http://code.google.com/p/gwt-table-to-excel/, at least the server part.

我可以建议您尝试http://code.google.com/p/gwt-table-to-excel/,至少是服务器部分。

回答by shrey

I also spend lot of time to convert html to excel after lot of R & D i found following easiest way.

经过大量的研发后,我还花了很多时间将 html 转换为 excel,我发现以下最简单的方法。

  1. create hidden field and in that pass your html data to your servlet or controller for e.g

    <form id="formexcel" action="" method="post" name="formexcel">
        <input type="hidden" name="exceldata" id="exceldata" value="" />
    </form>
    
  2. on your button of href click call following function and pass your html data using in document.formexcel.exceldata.value and your servlet or controller in document.formstyle.action

    function exportDivToExcel() {
        document.formexcel.exceldata.value=$('#htmlbody').html();
        $("#lblReportForPrint").html("Procurement operation review report");
        document.formstyle.method='POST';
        document.formstyle.action='${pageContext.servletContext.contextPath}/generateexcel';
        document.formstyle.submit();
    }
    
  3. Now in your controller or servlet write following code

    StringBuilder exceldata = new StringBuilder();
    exceldata.append(request.getParameter("exceldata"));
    ServletOutputStream outputStream = response.getOutputStream();
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename=\"exportexcel.xls\"");
    outputStream.write(exceldata.toString().getBytes());
    
  1. 创建隐藏字段并将您的 html 数据传递给您的 servlet 或控制器,例如

    <form id="formexcel" action="" method="post" name="formexcel">
        <input type="hidden" name="exceldata" id="exceldata" value="" />
    </form>
    
  2. 在您的 href 单击按钮上调用以下函数并使用 document.formexcel.exceldata.value 和您的 servlet 或控制器在 document.formstyle.action 中传递您的 html 数据

    function exportDivToExcel() {
        document.formexcel.exceldata.value=$('#htmlbody').html();
        $("#lblReportForPrint").html("Procurement operation review report");
        document.formstyle.method='POST';
        document.formstyle.action='${pageContext.servletContext.contextPath}/generateexcel';
        document.formstyle.submit();
    }
    
  3. 现在在您的控制器或 servlet 中编写以下代码

    StringBuilder exceldata = new StringBuilder();
    exceldata.append(request.getParameter("exceldata"));
    ServletOutputStream outputStream = response.getOutputStream();
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename=\"exportexcel.xls\"");
    outputStream.write(exceldata.toString().getBytes());
    

回答by Jonas Lomholdt

I have been using the jQuery plugin table2excel. It works very well and no serverside coding is needed.

我一直在使用 jQuery 插件table2excel。它工作得很好,不需要服务器端编码。

Using it is easy. Simply include jQuery

使用它很容易。只需包含 jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

Now include the table2excel script (Remember to change the src destination to match yours)

现在包含 table2excel 脚本(记住更改 src 目标以匹配您的目标)

<script src="dist/jquery.table2excel.min.js"></script>

Now simply call the script on the table you want exportet.

现在只需调用您想要导出的表上的脚本。

$("#yourHtmTable").table2excel({
    exclude: ".excludeThisClass",
    name: "Worksheet Name",
    filename: "SomeFile" //do not include extension
});

It's also easy to attach to a button like so:

附加到按钮也很容易,如下所示:

$("button").click(function(){
    $("#table2excel").table2excel({
    // exclude CSS class
    exclude: ".noExl",
    name: "Excel Document Name"
    }); 
});

All examples are taken directly from the authors github page and from jqueryscript.net

所有示例均直接取自作者 github 页面和jqueryscript.net