Export jsp content to Excel table

Keywords: Excel Javascript xml Java

<%@ page language="java" contentType="text/html; charset=UTF-8"  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html xmlns:x="urn:schemas-microsoft-com:office:excel">  
<script type="text/javascript">  
  function exportExcel(){  
<!-- display grid lines -->    
                        <x:Name>Sheet title</x:Name>    
                                <x:ValidPrinterInfo />    
<!-- display grid lines -->    
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Export to Excel - Demo</title>  
        String exportToExcel = request.getParameter("exportToExcel");  
        if (exportToExcel != null  
                && exportToExcel.toString().equalsIgnoreCase("YES")) {  
            response.setHeader("Content-Disposition", "inline; filename="  
                    + "excel.xls");  
    <table align="left" border="2">  
            <tr bgcolor="lightgreen">  
                <th>Text content</th>  
                <td style="display: none">Sequence 222</td>  
                for (int i = 0; i < 10; i++) {  
            <tr bgcolor="lightblue">  
                <td align="center"><%=i%></td>  
                <td align="center">Text content <%=i%></td>  
                <td align="center"><%=i*10%></td>  
                <td style="display: none" align="center"><%=i * 20%></td>  
        if (exportToExcel == null) {  
    <a href="javascript:exportExcel();">Export as Excel</a>  

When you click the "export to excel" hyperlink, the contents of all pages will be exported to excel. However, we may not want the "export to excel" hyperlink to appear in Excel. In order to prevent it from appearing, we add a judgment condition to judge whether the exportToExcel parameter appears. If it does, it means that the content will be exported to excel, and hyperlinks will not be included. On the contrary, it means that we just want the browser to display the web page, so the hyperlink will appear on the page.

164 original articles published, 1 praised, 2599 visitors
Private letter follow

Posted by satre on Tue, 28 Jan 2020 08:40:29 -0800