选中多条记录,打印.有可能有上百条,所有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了