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

  1. //导出到word  
  2.     public void exportWord(ActionMapping actionMapping, ActionForm actionForm,  
  3.             HttpServletRequest request, HttpServletResponse response) throws Exception {  
  4.           
  5.         DBTool tool = null ;  
  6.         try {     
  7.                     /*** 
  8.                 查询数据库获得数据                                                       **/  
  9.             ArrayList records = form.getRecords();  
  10.             if(null!=records&&0!=records.size()){  
  11.                 //word内容  
  12.                 String content="<html>";//拼接注意加上<html>  
  13.                 for (int i = 0; i < records.size(); i++) {  
  14.                     Record record =(Record) records.get(i);  
  15.                     //从数据库中获得数据,将oracle中的clob数据类型转换成string类型  
  16.                     Method method = record.get("CONTENT").getClass().getMethod("getVendorObj",new Class[]{});  
  17.                     CLOB clob = (CLOB)method.invoke(record.get("CONTENT"));  
  18.                     String cx  = clob.getSubString((long1, (int) clob.length());  
  19.                     String title= (String) record.get("TITLE");  
  20.                     //html拼接出word内容  
  21.                     content+="<div style=\"text-align: center\"><span style=\"font-size: 24px\"><span style=\"font-family: 黑体\">"       
  22.                             +title+"<br /> <br /> </span></span></div>";  
  23.                     content+="<div style=\"text-align: left\"><span >"       
  24.                         +cx+"<br /> <br /> </span></span></div>";  
  25.                     //插入分页符  
  26.                     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>";  
  27.                     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>";  
  28.   
  29.                 }   
  30.                 content += "</html>";  
  31.                 byte b[] = content.getBytes();    
  32.                 ByteArrayInputStream bais = new ByteArrayInputStream(b);    
  33.                 POIFSFileSystem poifs = new POIFSFileSystem();    
  34.                 DirectoryEntry directory = poifs.getRoot();    
  35.                 DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);    
  36.                 //输出文件  
  37.                 String name="导出知识";  
  38.                 response.reset();  
  39.                 response.setHeader("Content-Disposition",  
  40.                          "attachment;filename=" +  
  41.                          new String( (name + ".doc").getBytes(),  
  42.                                     "iso-8859-1"));  
  43.                 response.setContentType("application/msword");  
  44.                 OutputStream ostream = response.getOutputStream();   
  45.                 //输出到本地文件的话,new一个文件流  
  46.                 //FileOutputStream ostream = new FileOutputStream(path+ fileName);    
  47.                 poifs.writeFilesystem(ostream);    
  48.                 bais.close();    
  49.                 ostream.close();   
  50.             }  
  51.         } catch (IOException e) {    
  52.             e.printStackTrace();    
  53.       }    
  54.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值