• JAVA操作word方法


    jacob,功能非常强大,能操作word,excel和pdf。下载地址是:http://sourceforge.net/projects/jacob-project/

    1、新建一个文档
      Dispatch word = new ActiveXComponent("Word.Application");
      Dispatch documents = word.getProperty("Documents").toDispatch();
      Dispatch doc = Dispatch.call(documents, "Add").toDispatch();
    2、保存并关闭文档
      Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {"D:/Dog.doc",new Variant(1) }, new int[3]);
      Dispatch.call(doc, "Close", new Variant(false));
      word.invoke("Quit", new Variant[0]);
    3、获得当前输入点
      private Dispatch getSelection()
      {
       return word.getProperty("Selection").toDispatch();
      }
    4、插入换行符
      Dispatch range = Dispatch.get(getSelection(), "Range").toDispatch();
      Dispatch.call(range, "InsertParagraphAfter");
      Dispatch.call(getSelection(), "MoveDown");
    5、设置字体格式
      Dispatch font = Dispatch.get(getSelection(), "Font").toDispatch();
      Dispatch.put(font, "Name", new Variant("宋体"));
      Dispatch.put(font, "Size", "18");
      Dispatch.put(font, "Bold", "1");
    6、设置段落格式
      Dispatch paraFormat = Dispatch.get(getSelection(), "ParagraphFormat").toDispatch();   
      Dispatch.put(paraFormat, "SpaceBefore", "5")
      Dispatch.put(paraFormat, "SpaceAfter", "5");
      Dispatch.put(paraFormat, "Alignment", "0");
    7、设置标题级别
      Dispatch activeDoc = word.getProperty("ActiveDocument").toDispatch();
      Dispatch style = Dispatch.call(activeDoc,"Styles","标题 1").toDispatch();
      Dispatch.put(getSelection(),"Style",style);
    8、插入换页符
      Dispatch.call(getSelection(), "InsertBreak", new Variant(7)); 
    9、当前位置插入文字
      
    Dispatch.put(getSelection(), "Text","社会主义好");  
    10、插入表格

      Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
      Dispatch range = Dispatch.get(getSelection(), "Range").toDispatch();
      Dispatch table = Dispatch.call(tables, "Add", range, new Variant(5),new Variant(5)).toDispatch(); 
    11、设置单元格样式并插入文字
      Dispatch cell = Dispatch.call(table, "Cell",Integer.toString(1), Integer.toString(1)).toDispatch();
      Dispatch.call(cell, "Select");
      Dispatch shading = Dispatch.get(cell, "Shading").toDispatch();
      Dispatch.put(shading, "BackgroundPatternColorIndex", "15");
      Dispatch font = Dispatch.get(getSelection(), "Font").toDispatch();
      Dispatch.put(font, "Color", "0,0,0,0");
      Dispatch.put(font, "Bold", "1");
      Dispatch.put(getSelection(), "Text", "中国"); 
    12、设置表格列宽
      Dispatch columns = Dispatch.get(table, "Columns").toDispatch(); 
      int[] ColWidth = {180,40,90,15,33};
      for(int iCW=0;iCW<5;iCW++)
      {
       Dispatch column = Dispatch.call(columns, "Item",new Variant(iCW+1)).toDispatch(); 
       Dispatch.put(column, "Width", new Variant(ColWidth[iCW]));
      }
    13、从表格中移到下面
      Dispatch.call(getSelection(),"MoveRight",new Variant(1),new Variant(1));
      Dispatch.call(getSelection(), "MoveDown");
      
      就总结这些吧,如果你用到其他的格式什么的,你可以通过Word提供的宏录制功能记录下你的操作然后查看它是用的什么指令。

  • 相关阅读:
    一个好玩的代码生成工具
    正式决定了...
    小评几种O/R Mapping工具
    本期期刊主题:ASP.NET技术与JavaScript技巧,包括控件等
    vs.net2005单元测试的私有方法测试
    如何为一个instance配置多个监听,并且实现Client Load Balancing和Client Load Balancing
    编译告警解决记录
    SourceInsight宏开发
    map
    挖掘G13金矿让150M手机内存变成250M
  • 原文地址:https://www.cnblogs.com/ysgcs/p/9239910.html
Copyright © 2020-2023  润新知