简体   繁体   中英

Export data from a table to Excel in JSP

I have a web application with a table. 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. 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. 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

The Javascript code you posted generates a data: URI that contains an "Excel HTML" file, not a true Excel workbook (XLS or XLSX) document.

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.

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.

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). 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.

The alternative is just to generate "Excel HTML" files, like you're doing right now, but server-side rather than relying on Javascript. I don't know how good you are with JSP, but returning custom content from a Servlet is a trivial task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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