简体   繁体   English

在JSP中将数据从表导出到Excel

[英]Export data from a table to Excel in JSP

I have a web application with a table. 我有一个带有表的Web应用程序。 And i need to add a export to excel button such that when the button is clicked the current tabel in the page is downloaded. 而且我需要向excel按钮添加导出,以便在单击按钮时下载页面中的当前表格。 I tried the following code: 我尝试了以下代码:

<script type="text/javascript">
var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()
</script>

which i got from the internet. 我从互联网上得到的。 It works fine for chrome, but in firefox it downloads the file as xxx.xls.part and doesnot work for Internet explorer. 对于chrome,它可以正常工作,但是在firefox中,它将文件下载为xxx.xls.part ,对于Internet Explorer不起作用。 I don't know which browser user is going to uses so i need it to work on all the browsers. 我不知道哪个浏览器用户将要使用,所以我需要它在所有浏览器上都能工作。

What i am doing here wrong? 我在这里做什么错了? Or is there any other way to do this. 还是有其他方法可以做到这一点。

Plz help. 请帮助。

Thanks in Advance. 提前致谢。

Note: I know that this type of questions are asked several times but none of them are working in my case. 注意:我知道这类问题会被问过几次,但是在我看来,这些问题都不起作用。 So please help. 所以请帮忙。

Note 2: I have a constrain that i can't use any external libraries such as poi or jxls 注意2:我有一个约束,我不能使用任何外部库,例如poi或jxls

The Javascript code you posted generates a data: URI that contains an "Excel HTML" file, not a true Excel workbook (XLS or XLSX) document. 您发布的Javascript代码生成一个data: URI,其中包含“ Excel HTML”文件,而不是真正的Excel工作簿(XLS或XLSX)文档。

data: URIs ( see Wikipedia ) are supported by most current generation browsers (IE8 and later), however IE8 only supports data URIs for images, for security reasons. data:大多数当前版本的浏览器(IE8和更高版本)都支持URI( 请参阅Wikipedia ),但是出于安全原因,IE8仅支持图像的数据URI。

Most "Export to Excel" implementations just generate a CSV file that can be opeed by Excel, however this means you can't include things like formatting, formulae, multiple worksheets and macros. 大多数“导出到Excel”实现只生成一个Excel可以操作的CSV文件,但是,这意味着您不能包含格式,公式,多个工作表和宏之类的内容。

With the new Office Open XML standards you can use standard Zip file and XML libraries (present in base Java libraries) to generate and modify Office documents, however OOXML documents are very complex, so this will require a lot of work (unless you can find anyone else who has done this already). 使用新的Office Open XML标准,您可以使用标准的Zip文件和XML库(存在于基本Java库中)来生成和修改Office文档,但是OOXML文档非常复杂,因此这将需要大量工作(除非您可以找到)已经完成此操作的其他任何人)。 Out of curiosity, why can't you use external libraries? 出于好奇,您为什么不能使用外部库? It's just a matter of referencing their Jar files in your project and bundling them. 只需在项目中引用其Jar文件并将其捆绑即可。

The alternative is just to generate "Excel HTML" files, like you're doing right now, but server-side rather than relying on Javascript. 替代方法是只生成“ Excel HTML”文件,就像您现在正在做的那样,但是要在服务器端而不是依靠Javascript。 I don't know how good you are with JSP, but returning custom content from a Servlet is a trivial task. 我不知道您使用JSP有多好,但是从Servlet返回自定义内容是一项微不足道的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM