ctfzh

用java在IE中打开Excel

public class TestOpenExcel extends HttpServlet {  

  private static final String url = "D:/test.xls";  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    

/**    
* setContentType设置MIME类型,Acrobat  
* PDF文件为"application/pdf",WORD文件为:"application/msword",    
* EXCEL文件为:"application/vnd.ms-excel"。  
*/  
response.setContentType("application/vnd.ms-excel");  

/**    
* setHeader设置打开方式,具体为:inline为在浏览器中打开,attachment单独打开。  
*/  
response.setHeader("Content-disposition", "inline;filename=\"" + "test.xls"+ "\";");  

  ServletOutputStream sos = response.getOutputStream();  

  FileInputStream fis = new FileInputStream(url);      

  BufferedOutputStream bos = new BufferedOutputStream(sos);  
  byte[] bytes = new byte[8192];    

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

sos.close();  
bos.close();      

}    

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
    doGet(request, response);  

}    

}



分类:

技术点:

相关文章:

  • 2021-12-22
  • 2021-12-22
  • 2022-01-16
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-22
  • 2022-12-23
  • 2021-12-12
  • 2021-12-22
  • 2022-01-15
  • 2021-12-04
相关资源
相似解决方案