jsp 将表格以Excel形式导出方法之一(不用导包)

1.使用这种方式可以将整个jsp页面以Excel的形式导出

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Excel.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>

  <body>
     <%  
        String exportToExcel = request.getParameter("exportToExcel");  
        if (exportToExcel != null  
                && exportToExcel.toString().equalsIgnoreCase("YES")) {  
            response.setContentType("application/vnd.ms-excel");  
            response.setHeader("Content-Disposition", "inline; filename="  
                    + "excel.xls");  
   
        }  
    %>  <h1>这里是除表格以外的部分</h1>
    <table>  
    
        <thead>  
            <tr bgcolor="lightgreen">  
                <th>Sr. No.</th>  
                <th>Text Data</th>  
                <th>Number Data</th>  
            </tr>  
        </thead>  
        <tbody>  
            <%  
                for (int i = 0; i < 5; i++) {  
            %>  
            <tr bgcolor="lightblue">  
                <td align="center"><%=i + 1%></td>  
                <td align="center">This is text data <%=i%></td>  
                <td align="center"><%=i * i%></td>  
            </tr>  
            <%  
                }  
            %>  
        </tbody>  
    </table>  
    <br><br><br>
              
    <%  
        if (exportToExcel == null) {  
    %>  
    <a href="index.jsp?exportToExcel=YES">Export to Excel</a>  
    <%  
        }  
    %>  
  </body>
</html>

  </body>
</html>
效果图:



2.如果你只想导出表格而不是整个jsp页面,你可以用<jsp:include>的方法将表格包含进去

Excel.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Excel.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>

  <body>
    <h1>这里是除表格以外的部分</h1>
    <jsp:include page="table.jsp"></jsp:include>
    <br><br><br><br>
      
    <a href="table.jsp">Export to Excel</a>  
    
    </body>
    </html>

table.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
	String wjm = "Excle.xls";
	response.setContentType("application/vnd.ms-excel;charset=utf-8");//当上一个页面点击跳转到这个页面,直接加载 导出
	response.setHeader("content-disposition", "attachment; filename="	+ wjm);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'table.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <table align="left" border="2">  
        <thead>  
            <tr bgcolor="lightgreen">  
                <th>Sr. No.</th>  
                <th>Text Data</th>  
                <th>Number Data</th>  
            </tr>  
        </thead>  
        <tbody>  
            <%  
                for (int i = 0; i < 5; i++) {  
            %>  
            <tr bgcolor="lightblue">  
                <td align="center"><%=i + 1%></td>  
                <td align="center">This is text data <%=i%></td>  
                <td align="center"><%=i * i%></td>  
            </tr>  
            <%  
                }  
            %>  
        </tbody>  
    </table>  
  </body>
</html>

效果图:



注:还有一种经典的导出方式。可以去学习学习



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值