最近碰到一個問題,在jsp頁面匯出excel的時候,只匯出1行沒有亂碼,匯出2行的時候,出現亂碼,然後我就一列列的匯出,在倒數第2列的時候出現亂碼,網上各種找資料,都沒有怎麼解決我的問題 ,網上大部分都是解決jsp中文亂碼的文章,跟我這個還是有點差別的。後來發現,不知道誰寫的代碼,由於涉及到匯出,有可能匯出word 也有可能匯出excel,以前的人在代碼中判斷了一下:
<%
if("word".equals(docType)){
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body>
<style>
<!--
.td_border1{
border-right:1 solid #000;
border-bottom:1 solid #000;
}
-->
</style>
<%
}
對,就是這種代碼,如果是匯出word的話<html>這種標籤才加,匯出excel不加
註:jsp匯出excel,如果其他該設定的都設定了,還有亂碼,記得添加: <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="org.apache.commons.codec.binary.Base64"%><%@ taglib prefix="s" uri="/struts-tags"%><%@ page import="java.net.URLEncoder"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%><%@ taglib prefix="limit" uri="http://www.yineng.com/limit"%><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%><%@ taglib prefix="ev" uri="http://www.yineng.com/enumvalue" %><%String path = request.getContextPath(); response.setContentType("application/vnd.ms-excel;charset=UTF-8");String excelName = "學生學分達標情況"; //亂碼解決,根據使用者瀏覽器獲得下載資訊String agent = (String)request.getHeader("USER-AGENT");if(agent != null && agent.indexOf("MSIE") == -1){// FF excelName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(excelName.getBytes("UTF-8")))) + "?="; response.setHeader("Content-Disposition", "attachment; filename=" + excelName + ".xls"); } else { // IEexcelName = new String(excelName.getBytes("GBK"), "ISO-8859-1"); response.setHeader("Content-Disposition", "attachment; filename=" + excelName+ ".xls"); }%><html><head></head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><body><div><table class="tableMainStyle" border="1"><thead><tr> <th class="xh_align">序號</th><th >學號</th><th >姓名</th><th >行政班</th><th >組織機構</th><th >專業</th><th >年級</th><th >性別</th><th >民族</th><th >學生總學分</th><th >是否達標</th></tr></thead><tbody ><c:choose><c:when test="${! empty list }"><c:forEach var="entity" items="${list }" varStatus="obj"><tr><td>${obj.index+1 }</td><td>${entity.stuNum }</td><td>${entity.infoName }</td><td>${entity.orgClassName}</td><td>${entity.orgmanName}</td><td>${entity.specialtyName}</td><td>${entity.grade}</td><td>${entity.sex}</td><td>${entity.nation}</td><td>${entity.totalCredit}</td><td><ev:changeValue enumIndex="${entity.isCompliance}" enumClassPath="com.yineng.base.enums.StandardStateEnum" /></td></tr></c:forEach></c:when><c:otherwise><tr><td colspan="11" align="center"><s:text name="nodata" /></td></tr></c:otherwise></c:choose></tbody></table></div></body></html>