用java在IE中開啟Excel

xuehongliang發表於2007-06-29

閒來無事做的一個小例子:

java 程式碼
  1. public class TestOpenExcel extends HttpServlet {
  2. private static final String url = "D:/test.xls";
  3. protected void doGet(HttpServletRequest request,
  4. HttpServletResponse response) throws ServletException, IOException {
  5. /**
  6. * setContentType設定MIME型別,Acrobat
  7. * PDF檔案為"application/pdf",WORD檔案為:"application/msword",
  8. * EXCEL檔案為:"application/vnd.ms-excel"。
  9. */
  10. response.setContentType("application/vnd.ms-excel");
  11. /**
  12. * setHeader設定開啟方式,具體為:inline為在瀏覽器中開啟,attachment單獨開啟。
  13. */
  14. response.setHeader("Content-disposition", "inline;filename="" + "test.xls"+ "";");
  15. ServletOutputStream sos = response.getOutputStream();
  16. FileInputStream fis = new FileInputStream(url);
  17. BufferedOutputStream bos = new BufferedOutputStream(sos);
  18. byte[] bytes = new byte[8192];
  19. bos.write(bytes, 0, fis.read(bytes));
  20. fis.close();
  21. sos.close();
  22. bos.close();
  23. }
  24. protected void doPost(HttpServletRequest request,
  25. HttpServletResponse response) throws ServletException, IOException {
  26. doGet(request, response);
  27. }
  28. }
[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/800861/viewspace-922103/,如需轉載,請註明出處,否則將追究法律責任。

相關文章