首页 > 编程语言 > 详细

Java 将html导出word格式

时间:2015-05-13 16:30:23      阅读:246      评论:0      收藏:0      [点我收藏+]
@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());
            }  
    }

 

Java 将html导出word格式

原文:http://www.cnblogs.com/yuanfy008/p/4500480.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!