您的位置:首页 > Web前端 > JavaScript

JSP应用导出Excel报表的简单实现以及中文乱码彻底解决(HTML)

2013-11-27 16:59 1161 查看
JSP应用导出Excel报表的简单实现以及中文乱码彻底解决(HTML)

一,先新建一个excel文件,调整格式(就是你所想要显示的格式),

二,把刚才新建的excel文件令存为.html(demo.html)文件,

三,新建一个jsp页面, 在该JSP页面头部设置response的ContentType为Excel格式

<% response.setContentType("application/vnd.ms-excel;charset=GBK"); %>

然后设置网页资料是以excel报表以线上浏览方式呈现或者是下载的方式呈现

<%

/ /这行设定传送到前端浏览器时的档名为test1.xls 就是靠这一行,让前端浏览器以为接收到一个excel档

//将网页资料以excel报表以线上浏览方式呈现

response.setHeader("Content-disposition","inline; filename=test1.xls");

//将网页资料以下载的方式

response.setHeader("Content-disposition","attachment; filename=test2.xls");

%>

然后把 demo.html的源代码粘贴在jsp页面,如下

<%@ page contentType="text/html; charset=GBK" %>

<% response.setContentType("application/vnd.ms-excel;charset=GBK");

response.setHeader("Content-disposition","attachment; filename=test2.xls");

%>

<!--以下为保持成html页面的excel的内容 demo.html页面-->

<html xmlns:o="urn:schemas-microsoft-com:office:office"

xmlns:x="urn:schemas-microsoft-com:office:excel"

xmlns="http://www.w3.org/TR/REC-html40">

<head>

<meta http-equiv=Content-Type content="text/html; charset=gb2312">

<meta name=ProgId content=Excel.Sheet>

<meta name=Generator content="Microsoft Excel 11">

<link rel=File-List href="qwe.files/filelist.xml">

<link rel=Edit-Time-Data href="qwe.files/editdata.mso">

<link rel=OLE-Object-Data href="qwe.files/oledata.mso">

<!--[if gte mso 9]><xml>

<o:DocumentProperties>

<o:Created>1996-12-17T01:32:42Z</o:Created>

<o:LastSaved>2010-05-12T13:59:04Z</o:LastSaved>

<o:Version>11.9999</o:Version>

</o:DocumentProperties>

<o:OfficeDocumentSettings>

<o:RemovePersonalInformation/>

</o:OfficeDocumentSettings>

</xml><![endif]-->

<style>

<!--table

{mso-displayed-decimal-separator:"\.";

mso-displayed-thousand-separator:"\,";}

@page

{margin:1.0in .75in 1.0in .75in;

mso-header-margin:.5in;

mso-footer-margin:.5in;}

tr

{mso-height-source:auto;

mso-ruby-visibility:none;}

col

{mso-width-source:auto;

mso-ruby-visibility:none;}

br

{mso-data-placement:same-cell;}

.style0

{mso-number-format:General;

text-align:general;

vertical-align:bottom;

white-space:nowrap;

mso-rotate:0;

mso-background-source:auto;

mso-pattern:auto;

color:windowtext;

font-size:12.0pt;

font-weight:400;

font-style:normal;

text-decoration:none;

font-family:宋体;

mso-generic-font-family:auto;

mso-font-charset:134;

border:none;

mso-protection:locked visible;

mso-style-name:常规;

mso-style-id:0;}

td

{mso-style-parent:style0;

padding-top:1px;

padding-right:1px;

padding-left:1px;

mso-ignore:padding;

color:windowtext;

font-size:12.0pt;

font-weight:400;

font-style:normal;

text-decoration:none;

font-family:宋体;

mso-generic-font-family:auto;

mso-font-charset:134;

mso-number-format:General;

text-align:general;

vertical-align:bottom;

border:none;

mso-background-source:auto;

mso-pattern:auto;

mso-protection:locked visible;

white-space:nowrap;

mso-rotate:0;}

ruby

{ruby-align:left;}

rt

