浏览 6365 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-07-08
最后修改:2009-07-08
首先说下我的环境 :jacob 1.14 , jdk 1.5 ,tomcat 5.X ,dll 文件放到 java—home 的bin下 。
网上都看了些关于jacob的文章 。但都不是很满足我的需求 。我们的需求是生成的word 有 图片 有动态的 table 。等等 。经过研究过网上一些内容, 封装了个java类 public class Export2Word { private Logger log=Logger.getLogger(this.getClass()); /** * 打开文件 * @param documents * @param inputDocPath * @return */ private Dispatch open(Dispatch documents,String inputDocPath){ return Dispatch.call(documents,"Open",inputDocPath).toDispatch(); } /** * 选定内容 * @param word * @return */ private Dispatch select(ActiveXComponent word){ return word.getProperty("Selection").toDispatch(); } /** * 把插入点移动到文件首位置 * @param selection */ private void moveStart(Dispatch selection){ Dispatch.call(selection, "HomeKey",new Variant(6)); } /** * 从选定内容或插入点开始查找文本 * @param selection 选定内容 * @param toFindText 要查找的文本 * @return true:查找到并选中该文本;false:未查找到文本。 */ private boolean find(Dispatch selection,String toFindText){ //从selection所在位置开始查询 Dispatch find = Dispatch.call(selection, "Find").toDispatch(); //设置要查找的内容 Dispatch.put(find, "Text", toFindText); //向前查找 Dispatch.put(find, "Forward", "True"); //设置格式 Dispatch.put(find,"format","True"); //大小写匹配 Dispatch.put(find, "MatchCase", "True"); //全字匹配 Dispatch.put(find, "MatchWholeWord", "True"); //查找并选中 return Dispatch.call(find, "Execute").getBoolean(); } /** * 把选定内容替换为设定文本 * @param selection * @param newText */ private void replace(Dispatch selection,String newText){ Dispatch.put(selection, "Text", newText); } /** * 全局替换 * @param selection * @param oldText * @param replaceObj */ private void replaceAll(Dispatch selection,String oldText,Object replaceObj){ moveStart(selection); // log.info("replace--"+oldText); if(oldText.startsWith("{table1}")){ createTable(selection,oldText,(List<String[]>) replaceObj); }else if(oldText.startsWith("{zp}")){ log.info("替换图片"); replaceImage(selection, oldText, replaceObj.toString()); }else{ String newText = (String) replaceObj; while (find(selection, oldText)) { replace(selection, newText); Dispatch.call(selection, "MoveRight"); } } } /** * image * @param selection * @param toFindText * @param imagePath */ public void replaceImage(Dispatch selection,String toFindText, String imagePath) { if(find(selection,toFindText)){ Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath); Dispatch.call(selection, "MoveRight"); } } /** * table */ public void createTable(Dispatch selection, String tableName, List<String[]> dataList) { if(find(selection,tableName)){ int row=dataList.size(); Dispatch tables = Dispatch.get(selection, "Tables").toDispatch(); Dispatch range = Dispatch.get(selection, "Range").toDispatch(); Dispatch newTable = Dispatch.call(tables, "Add", range, new Variant(row), new Variant(5)).toDispatch(); for(int i=0 ;i<row;i++){ String[] str=dataList.get(i); for(int j=0;j<str.length;j++){ log.info("行="+i+" 列="+j+"值="+str[j]); String s=str[j]; Dispatch cell = Dispatch.call(newTable, "Cell", new Variant(i+1), new Variant(j+1)).toDispatch(); Dispatch.call(cell, "Select"); Dispatch.put(selection, "Text", s); } } Dispatch.call(selection, "MoveRight"); } } /** * 打印 * @param document */ private void print(Dispatch document){ Dispatch.call(document, "PrintOut"); } /** * 保存文件 * @param word * @param outputPath */ private void save(ActiveXComponent word,String outputPath){ Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(), "FileSaveAs",outputPath); } /** * 关闭文件 * @param doc */ private void close(Dispatch doc){ Dispatch.call(doc, "Close",new Variant(true)); } /** * 保存打印doc文档 * @param inputDocPath * @param outPutDocPath * @param data * @param isPrint */ public void saveDoc(String inputDocPath,String outPutDocPath,Map<String,Object> data,boolean isPrint){ //初始化com的线程 ComThread.InitSTA(); //word运行程序对象 ActiveXComponent word = new ActiveXComponent("Word.Application"); //文档对象 Dispatch wordObject = (Dispatch) word.getObject(); //设置属性 Variant(true)表示word应用程序可见 Dispatch.put((Dispatch)wordObject,"Visible", new Variant(false)); //word所有文档 Dispatch documents = word.getProperty("Documents").toDispatch(); //打开文档 Dispatch document = this.open(documents,inputDocPath); Dispatch selection = this.select(word); Iterator keys = data.keySet().iterator(); String oldText; Object newValue; while(keys.hasNext()){ oldText = (String)keys.next(); newValue = data.get(oldText); this.replaceAll(selection, oldText, newValue); } //是否打印 if(isPrint){ this.print(document); } this.save(word,outPutDocPath); this.close(document); word.invoke("Quit", new Variant[0]); //关闭com的线程 ComThread.Release(); } } 调用的那方传入的替换变量是个Map Map<String,Object> map=new HashMap<String,Object>(); map.put("{xm}",jbxx.getXm()==null?"":jbxx.getXm()); List<String[]> tableList=new ArrayList<String[]>(); String strTitle[]=new String[]{"称谓","姓名","年龄","政治面貌","工作单位及主要职务"}; tableList.add(strTitle);
通过这里的list size 去创建动态的table 行数 更具 string[] 去创建 table的列。。让后在插入数据。普通变量的替换还是没变。主要是针对image 和table的时候要注意。。希望对 windows下 的同行有所帮助。。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-10-22
页码显示如何实现呢?
如果知道如何实现,请发送邮件wangxiaojs@gmail.com讨论 多谢 |
|
返回顶楼 | |
发表时间:2009-10-22
页面上就是一个按钮。。
|
|
返回顶楼 | |
发表时间:2009-10-27
用jacobgen生成word8.jar(对应word97).然后操作起来就方便多了.
|
|
返回顶楼 | |