從JSP頁面匯出資料到Excel表格中,本內容僅供參考。(大三實訓時寫的“畢業生資訊查詢”項目)
1. 產生Excel模板
1) 自己建立一個Excel檔案(模板.xls),輸入一條記錄,然後設定字型樣式。如圖:
2) 將Excel檔案另存新檔網頁(“檔案”—>“另存新檔網頁”)
3) 使用記事本或UE開啟模板.htm,可以看到原始碼(源檔案比較長,在此省略)
2. 產生Jsp頁面,並進行一點的修改
注意:
1) 綠色代碼是要自己寫的,其中需要將contentType的屬性修改為application/vnd.ms-excel,編碼方式修改為gb2312,防止亂碼)
2) 紅色代碼是要根據自己的需求寫的,比如要匯入哪些類,輸出哪些內容
3) 其餘代碼都是複製的模板.htm的原始碼,其中黃色代碼需要修改(為其中一些屬性值加” ”或’’)
ExportToExcel.Jsp:
<%@ page language="java" contentType="application/vnd.ms-excel; charset=gb2312" pageEncoding="gb2312"%>
<%@page import="com.gsms.dao.*"%>
<%@page import="com.gsms.dao.impl.*"%>
<%@page import="com.gsms.domain.Student"%>
<%@page import="java.util.List"%>
<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="模板.files/filelist.xml">
<link rel="Edit-Time-Data" href="模板.files/editdata.mso">
<link rel="OLE-Object-Data" href="模板.files/oledata.mso">
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Created> 1996-12-17T01:32:42Z</o:Created>
<o:LastSaved>2012-12-20T06:47:47Z</o:LastSaved>
<o:Version>11.6568</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;}
.xl24
{mso-style-parent:style0;
text-align:center;}
.xl25
{mso-style-parent:style0;
mso-number-format:"Short Date";
text-align:center;}
.xl26
{mso-style-parent:style0;
font-size:14.0pt;
font-weight:700;
&n