jsp页面直接显示excel

本文介绍了一种通过配置web.xml和使用JSP页面实现在线预览Excel文件的方法。具体步骤包括设置MIME类型映射,创建测试页面以链接到Excel文件,并通过show.jsp页面读取并输出Excel文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、配置web.xml

web.xml中加入 

<mime-mapping>   
 <extension> xls </extension>   
 <mime-type> application/vnd.ms-excel </mime-type>   
 </mime-mapping>

2、测试页面testexcel.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
 <a href="show.jsp?path=E:\\结构图.xls"  target="_blank">查看</a>
 

3、显示页面show.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
 <%@ page import="java.io.*"%>
 <%
 response.reset();
 String path = new String(request.getParameter("path").getBytes("iso-8859-1"),"utf-8");
 System.out.println(path);
 response.setContentType("application/vnd.ms-excel");
 InputStream ips = new FileInputStream(path);  //<---你的excel文件
 OutputStream ops = response.getOutputStream();
 
 int data = -1;
 while((data = ips.read()) != -1) {
 
 ops.write(data);
 }
 
 ops.flush();%>


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值