JSP导出Excel表格 文件名+内容乱码解决+网格显示

【转载请注明出处:http://blog.csdn.net/leytton/article/details/38613549

文件名乱码解决:

excelName=new String("学生信息统计".getBytes("GBK"), "ISO8859-1");

内容乱码解决:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
response.setContentType("application/vnd.ms-excel;charset=GBK");
<meta http-equiv="Content-Type" content="text/html; charset=GBK">

网格显示:

<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<!-- 显示网格线 -->
<!--[if gte mso 9]><xml>
            <x:ExcelWorkbook>
                <x:ExcelWorksheets>
                    <x:ExcelWorksheet>
                        <x:Name>工作表标题</x:Name>
                        <x:WorksheetOptions>
                            <x:Print>
                                <x:ValidPrinterInfo />
                            </x:Print>
                        </x:WorksheetOptions>
                    </x:ExcelWorksheet>
                </x:ExcelWorksheets>
            </x:ExcelWorkbook>
        </xml>
        <![endif]-->
<!-- 显示网格线 -->


全部代码:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<%

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
   excelName=new String("学生信息统计".getBytes("GBK"), "ISO8859-1");
   response.reset();
   response.setHeader("Pragma", "No-cache"); 
   response.setHeader("Cache-Control", "No-cache"); 
   response.setDateHeader("Expires", 0);  
   response.setContentType("application/vnd.ms-excel;charset=GBK");

   response.setHeader("Content-disposition", "inline;filename="+excelName+".xls");
%>
<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
<!-- 显示网格线 -->
<!--[if gte mso 9]><xml>
            <x:ExcelWorkbook>
                <x:ExcelWorksheets>
                    <x:ExcelWorksheet>
                        <x:Name>工作表标题</x:Name>
                        <x:WorksheetOptions>
                            <x:Print>
                                <x:ValidPrinterInfo />
                            </x:Print>
                        </x:WorksheetOptions>
                    </x:ExcelWorksheet>
                </x:ExcelWorksheets>
            </x:ExcelWorkbook>
        </xml>
        <![endif]-->
<!-- 显示网格线 -->
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>

<body>

<table>
	<tr>
		  <td>学号</td>
		  <td>姓名</td>
		  <td>年龄</td>
	</tr>
	<tr>
		 <td>121310</td>
		  <td>张三</td>
		  <td>18</td>
	 </tr>
	 <tr>
		  <td>121311</td>
		  <td>李四</td>
		  <td>19</td>
	 </tr>
	 <tr>
		  <td>121312</td>
		  <td>王五</td>
		  <td>20</td>
	 </tr>
</table>
    
</body> 
</html>


posted @ 2014-08-16 15:22  Leytton  阅读(564)  评论(0编辑  收藏  举报