Save JSP page content to Excel

Source: Internet
Author: User

In this, the emphasis on the search keyword importance, so that you can immediately locate the article, otherwise in elephant, no purpose awkward situation. This article is found through the export JSP to Excel.

The original address: How to Export the Web Page to Excel (in JSP)?

In this tutorial we will see how to export JSP pages to Excel and add the ability to export Excel to existing JSP pages.

Many times it is convenient for users to see the content of the page in Excel. Public schemes are exported to forms that contain information such as reports, numbers, and so on. Exporting data to Excel, end users can also use Excel to do various analyses, which is difficult for your Java Basic program to implement.

Let's say this is your JSP page:

This is the corresponding JSP source code (export Excel function has not added). A JSP page that contains a simple data table.

[Java]View PlainCopy print?
  1. <%@ page language="java" contenttype="text/html; Charset=iso-8859-1 "
  2. pageencoding="Iso-8859-1"%>
  3. <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c7>
  4. <meta http-equiv="Content-type" content= "text/html; Charset=iso-8859-1 ">
  5. <title>export to Excel-demo</title>
  6. <body>
  7. <table align="center" border="2" >
  8. <thead>
  9. <tr bgcolor="LightGreen" >
  10. <TH>SR. no.</th>
  11. <th>text data</th>
  12. <th>number data</th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <%
  17. For (int i = 0; i < i++) {
  18. %>
  19. <tr bgcolor="LightBlue" >
  20. <TD align="center" ><%=i%></td>
  21. <TD align="center" >this is text data <%=i%></td>
  22. <TD align="center" ><%=i * i%></td>
  23. </tr>
  24. <%
  25. }
  26. %>
  27. </tbody>
  28. </table>
  29. </body>


We'll add a "Export to Excel" hyperlink that will export the page content to an Excel file. Then this page will look like this:

The following is a new version of the JSP source code. This version adds the "Export to Excel" hyperlink, and adds the appropriate functionality:

[Java]View PlainCopyprint?
  1. <%@ page language="java" contenttype="text/html; Charset=iso-8859-1 "
  2. pageencoding="Iso-8859-1"%>
  3. <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c3>
  4. <meta http-equiv="Content-type" content= "text/html; Charset=iso-8859-1 ">
  5. <title>export to Excel-demo</title>
  6. <body>
  7. <%
  8. String exportToExcel = Request.getparameter ("exportToExcel");
  9. if (exporttoexcel! = null
  10. && exporttoexcel.tostring (). Equalsignorecase ("YES") {
  11. Response.setcontenttype ("application/vnd.ms-excel");
  12. Response.setheader ("content-disposition", "inline; Filename= "
  13. + "Excel.xls");
  14. }
  15. %>
  16. <table align="left" border="2" >
  17. <thead>
  18. <tr bgcolor="LightGreen" >
  19. <TH>SR. no.</th>
  20. <th>text data</th>
  21. <th>number data</th>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. <%
  26. For (int i = 0; i < i++) {
  27. %>
  28. <tr bgcolor="LightBlue" >
  29. <TD align="center" ><%=i + 1%></td>
  30. <TD align="center" >this is text data <%=i%></td>
  31. <TD align="center" ><%=i * i%></td>
  32. </tr>
  33. <%
  34. }
  35. %>
  36. </tbody>
  37. </table>
  38. <br><br><br><br><br><br><br><br><br><br><br> <br><br><br><br>
  39. <%
  40. if (exporttoexcel = = null) {
  41. %>
  42. <a href="Excel.jsp?exporttoexcel=yes" >export to excel</a>
  43. <%
  44. }
  45. %>
  46. </body>

Export Web page to Excel code Explanation: 1) In this version of the JSP page, when you click the "Export to Excel" hyperlink, the request is sent to the same page (excel.jsp), but the accompanying URL parameter is exporttoexcel=yes. [Java]View PlainCopyprint?
    1. <a href="Excel.jsp?exporttoexcel=yes" >export to excel</a>
2) The JSP page will check the correctness of the parameters at the beginning. If the value of this parameter is yes, we will see the content type in the response message, which identifies the Excel file name and opens it on the user's computer. [Java]View PlainCopy  print?
    1. string exporttoexcel = request.getparameter ( "exportToExcel");   
    2. if  (exporttoexcel  != null  
    3.          && exporttoexcel.tostring (). Equalsignorecase ( "YES"))  {  
    4.     response.setcontenttype (
    5.     response.setheader ( "Content-disposition",  
    6. &NBSP;&NBSP;&NBSP;
    7. }  

3) When you click the "Export to Excel" hyperlink, the contents of all pages will be exported to Excel. However, we may not want to have the "Export to Excel" hyperlink come out of Excel. To prevent it from appearing, we have added a judgment condition to determine whether the exportToExcel parameter appears. If it does, it means that the content is exported to Excel and does not include hyperlinks. Conversely, it means that we just want the browser to display the page, then the hyperlink will appear on the page. [Java]View PlainCopyprint?
    1. <%
    2. if (exporttoexcel = = null) {
    3. %>
    4. <a href="Excel.jsp?exporttoexcel=yes" >export to excel</a>
    5. <%
    6. }
    7. %>

Export page to Excel display but when you click the Hyperlink, a dialog box pops up asking if you want to open or save the file. Click Open and you will see the following: As you can see, the exported file will also save your formatting on the page. In the next tutorial, we'll see how to export a page to a Word file
    • How to Export the Web Page to Word (in JSP)
You can download the Eclipse engineering file in the Code Interpretation section of this tutorial.
    • Javaexcelexport.zip (5KB)
[end] =============================================

Attention:

If not set: Response.setheader ("Content-disposition", "attachment; Filename= "+ filename+". xls ");
Zemer considers the current page. xls.
For example testexcel.jsp---> Generate testexcel.xls form


Extended reading:

[1] generation of Excel and Word via HTTP protocol in JSP: After the author has used JXL, the HTTP method is chosen to generate Excel

[2] Jsp-excel save defualt in. xls extension, open EXCEL with in the browser:

[3] using the HTTP protocol to change the output file: The importance of the HTTP1.1 protocol

[4] Hypertext Transfer Protocol-http (Rev.): HTTP1.1 protocol explained in detail

[5] Multiple tables data exported to multiple worksheets of the same Excel sheet in Java: One in Excel, stored in multiple sheet

Save JSP page content to Excel (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.