免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4460 | 回复: 0
打印 上一主题 下一主题

jacob循环替换模板文本的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-31 17:47 |只看该作者 |倒序浏览
RT,用以下程序按模板生成新的word,执行是可行的,但是现在需要循环替换模板文件,生成在同一个word上,则不行,只能执行一次,为何?应该如何修改?求高手指点。
模板已传到附件中
  1. package com.word;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.HashMap;

  9. import com.jacob.activeX.ActiveXComponent;
  10. import com.jacob.com.ComThread;
  11. import com.jacob.com.Dispatch;
  12. import com.jacob.com.Variant;

  13. public class Java2Word1 {
  14.         private boolean saveOnExit;
  15.         /**
  16.          * word文档
  17.          */
  18.         private Dispatch doc = null;
  19.         /**
  20.          * word运行程序对象
  21.          */
  22.         private ActiveXComponent word;
  23.         /**
  24.          * 所有word文档
  25.          */
  26.         private Dispatch documents;

  27.         /**
  28.          * 构造函数
  29.          */
  30.         public Java2Word1() {
  31.                 saveOnExit = false;
  32.                 word = new ActiveXComponent("Word.Application");
  33.                 word.setProperty("Visible", new Variant(false));
  34.                 documents = word.getProperty("Documents").toDispatch();
  35.         }

  36.         /**
  37.          * 设置参数:退出时是否保存
  38.          *
  39.          * @param saveOnExit
  40.          *            true-退出时保存文件,false-退出时不保存文件
  41.          */
  42.         public void setSaveOnExit(boolean saveOnExit) {
  43.                 this.saveOnExit = saveOnExit;
  44.         }

  45.         /**
  46.          * 得到参数:退出时是否保存
  47.          *
  48.          * @return boolean true-退出时保存文件,false-退出时不保存文件
  49.          */
  50.         public boolean getSaveOnExit() {
  51.                 return saveOnExit;
  52.         }

  53.         /**
  54.          * 打开文件
  55.          *
  56.          * @param inputDoc
  57.          *            要打开的文件,全路径
  58.          * @return Dispatch 打开的文件
  59.          */
  60.         public Dispatch open(String inputDoc) {
  61.                 return Dispatch.call(documents, "Open", inputDoc).toDispatch();
  62.         }

  63.         /**
  64.          * 选定内容
  65.          *
  66.          * @return Dispatch 选定的范围或插入点
  67.          */
  68.         public Dispatch select() {
  69.                 return word.getProperty("Selection").toDispatch();
  70.         }

  71.         /**
  72.          * 把选定内容或插入点向上移动
  73.          *
  74.          * @param selection
  75.          *            要移动的内容
  76.          * @param count
  77.          *            移动的距离
  78.          */
  79.         public void moveUp(Dispatch selection, int count) {
  80.                 for (int i = 0; i < count; i++)
  81.                         Dispatch.call(selection, "MoveUp");
  82.         }

  83.         /**
  84.          * 把选定内容或插入点向下移动
  85.          *
  86.          * @param selection
  87.          *            要移动的内容
  88.          * @param count
  89.          *            移动的距离
  90.          */
  91.         public void moveDown(Dispatch selection, int count) {
  92.                 for (int i = 0; i < count; i++)
  93.                         Dispatch.call(selection, "MoveDown");
  94.         }

  95.         /**
  96.          * 把选定内容或插入点向左移动
  97.          *
  98.          * @param selection
  99.          *            要移动的内容
  100.          * @param count
  101.          *            移动的距离
  102.          */
  103.         public void moveLeft(Dispatch selection, int count) {
  104.                 for (int i = 0; i < count; i++)
  105.                         Dispatch.call(selection, "MoveLeft");
  106.         }

  107.         /**
  108.          * 把选定内容或插入点向右移动
  109.          *
  110.          * @param selection
  111.          *            要移动的内容
  112.          * @param count
  113.          *            移动的距离
  114.          */
  115.         public void moveRight(Dispatch selection, int count) {
  116.                 for (int i = 0; i < count; i++)
  117.                         Dispatch.call(selection, "MoveRight");
  118.         }

  119.         /**
  120.          * 把插入点移动到文件首位置
  121.          *
  122.          * @param selection
  123.          *            插入点
  124.          */
  125.         public void moveStart(Dispatch selection) {
  126.                 Dispatch.call(selection, "HomeKey", new Variant(6));
  127.         }

  128.         /**
  129.          * 从选定内容或插入点开始查找文本
  130.          *
  131.          * @param selection
  132.          *            选定内容
  133.          * @param toFindText
  134.          *            要查找的文本
  135.          * @return boolean true-查找到并选中该文本,false-未查找到文本
  136.          */
  137.         public boolean find(Dispatch selection, String toFindText) {
  138.                 // 从selection所在位置开始查询
  139.                 Dispatch find = Dispatch.call(selection, "Find").toDispatch();
  140.                 // 设置要查找的内容
  141.                 Dispatch.put(find, "Text", toFindText);
  142.                 // 向前查找
  143.                 Dispatch.put(find, "Forward", "True");
  144.                 // 设置格式
  145.                 Dispatch.put(find, "Format", "True");
  146.                 // 大小写匹配
  147.                 Dispatch.put(find, "MatchCase", "True");
  148.                 // 全字匹配
  149.                 Dispatch.put(find, "MatchWholeWord", "True");
  150.                 // 查找并选中
  151.                 return Dispatch.call(find, "Execute").getBoolean();
  152.         }

  153.         /**
  154.          * 把选定内容替换为设定文本
  155.          *
  156.          * @param selection
  157.          *            选定内容
  158.          * @param newText
  159.          *            替换为文本
  160.          */
  161.         public void replace(Dispatch selection, String newText) {
  162.                 // 设置替换文本
  163.                 Dispatch.put(selection, "Text", newText);
  164.         }

  165.         /**
  166.          * 全局替换
  167.          *
  168.          * @param selection
  169.          *            选定内容或起始插入点
  170.          * @param oldText
  171.          *            要替换的文本
  172.          * @param newText
  173.          *            替换为文本
  174.          */
  175.         public void replaceAll(Dispatch selection, String oldText, Object replaceObj) {
  176.                 // 移动到文件开头
  177.                 moveStart(selection);
  178.                 if (oldText.startsWith("table") || replaceObj instanceof List) {
  179.                         replaceTable(selection, oldText, (List) replaceObj);
  180.                 } else {
  181.                         String newText = (String) replaceObj;
  182.                         if (oldText.indexOf("image") != -1
  183.                                         || newText.lastIndexOf(".bmp") != -1
  184.                                         || newText.lastIndexOf(".jpg") != -1
  185.                                         || newText.lastIndexOf(".gif") != -1)
  186.                                 while (find(selection, oldText)) {
  187.                                         replaceImage(selection, newText);
  188.                                         Dispatch.call(selection, "MoveRight");
  189.                                 }
  190.                         else
  191.                                 while (find(selection, oldText)) {
  192.                                         replace(selection, newText);
  193.                                         Dispatch.call(selection, "MoveRight");
  194.                                 }
  195.                 }
  196.         }

  197.         /**
  198.          * 替换图片
  199.          *
  200.          * @param selection
  201.          *            图片的插入点
  202.          * @param imagePath
  203.          *            图片文件(全路径)
  204.          */
  205.         public void replaceImage(Dispatch selection, String imagePath) {
  206.                 Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
  207.                                 "AddPicture", imagePath);
  208.         }

  209.         /**
  210.          * 替换表格
  211.          *
  212.          * @param selection
  213.          *            插入点
  214.          * @param tableName
  215.          *            表格名称,形如table$1@1、table$2@1...table$R@N,R代表从表格中的第N行开始填充,
  216.          *            N代表word文件中的第N张表
  217.          * @param fields
  218.          *            表格中要替换的字段与数据的对应表
  219.          */
  220.         public void replaceTable(Dispatch selection, String tableName, List dataList) {
  221.                 if (dataList.size() <= 1) {
  222.                         System.out.println("Empty table!");
  223.                         return;
  224.                 }
  225.                 // 要填充的列
  226.                 String[] cols = (String[]) dataList.get(0);
  227.                 // 表格序号
  228.                 String tbIndex = tableName.substring(tableName.lastIndexOf("@") + 1);
  229.                 // 从第几行开始填充
  230.                 int fromRow = Integer.parseInt(tableName.substring(
  231.                                 tableName.lastIndexOf("$") + 1, tableName.lastIndexOf("@")));
  232.                 // 所有表格
  233.                 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  234.                 // 要填充的表格
  235.                 Dispatch table = Dispatch.call(tables, "Item", new Variant(tbIndex))
  236.                                 .toDispatch();
  237.                 // 表格的所有行
  238.                 Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
  239.                 // 填充表格
  240.                 for (int i = 1; i < dataList.size(); i++) {
  241.                         // 某一行数据
  242.                         String[] datas = (String[]) dataList.get(i);
  243.                         // 在表格中添加一行
  244.                         if (Dispatch.get(rows, "Count").getInt() < fromRow + i - 1)
  245.                                 Dispatch.call(rows, "Add");
  246.                         // 填充该行的相关列
  247.                         for (int j = 0; j < datas.length; j++) {
  248.                                 // 得到单元格
  249.                                 Dispatch cell = Dispatch.call(table, "Cell",
  250.                                                 Integer.toString(fromRow + i - 1), cols[j])
  251.                                                 .toDispatch();
  252.                                 // 选中单元格
  253.                                 Dispatch.call(cell, "Select");
  254.                                 // 设置格式
  255.                                 Dispatch font = Dispatch.get(selection, "Font").toDispatch();
  256.                                 Dispatch.put(font, "Bold", "0");
  257.                                 Dispatch.put(font, "Italic", "0");
  258.                                 // 输入数据
  259.                                 Dispatch.put(selection, "Text", datas[j]);
  260.                         }
  261.                 }
  262.         }

  263.         /**
  264.          * 保存文件
  265.          *
  266.          * @param outputPath
  267.          *            输出文件(包含路径)
  268.          */
  269.         public void save(String outputPath) {
  270. //                Dispatch.call((Dispatch) Dispatch.call(word, "WordBasic").getDispatch(),
  271. //                            "FileSaveAs", outputPath,new Variant(51));
  272.                 Dispatch.invoke(doc, "SaveAs", Dispatch.Method,
  273.                                 new Object[] { outputPath }, new int[1]);

  274.         }

  275.         /**
  276.          * 关闭文件
  277.          *
  278.          * @param document
  279.          *            要关闭的文件
  280.          */
  281.         public void close(Dispatch doc) {
  282.                 Dispatch.call(doc, "Close", new Variant(saveOnExit));
  283.         }

  284.         /**
  285.          * 退出程序
  286.          */
  287.         public void quit() {
  288.                 word.invoke("Quit", new Variant[0]);
  289.                 ComThread.Release();
  290.         }

  291.         /**
  292.          * 根据模板、数据生成word文件
  293.          *
  294.          * @param inputPath
  295.          *            模板文件(包含路径)
  296.          * @param outPath
  297.          *            输出文件(包含路径)
  298.          * @param data
  299.          *            数据包(包含要填充的字段、对应的数据)
  300.          */
  301.         public void toWord(String inputPath, String outPath, HashMap data) {
  302.                 String oldText;
  303.                 Object newValue;
  304.                 try {
  305.                         doc = open(inputPath);
  306.                         Dispatch selection = select();
  307.                         Iterator keys = data.keySet().iterator();
  308.                         while (keys.hasNext()) {
  309.                                 oldText = (String) keys.next();
  310.                                 newValue = data.get(oldText);
  311.                                 replaceAll(selection, oldText, newValue);
  312.                         }
  313.                         save(outPath);
  314.                 } catch (Exception e) {
  315.                          //debug.println("toword[Java2Word]------------操作word文件失败!"+e.getMessage(),true);
  316.                         e.printStackTrace();
  317.                 }
  318.                 finally {
  319.                         if (doc != null)
  320.                                 close(doc);
  321.                 }
  322.         }
  323.         public static void main(String[] args) {
  324.                
  325.                 Java2Word1 j2w=new Java2Word1();
  326.                 String inputPath="C:\\Documents and Settings\\Administrator\\桌面\\报表\\提案模板.doc";
  327.                 String time = new SimpleDateFormat("yyyyMM-ddHHmmssSSS").format(new Date());
  328.                 String outPath="C:\\"+time+".doc";
  329.                 System.out.println(outPath);
  330.                 HashMap<String, String> data=new HashMap<String, String>();
  331.                 String main="社会发展.卫生.医疗";
  332.                 String settime="2013.12.17";
  333.                 String number="0004";
  334.                 String title="关于进一步完善社区卫生服务中心补充建立和配套医疗设施的提案";
  335.                 String zhuban="市卫生局";
  336.                 String huiban="市人力社保局";
  337.                 String content="问题:" +
  338.                                 "“看病贵、看病难”一直以来都是当今百姓最希望解决的问题。很多人都有过这样的经历:为了挂到大医院的专家号,早上天没亮就匆忙赶到医院开始排队等号……在大医院人满为患时,社区医院却“门可罗雀”。于是有了大医院里“病人等医生”,社区医院里“医生等病人”的反差。无论大病小患均到大医院就诊,已经成为百姓看病就医的习惯,“过度诊断”、“小病大治”也就应运而生,医患矛盾频发。" +
  339.                                 "中国的社区卫生服务是由以全科医生为核心的卫生服务团队提供的服务,相当于全科医疗服务,包括以个人的健康为中心的服务、以家庭为单位的服务、以社区为范围的服务和社会服务等内容。社区医院就是为百姓提供这种医疗服务的基层医院。" +
  340.                                 "在许多国家,社区医院都是就医首选,“小病在社区,大病到医院;手术在医院,护理在社区”已成医疗惯例。在英美等发达国家,基层卫生机构的就诊率达80%以上,不到20%的患者需转诊到中心医院或专科医院。在英国,平均每2000人就拥有一个社区全科医生。相比之下,我国的社区医疗就相形见绌了。调查显示,仅有22.5%的人愿意去社区医院就诊;而卫生部门的资料却表明,本来三级医院65%的门诊病人和77%的住院病人均可分流到社区卫生机构。究其原因,一方面是广大百姓对社区医院缺乏了解,另一方面,部分到社区医院就诊的患者反映:社区医院设备不足、就医条件差、人员资质水平有限、全科医生较少等诸多因素。" +
  341.                                 "按照国家对社区卫生服务中心规格的设定和服务任务要求按照1万名居民配备2-3名全科医生。公共卫生六位一体任务,即是指预防、医疗、保健、健康教育、康复和计划生育技术指导。目前社区卫生服务中心基本以区域建设完成,但就合理配套还有一定的差距。如大区域人口只有一个卫生服务中心,下属卫生站也不够,不能达到15分钟内步行到达。而且给当地社区卫生服务中心增加过多的医疗任务及公共卫生工作,造成社区卫生服务中心像个“筐”,什么都往里装,使得医务人员应接不暇,既完成不好绩效考核指标,又不能与正常的社区卫生服务中心同步争取“优秀”评比工作,大大挫伤了不是同一起跑线但是同一水平要求的一级社区卫生服务中心的积极性。\r\n" +
  342.                                 "分析:" +
  343.                                 "目前,全国95%的地级以上城市、86%的市辖区和一批县级市开展了城市社区卫生服务,全国已设置社区卫生服务中心3400多个社区卫生服务站近12000个,创建了108个全国社区卫生服务示范区。" +
  344.                                 "一个街道3万至10万常住居民规模设置一个社区卫生服务中心,居民从家中步行不超过15分钟能到达一个社区卫生服务机构。2000年我国60岁以上的老年人就已达到1.3亿,占我国总人口的10%,标明我国已经进入了老龄化社会。2020年预计我国60岁以上的老年人口将达到2.45亿,占总人口的16%以上。2020年基本实现城乡每万名居民有2-3名合格的全科医生,全国至少需要27万到41万名高素质的全科医生。而目前的全科医生和助理医生只有7.8万人,缺口近30万名。" +
  345.                                 "李克强总理说过:医改事关民生福祉,也是民心所向,坚持保基本、强基层、建机制,向深化改革要红利,把基本医疗卫生制度作为公共产品向全民提供。而基层医疗卫生机构是做好预防保健工作的“前哨”,深化医改要坚持预防为主、防治结合的方针,促进医疗卫生服务的关口要前移,重心下移。" +
  346.                                 "而就目前社区卫生服务中心机构而言,还存在诸多问题。如医疗人力资源匮乏、基础差、流失多等、是目前社区卫生服务面临的问题。要开展和完成真正意义上的“六位一体”工作尚有一定难度。\r\n" +
  347.                                 "建议:" +
  348.                                 "为了能更好的实现六位一体的服务模式,政府应继续加大投入与补偿政策,应进一步完善社区卫生服务机构结构建设,以便继续加强社区卫生服务“六位一体”功能:" +
  349.                                 "1、规范、严格按照国家规定的以区域人口建立、建设健全社区卫生服务中心、卫生站。" +
  350.                                 "2、统一完善各社区卫生服务中心配套设施、医疗科室建设。让老百姓享受规范的社区卫生服务中心服务要求。" +
  351.                                 "3、规范、完善健康档案及信息网络。" +
  352.                                 "4、绩效考核标准以管理人群为界定,让医务人员更实际、更好地完成各项医疗、公共卫生及医疗管理任务。" +
  353.                                 "5、加大对社区卫生服务中心的财力支持调整医疗保险相关政策:对社区卫生服务中心加大倾斜,完善配套政策;鼓励大医院办社区卫生服务中心,形成医疗集团牵头人,保障社区卫生服务医疗水平;鼓励民营资本进入社区卫生服务领域,共享医疗资源,进一步完善卫生机构内部运行机制:加强全科医师人才培养,推进社区卫生服务整体水平的提高,并广纳人才,以提高社区卫生服务中心的整体素质。" +
  354.                                 "";
  355.                 String person="王以新(189)";
  356.                 String job="首都医科大学附属北京安贞医院教育处副处长、全科医疗科主任";
  357.                 String address="北京市朝阳区安贞里首都医科大学附属北京安贞医院教育处";
  358.                 String cell="13910157217";
  359.                 String tel="64456417 64456417(Fax)";
  360.                 String post="100029";
  361.                 String email="Wangyixin6417@sina.com";
  362.                
  363.                         data.put("“主题词”", main);
  364.                         data.put("“日期”", settime);
  365.                         data.put("第“0”号", "第"+number+"号");
  366.                         data.put("“主办”", zhuban);
  367.                         data.put("“会办”", huiban);
  368.                         data.put("“题目”", title);
  369.                         data.put("“内容”", content);
  370.                         data.put("“提案人”", person);
  371.                         data.put("“职务”", job);
  372.                         data.put("“联系地址”", address);
  373.                         data.put("“联系手机”", cell);
  374.                         data.put("“联系电话”", tel);
  375.                         data.put("“邮政编码”", post);
  376.                         data.put("“邮箱”", email);
  377.                         Dispatch doc=j2w.open(inputPath);
  378.             for(int i=0;i<3;i++)       
  379.                   j2w.toWord(inputPath, outPath, data);

  380.         }
  381. }
复制代码

提案模板.rar

6.91 KB, 下载次数: 48

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP