Export HTML table to Excel - using jQuery or Java

2019-01-15 01:13发布

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

What would be the best way of going about this?

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

Any code examples for demo purposes should be great.

9条回答
forever°为你锁心
2楼-- · 2019-01-15 01:25

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

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

查看更多
做自己的国王
3楼-- · 2019-01-15 01:27

Exporting to Excel file format with JQuery is impossible.

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

查看更多
对你真心纯属浪费
4楼-- · 2019-01-15 01:30

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

Using it is easy. Simply include 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)

<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

查看更多
Viruses.
5楼-- · 2019-01-15 01:31

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

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-15 01:33

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.

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

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

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');
查看更多
Fickle 薄情
7楼-- · 2019-01-15 01:34

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

  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());
    
查看更多
登录 后发表回答