Java 將html導出word格式


@RequestMapping("download")
    public void exportWord( HttpServletRequest request, HttpServletResponse response) 
                throws Exception {
        User user = AppContext.getLoginUser();   
        Student student = studentSvc.findByUserId(user.getId());
        try {
                //word內容
                String content="<html><body></body></html>";
                byte b[] = content.getBytes("utf-8");  //這里是必須要設置編碼的,不然導出中文就會亂碼。
                ByteArrayInputStream bais = new ByteArrayInputStream(b);//將字節數組包裝到流中  
                /*
                * 關鍵地方
                * 生成word格式
                */
                POIFSFileSystem poifs = new POIFSFileSystem();  
                DirectoryEntry directory = poifs.getRoot();  
                DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); 
                //輸出文件
                String fileName="實習考核鑒定表";
                request.setCharacterEncoding("utf-8");  
                response.setContentType("application/msword");//導出word格式
                response.addHeader("Content-Disposition", "attachment;filename=" +
                         new String( (fileName + ".doc").getBytes(),  
                                 "iso-8859-1"));
                 
                OutputStream ostream = response.getOutputStream(); 
                poifs.writeFilesystem(ostream);  
                bais.close();  
                ostream.close(); 
            }catch(Exception e){
                AppUtils.logError("導出出錯:%s", e.getMessage());
            }  
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM