日期:2014-05-16 浏览次数:20330 次
??? 今天需要做一个从页面导出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>