`
独上高楼
  • 浏览: 58068 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类

JSP导出Excel奇怪的乱码问题

阅读更多

    今天需要做一个从页面导出Excel的功能。在测试的过程中发现了一个奇怪的乱码问题。当仅有一条数据的时候导出的Excel会出现乱码,但是当多余一条数据的时候就没有乱码,真是奇怪。

    但还是解决了(添加了代码中红色的那行),现在我只把JSP的代码贴出来。

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="application/msexcel" %>
<% 
    //就是靠这一行,让前端浏览器以为接收到一个excel档  
    response.setHeader("Content-disposition","attachment; filename=Excle.xls");    
%>
<%
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 'Educe.jsp' starting page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
       <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">
  </head>
 
  <body>
   <form method="post">
  <table border="1" id="tb_contract_view">
     <tr class="title" style="font-weight:bold;">
      <td width="9%">
       合同编号
      </td>
      <td width="16%">
       身份证号
      </td>
      <td width="9%">
       姓名
      </td>
      <td width="30%">
       房屋地址
      </td>
      <td width="9%">
       户型
      </td>
      <td width="9%">
       所属区
      </td>
      <td width="9%">
       售价
      </td>
      <td width="9%">
       签售日期
      </td>
     </tr>
     <c:if test="${contract != null}">
      <c:forEach var="item" items="${contract}">
       <tr>
        <td>${item.booksale_id }</td>
        <td>${item.person_identityno }</td>
        <td>${item.person_name }</td>
        <td>${item.house_address }</td>
        <td>${item.house_typeName }</td>
        <td>${item.house_zoneName }</td>
        <td>${item.house_price }</td>
        <td>${item.contract_date }</td>
       </tr>
      </c:forEach>
      <c:remove var="contract"/>
     </c:if>
  </table>
 </form>
  </body>
</html>

分享到:
评论
2 楼 zhengJackson 2013-12-14  
我这添加了这个 导出的excel还是乱码额?
1 楼 qq4940 2012-08-27  
谢谢分享!

相关推荐

Global site tag (gtag.js) - Google Analytics