jsp 导出excel

excel文件是可以在jsp响应,通过输出相应xml,即可得到相应xls文件。
jsp相接导出excel文件实例:

<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*" %>
<%@ include file="/common/taglibs.jsp"%>
<%@ page errorPage="../inc/error.jsp"%>
<%

response.setHeader("Content-Type", "application/vnd.ms-excel");
String fileName ="用户操作日志列表.xls";
fileName= new String(fileName.getBytes("GBK"),"ISO_8859_1");
response.addHeader("Content-Disposition", "attachment;filename="
+ fileName);

%>
<?xml version="1.0" encoding="GBK"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>1996-12-17T01:32:42Z</Created>
<LastSaved>2006-09-05T08:00:41Z</LastSaved>
<Version>11.5606</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<RemovePersonalInformation/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>4530</WindowHeight>
<WindowWidth>8505</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>120</WindowTopY>
<AcceptLabelsInFormulas/>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="m26817072">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="20" ss:Bold="1"/>
</Style>
<Style ss:ID="s20">
<Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
</Style>
<Style ss:ID="s21">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="14" ss:Bold="1"/>
</Style>
<Style ss:ID="s25">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s29">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom" ss:WrapText="1"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
</Style>
<Style ss:ID="s30">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom" ss:WrapText="1"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table>
<Column ss:AutoFitWidth="0" ss:Width="34.5"/>
<Column ss:AutoFitWidth="0" ss:Width="250.75"/>
<Column ss:AutoFitWidth="0" ss:Width="120.5"/>
<Column ss:AutoFitWidth="0" ss:Width="120.5"/>
<Column ss:AutoFitWidth="0" ss:Width="80.25"/>
<Column ss:AutoFitWidth="0" ss:Width="150.25"/>
<Row ss:AutoFitHeight="0" ss:Height="42.25">
<Cell ss:MergeAcross="5" ss:StyleID="m26817072"><Data ss:Type="String">用户操作日志列表</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="20.9375" ss:StyleID="s25">
<Cell ss:StyleID="s21"><Data ss:Type="String">序号</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">操作内容</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">操作用户</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">用户机构</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">用户Ip</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">访问时间</Data></Cell>
</Row>

<c:set value="0" var="i" scope="page" />
<s:iterator value="operateLogs">
<c:set value="${i+1}" var="i" scope="page" />
<Row>
<Cell ss:StyleID="s29"><Data ss:Type="Number">${i}</Data></Cell>
<Cell ss:StyleID="s20"><Data ss:Type="String">${operateDescption.descption}</Data></Cell>
<Cell ss:StyleID="s29"><Data ss:Type="String">${operatename}</Data></Cell>
<Cell ss:StyleID="s29"><Data ss:Type="String">${operatelogid.department.name}</Data></Cell>
<Cell ss:StyleID="s29"><Data ss:Type="String">${ipaddress}</Data></Cell>
<Cell ss:StyleID="s29"><Data ss:Type="String">${operatetime}</Data></Cell>
</Row>
</s:iterator>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Layout x:Orientation="Landscape"/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>300</HorizontalResolution>
<VerticalResolution>300</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>2</ActiveRow>
<ActiveCol>5</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet2">
<Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25"/>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet3">
<Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25"/>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>


生成word格试:

<%@ page contentType="application/vnd.ms-word; charset=utf-8"%>
<%@ page pageEncoding="utf-8"%>
<%@ page import="java.net.URLEncoder" %>

<%
String fileName = "test.doc";
//对中文文件名编码
fileName = URLEncoder.encode("test", "UTF-8") + ".doc";
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
%>

<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns:st1="urn:schemas-microsoft-com:office:smarttags"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ProgId" content="Word.Document">
<meta name="Generator" content="Microsoft Word 12">
<meta name="Originator" content="Microsoft Word 12">
<title>JSP页面导出为Word文档</title>
</head>
<body>
。。。页面内容。。。
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值