jacob 操作word 分页

1 篇文章 0 订阅
1 篇文章 0 订阅


最近由于项目需要,要做web版的批量打印,查了许多资料,第一种是用IE的打印控件,倒是挺简单的,但是数据多了浏览器直接卡死,先说说我的需求吧.如图:


选中多条记录,打印.有可能有上百条,所有IE是不能够支持的.  我们的打印模版开始是htm版的,开始

用模版直接舒服html版word进行打印  
<%@ page contentType="application/msword;charset=UTF-8" 大致可以生成word,可以打印,但是不支持外联css,导致页面混乱.最后在网上
看到了 iText ,准备直接把html转成pdf,用了 htmlworder 和 xmlworker ,都一样能转成功,但是样式不够关,道理一样,不支持外联css.最后
只能用jacob了,先在word中,定义成模版,在生成pdf,直接在浏览器中打印.生成倒是没问题,但是分页问题耽误了我半天时间,再此写出关键代码

:

     // 合并Word文档
     private void mergeWord(List<Map<String,String>> dataList,Dispatch selection,String tempPath) throws Exception {
         for(int i=0;i<dataList.size();i++)
         {
        	 // Dispatch.put(selection, "Text", "文档内容如下:"+i);
              Dispatch.call(selection, "MoveDown");
              Dispatch.call(selection, "TypeParagraph"); // 空一行段落
              Dispatch.call(selection, "insertFile", tempPath);// 插入文件内容
              Dispatch.call(selection, "TypeParagraph"); // 空一行段落
              fillData(dataList.get(i), selection);
              if( i != dataList.size()-1)
            	  nextPage(selection);
         }
     }
     /**
      * 换页
      * @throws Exception
      */
     public void nextPage(Dispatch select)
         throws Exception
     {
    	 
    	 
    	 Dispatch.call(select, "EndKey", new Variant(6));
    	// 插入分页符
    	Dispatch.call(select,  "InsertBreak" ,  new Variant(7) ); 
     }
    //填充数据
     private void fillData(Map p,Dispatch selection)
     {
         Iterator keys = p.keySet().iterator();

         while(keys.hasNext()) {

           String  oldText = (String) keys.next();

           String  newValue = (String) p.get(oldText);
           
             replaceAll(selection,oldText,newValue);

         }
     }
 /**

      * 根据模板、数据生成word文件

      * @param inputPath String 模板文件(包含路径)

      * @param outPath String 输出文件(包含路径)

      * @param data HashMap 数据包(包含要填充的字段、对应的数据)

      */

     public void toWord(String inputPath,String outPath,List<Map<String,String>> dataList) {

        String  path = "D:\\file_temp.doc";
         try {

             if(doc==null)
             {
            	 File file = new File(path);
            	 if( !file.exists()) 
            		 file.createNewFile();
            	 doc = open(path);
             }

             Dispatch selection = select();

             mergeWord(dataList,selection,inputPath);

              save(outPath);

         } catch(Exception e) {

             System.out.println("操作word文件失败!");

             e.printStackTrace();

         } finally {

             if(doc != null)

                 close(doc);

         }

     }

上面都是工具,下面是调用部分

/**
	 * create word by jacob-> replace $*$  text
	 * @param list  business list data
	 * @return
	 */ 
	public static List<Map<String,String>> getJavaWordListMap(List<?> list,String barcodeText,String temppic)
	{
		List<Map<String,String>> listMap = new ArrayList<Map<String,String>>();
		for( Object object : list)
		{
			Map beanmap = BeanMap.create(object);
			Map<String,String> dataMap = new HashMap<String,String>();
			Set set = beanmap.entrySet();   
	        Iterator iterator = set.iterator();   
	        while(iterator.hasNext())  
	        {   
	            Map.Entry mapentry = (Map.Entry)iterator.next();  
	            String key = mapentry.getKey()+"";
	            if( !key.contains(BOOLEAN_INITIALIZED) && !key.contains(BOOLEAN_MODIFIED ))
	            {
	            	dataMap.put("$"+key.toUpperCase()+"$", mapentry.getValue()+"");
	            }
	        }
	        
	        try {
	        	// 生成一维码部分
				dataMap.put("$barcode$", genBarcode(temppic,beanmap.get(barcodeText)+""));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			listMap.add(dataMap);
		}
		return listMap;
	}



barcodeText  为一维码图标   
temppic  为生成图片地址
下面是测试方法
	private void createWord() throws USMException
	{
		List<AppforvisaDAO> list = this.getAppbriefList();
		String temppic = "d:/barcode/";
		List<Map<String,String>> listData = AcsUtil.getJavaWordListMap(list,"appno",temppic);
		Java2Word j2w = new Java2Word();
		j2w.toWord("D:\\file2.doc","D:\\新员工入司需知.pdf",listData);
		File folder = new File(temppic);
		AcsUtil.delAllFile(temppic);
		 
	}




这样就ok了







  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值