jsp 直接打开excel

response.setContentType("application/vnd.ms-excel");
File file =new File("D:/test.xls");
FileInputStream in=new FileInputStream(file);
byte[] buffer=new byte[in.available()];
in.read(buffer);
response.getOutputStream().write(buffer);
response.getOutputStream().flush();

response.getOutputStream().close();

 

 

 

<%@
      page language="java"
%><%@
      page import="java.util.*"
%><%@
      page import="org.apache.poi.hssf.usermodel.*"
%><%
      HSSFWorkbook wb =  new HSSFWorkbook();
      HSSFSheet s = wb.createSheet();
      response.setContentType("application/vnd.ms-excel");
      response.setHeader("Content-Disposition","attachment; filename
=unknown.xls");
      OutputStream out = response.getOutputStream();
      wb.write(out);
      out.close();
      return;
%>

调用response.setContentLength(文件大小,单位字节); 调用response.setHeader("Content-type","application/x-msexcel"); 调用response.setHeader("Content-DisposITion","inline; filename=\"test.xls\"");//在线打开 或者 response.setHeader("content-disposITion","attachment;filename=\"test.xls\"");//下载

你可能感兴趣的:(apache,jsp,Excel)