{color:windowtext;

font-size:9.0pt;

font-weight:400;

font-style:normal;

text-decoration:none;

font-family:宋体;

mso-generic-font-family:auto;

mso-font-charset:134;

mso-char-type:none;

display:none;}

-->

</style>

<!--[if gte mso 9]><xml>

<x:ExcelWorkbook>

<x:ExcelWorksheets>

<x:ExcelWorksheet>

<x:Name>Sheet1</x:Name>

<x:WorksheetOptions>

<x:DefaultRowHeight>285</x:DefaultRowHeight>

<x:CodeName>Sheet1</x:CodeName>

<x:Selected/>

<x:Panes>

<x:Pane>

<x:Number>3</x:Number>

<x:ActiveCol>1</x:ActiveCol>

</x:Pane>

</x:Panes>

<x:ProtectContents>False</x:ProtectContents>

<x:ProtectObjects>False</x:ProtectObjects>

<x:ProtectScenarios>False</x:ProtectScenarios>

</x:WorksheetOptions>

</x:ExcelWorksheet>

<x:ExcelWorksheet>

<x:Name>Sheet2</x:Name>

<x:WorksheetOptions>

<x:DefaultRowHeight>285</x:DefaultRowHeight>

<x:CodeName>Sheet2</x:CodeName>

<x:ProtectContents>False</x:ProtectContents>

<x:ProtectObjects>False</x:ProtectObjects>

<x:ProtectScenarios>False</x:ProtectScenarios>

</x:WorksheetOptions>

</x:ExcelWorksheet>

<x:ExcelWorksheet>

<x:Name>Sheet3</x:Name>

<x:WorksheetOptions>

<x:DefaultRowHeight>285</x:DefaultRowHeight>

<x:CodeName>Sheet3</x:CodeName>

<x:ProtectContents>False</x:ProtectContents>

<x:ProtectObjects>False</x:ProtectObjects>

<x:ProtectScenarios>False</x:ProtectScenarios>

</x:WorksheetOptions>

</x:ExcelWorksheet>

</x:ExcelWorksheets>

<x:WindowHeight>4530</x:WindowHeight>

<x:WindowWidth>8505</x:WindowWidth>

<x:WindowTopX>480</x:WindowTopX>

<x:WindowTopY>120</x:WindowTopY>

<x:AcceptLabelsInFormulas/>

<x:ProtectStructure>False</x:ProtectStructure>

<x:ProtectWindows>False</x:ProtectWindows>

</x:ExcelWorkbook>

</xml><![endif]-->

</head>

<body link=blue vlink=purple>

<table x:str border=0 cellpadding=0 cellspacing=0 width=288 style='border-collapse:

collapse;table-layout:fixed;width:216pt'>

<col width=72 span=4 style='width:54pt'>

<tr height=19 style='height:14.25pt'>

<td height=19 width=72 style='height:14.25pt;width:54pt'>全球</td>

<td width=72 style='width:54pt'>问问</td>

<td width=72 style='width:54pt'>ee</td>

<td width=72 style='width:54pt'>rr</td>

</tr>

<tr height=19 style='height:14.25pt'>

<td height=19 style='height:14.25pt'>暗暗</td>

<td>ss</td>

<td>dd</td>

<td>ff</td>

</tr>

<![if supportMisalignedColumns]>

<tr height=0 style='display:none'>

<td width=72 style='width:54pt'></td>

<td width=72 style='width:54pt'></td>

<td width=72 style='width:54pt'></td>

<td width=72 style='width:54pt'></td>

</tr>

<![endif]>

</table>

</body>

</html>

中文问题:

查看源代码时发现JSP文件中写死的中文为乱码,则在JSP文件头部添加一行

<%@ page contentType="text/html; charset=gb2312" %>

查看源代码时发现文字为中文,但是用Excel打开为乱码则在<html>与<head>中加入

<meta http-equiv="Content-Type" content="text/html; charset=GBK">

在jsp页面中,要在excel中显示的内容可以从数据库中读取,在此就不做详细的介绍了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: