在jsp页面中显示word,excel

本文介绍了两种在Web页面上展示Word和Excel文档的方法。第一种方法通过修改web.xml配置文件并使用iframe标签来实现;第二种方法则通过在JSP页面读取文件并设置合适的Content-Type直接展示文档。

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

二。第一种方法

1.在web.xml中加入 

 

 <mime-mapping> 
       <extension>doc</extension> 
       <mime-type> application/msword </mime-type> 
   </mime-mapping> 
   <mime-mapping> 
       <extension>xls</extension> 
       <mime-type> application/msexcel </mime-type> 
   </mime-mapping>

 

 

 

2.jsp页面中加入

<iframe src="/SunECM/public/view/test.doc" align="middle"  style="margin-left: 100" width="90%" height="90%"></iframe>

 

********************以上两步即可。。。。。。。。。

 

二。第二种方法

在jsp页面添加

<%
   File f = new File("C:/Users/joner/Desktop/test.doc "); //写自己文件路径 
   FileInputStream fin = new FileInputStream(f);

   OutputStream output = response.getOutputStream();
   byte[] buf = new byte[1024];
   int r = 0;
   response.setContentType("application/msword;charset=GB2312 ");

   while ((r = fin.read(buf, 0, buf.length)) != -1) {
    output.write(buf, 0, r); //response.getOutputStream()   
   }
   fin.close();
   output.close();
  %>

 

 

 

 

 

在jsp页头引入 
<%@page contentType="application/msword;charset=gbk"%> 
这是在浏览器中已word形式打开 
<%@page contentType="application/vnd.ms-excel;charset=gbk"%> 
这是以excel。。。。。。 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值