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();%>