如何利用java在IE中打开Excel

本篇内容主要讲解“如何利用java在IE中打开Excel”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何利用java在IE中打开Excel”吧!

java 代码:

  1. publicclass TestOpenExcel extends HttpServlet {  

  2.  

  3. privatestaticfinal String url = "D:/test.xls";  

  4.  

  5. protectedvoid doGet(HttpServletRequest request,  

  6.   HttpServletResponse response) throws ServletException, IOException {  

  7.  

  8.  /**

  9.   * setContentType设置MIME类型,Acrobat

  10.   * PDF文件为"application/pdf",WORD文件为:"application/msword",

  11.   * EXCEL文件为:"application/vnd.ms-excel"。

  12.   */  

  13.  response.setContentType("application/vnd.ms-excel");  

  14.  /**

  15.   * setHeader设置打开方式,具体为:inline为在浏览器中打开,attachment单独打开。

  16.   */  

  17. response.setHeader("Content-disposition", "inline;filename="" + "test.xls"+ "";");  

  18.  

  19.  ServletOutputStream sos = response.getOutputStream();  

  20.  FileInputStream fis = new FileInputStream(url);    

  21.    

  22.  BufferedOutputStream bos = new BufferedOutputStream(sos);    

  23.  byte[] bytes = newbyte[8192];    

  24.  bos.write(bytes, 0, fis.read(bytes));  

  25.  fis.close();    

  26.  sos.close();    

  27.  bos.close();  

  28.  

  29.  

  30. }  

  31.  

  32.  

  33. protectedvoid doPost(HttpServletRequest request,  

  34.   HttpServletResponse response) throws ServletException, IOException {  

  35.  doGet(request, response);  

  36. }  

  37.  

  38. }  

到此,相信大家对“如何利用java在IE中打开Excel”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!