java将html导出成word(利用的poi包导出)

  1. 转载自:http://blog.csdn.net/myfmyfmyfmyf/article/details/25637345
  2. //导出到word
    	public void exportWord(ActionMapping actionMapping, ActionForm actionForm,
    			HttpServletRequest request, HttpServletResponse response) throws Exception {
    		
    		DBTool tool = null ;
            try {   
            			/***
    				查询数据库获得数据														**/
    			ArrayList records = form.getRecords();
    			if(null!=records&&0!=records.size()){
    				//word内容
    				String content="<html>";//拼接注意加上<html>
    				for (int i = 0; i < records.size(); i++) {
    					Record record =(Record) records.get(i);
    					//从数据库中获得数据,将oracle中的clob数据类型转换成string类型
    				    Method method = record.get("CONTENT").getClass().getMethod("getVendorObj",new Class[]{});
    				    CLOB clob = (CLOB)method.invoke(record.get("CONTENT"));
    				    String cx  = clob.getSubString((long) 1, (int) clob.length());
    					String title= (String) record.get("TITLE");
    					//html拼接出word内容
    					content+="<div style=\"text-align: center\"><span style=\"font-size: 24px\"><span style=\"font-family: 黑体\">"     
    							+title+"<br /> <br /> </span></span></div>";
    					content+="<div style=\"text-align: left\"><span >"     
    						+cx+"<br /> <br /> </span></span></div>";
    					//插入分页符
    					content+="<span lang=EN-US style='font-size:12.0pt;line-height:150%;mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;mso-ansi-language:EN-US;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all style='page-break-before:always'></span>";
    					content+="<p class=MsoNormal style='line-height:150%'><span lang=EN-US style='font-size:12.0pt;line-height:150%'><o:p> </o:p></span></p>";
    
    				} 
    				content += "</html>";
    	            byte b[] = content.getBytes();  
    	            ByteArrayInputStream bais = new ByteArrayInputStream(b);  
    	            POIFSFileSystem poifs = new POIFSFileSystem();  
    	            DirectoryEntry directory = poifs.getRoot();  
    	            DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);  
    	            //输出文件
    	            String name="导出知识";
    	            response.reset();
    				response.setHeader("Content-Disposition",
    	                     "attachment;filename=" +
    	                     new String( (name + ".doc").getBytes(),
    	                                "iso-8859-1"));
    				response.setContentType("application/msword");
    				OutputStream ostream = response.getOutputStream(); 
    				//输出到本地文件的话,new一个文件流
    				//FileOutputStream ostream = new FileOutputStream(path+ fileName);  
    	            poifs.writeFilesystem(ostream);  
    	            bais.close();  
    	            ostream.close(); 
    			}
            } catch (IOException e) {  
                e.printStackTrace();  
          }  
    	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值