熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java操作word

2022-06-13   來源: Java核心技術 

  最近在做項目的時候需要這麼一個功能客戶有一大堆word格式的模板需要我們用程序向模板裡面填充一些數據如果是直接重新寫一個Word用POI或Itext都可以搞定關鍵是讀取並解析而且Word裡有表格圖片等其他東西這兩個框架要解析就比較麻煩然而用jacob卻可以輕松搞定

  下面是借鑒了別人已經包裝好了的代碼

  import comjacobactiveXActiveXComponent;

  import Dispatch;

  import Variant;

  public class WordHandle{

  //運行時的Word程序

  private ActiveXComponent app;

  //word對象

  private Dispatch words;

  //當前的word文檔

  private Dispatch doc;

  //當前光標位置

  private Dispatch cursor;

  //當前文檔是否只讀

  private boolean readOnly;

  //當前文檔中所有表格

  private Dispatch tables;

  //當前所在表格

  private Dispatch table;

  private int count;

  public WordHandle()

  {

  thisapp = new ActiveXComponent(WordApplication);

  thisappsetProperty(Visible new Variant(false));    // 設置word不可見

  words = thisappgetProperty(Documents)toDispatch();

  thisdoc = null;

  thiscursor = null;

  thisreadOnly = true;

  unt = ;

  }

  public boolean open(String fileName boolean readOnly) throws Exception

  {

  if (doc != null)

  {

  Systemoutprintln(當前文件未關閉);

  return false;

  }

  thisdoc = Dispatchinvoke(thiswords Open DispatchMethod new Object[] {fileName new Variant(false) new Variant(readOnly)} new int[])toDispatch();

  thiscursor = appgetProperty(Selection)toDispatch();

  thistables = Dispatchget(thisdocTables)toDispatch();

  thisreadOnly = readOnly;

  unt = Dispatchget(Dispatchget(thisdoc Words)toDispatch() Count)getInt() ;

  Systemoutprintln(打開文件 + fileName + (readOnly ? ReadOnly : Writable));

  return true;

  }

  public boolean newFile() throws Exception

  {

  if (doc != null)

  {

  Systemoutprintln(當前文件未關閉);

  return false;

  }

  thisdoc = Dispatchcall(thiswords Add)toDispatch();

  thisreadOnly = false;

  thiscursor = appgetProperty(Selection)toDispatch();

  thistables = Dispatchget(thisdocTables)toDispatch();

  Systemoutprintln(新建word文檔);

  return true;

  }

  public boolean close()

  {

  String fileName = null;

  if (thisdoc != null)

  {

  try

  {

  fileName = Dispatchget(thisdoc Name)getString();

  Dispatchcall(thisdoc Close new Variant(false));

  }

  catch (Exception e)

  {

  eprintStackTrace();

  }

  finally

  {

  thisdoc = null;

  }

  }

  Systemoutprintln(關閉文件 + fileName);

  return true;

  }

  public boolean quit()

  {

  try

  {

  thisappinvoke(Quit new Variant[] {});

  }

  catch (Exception e)

  {

  eprintStackTrace();

  }

  Systemoutprintln(退出word);

  return true;

  }

  public boolean saveAs(String fileName) throws Exception

  {

  if (thisdoc == null)

  {

  Systemoutprintln(當前無文件);

  return false;

  }

  else

  {

  Dispatchcall(thisdoc SaveAs fileName);

  Systemoutprintln(另存為 + fileName);

  return true;

  }

  }

  public boolean save() throws Exception

  {

  if (thisdoc == null)

  {

  Systemoutprintln(當前無文檔無法保存);

  return false;

  }

  else

  {

  if (thisreadOnly)

  {

  Systemoutprintln(只讀文檔保存失敗);

  return false;

  }

  Dispatchcall(thisdoc Save);

  Systemoutprintln(保存完成);

  return true;

  }

  }

  public boolean moveRight(int steps) throws Exception

  {

  //int start = Dispatchget(thiscursor Start)getInt();

  //Dispatchput(thiscursor Start start + steps);

  for (int i=; i<steps; i++)

  {

  Dispatchcall(cursor MoveRight);

  }

  return true;

  }

  public boolean moveLeft(int steps) throws Exception

  {

  for (int i=; i<steps; i++)

  {

  Dispatchcall(cursor MoveLeft);

  }

  return true;

  }

  public int search(String str) throws Exception

  {

  // 從cursor所在位置開始查詢

  Dispatch find = Dispatchcall(thiscursor Find)toDispatch();

  // 設置要查找的內容

  Dispatchput(find Text str);

  // 向前查找

  Dispatchput(find Forward True);

  // 設置格式

  Dispatchput(find Format True);

  // 大小寫匹配

  Dispatchput(find MatchCase True);

  // 全字匹配

  Dispatchput(find MatchWholeWord True);

  // 查找

  if (!Dispatchcall(findExecute)getBoolean())

  return ;

  else

  {

  return Dispatchget(thiscursor Start)getInt();

  }

  }

  public int searchOnly(String str) throws Exception

  {

  // 從cursor所在位置開始查詢

  Dispatch find = Dispatchcall(thiscursor Find)toDispatch();

  // 設置要查找的內容

  Dispatchput(find Text str);

  // 向前查找

  Dispatchput(find Forward True);

  // 大小寫匹配

  Dispatchput(find MatchCase True);

  // 全字匹配

  Dispatchput(find MatchWholeWord True);

  if (!Dispatchcall(findExecute)getBoolean())

  return ;

  else

  {

  int start = Dispatchget(thiscursor Start)getInt();

  Dispatchput(thiscursor End unt);

  //Systemoutprintln(start);

  return start;

  }

  }

  public String getBetween(int start int end) throws Exception

  {

  Dispatch range = Dispatchget(thiscursor Range)toDispatch();

  Dispatchcall(rangeSetRange start end);

  return Dispatchget(range Text)getString();

  }

  public String getLineAfter(int start) throws Exception

  {

  Dispatchput(thiscursor Start start);

  int length = Dispatchcall(thiscursor EndKey)getInt() + start;

  return getBetween(start length);

  }

  public String getLine(int position) throws Exception

  {

  Dispatchput(thiscursor Start position);

  Dispatchcall(thiscursor SelectRow);

  int start = Dispatchget(thiscursor Start)getInt();

  int end = Dispatchget(thiscursor End)getInt();

  return getBetween(start start + end);

  }

  public boolean gotoPage(int index) throws Exception

  {

  Dispatchinvoke(thiscursor Goto DispatchMethod new Object[] { StringvalueOf(index)} new int[]);

  //Dispatchcall(thiscursor GoTo wdGoToLine wdGoToNext StringvalueOf(index) null);

  return true;

  }

  public int getCurrentCursor() throws Exception

  {

  return Dispatchget(thiscursor Start)getInt();

  }

  public boolean setCursorMode() throws Exception

  {

  Dispatchput(thiscursor End Dispatchget(thiscursor Start)getInt());

  return true;

  }

  public boolean gotoHome() throws Exception

  {

  Dispatchput(thiscursor Start );

  return true;

  }

  public boolean insert(int steps String str) throws Exception

  {

  int start = Dispatchget(thiscursor Start)getInt() + steps;

  Dispatchput(thiscursor Start start);

  Dispatchcall(thiscursor InsertBefore str);

  //thisgetCount();

  Dispatchput(thiscursor Start start + strlength());

  //Systemoutprintln(Dispatchget(thiscursor Start)getInt() +    + (Dispatchget(thiscursor Start)getInt()+Dispatchget(thiscursor End)getInt()));

  return true;

  }

  public boolean replace(String str) throws Exception

  {

  Dispatchput(thiscursor Text str);

  return true;

  }

  public int getTableNum() throws Exception

  {

  return Dispatchget(thistables Count)getInt();

  }

  public boolean setCurrentTable(int index) throws Exception

  {

  thistable = Dispatchcall(thistables Item new Variant(index))toDispatch();

  Dispatchcall(thistable Select);

  return true;

  }

  public String getCell(int row int col) throws Exception

  {

  Dispatch cell = Dispatchcall(table Cell IntegertoString(row) IntegertoString(col))toDispatch();

  Dispatchcall(cellSelect);

  String tmp = Dispatchget(thiscursor Text)getString();

  //Systemoutprintln( + tmp);

  if (tmplength() > )

  {

  return tmpsubstring( tmplength() );

  }

  else

  return ;

  }

  public boolean replaceCell(int row int col String str) throws Exception

  {

  Dispatch cell = Dispatchcall(tableCell IntegertoString(row) IntegertoString(col))toDispatch();

  Dispatchcall(cell Select);

  Dispatchput(thiscursor Text str);

  return true;

  }

  public static void main(String args[])

  {

  WordHandle word = new WordHandle();

  try

  {

  wordopen(D://doc//tax//開業登記合並事項實地調查表doc false);

  Systemoutprintln(wordgetTableNum());

  wordsetCurrentTable();

  wordreplaceCell( old Name);

  wordreplaceCell( 經營范圍);

  wordreplaceCell( );

  wordreplaceCell( );

  wordreplaceCell( );

  wordsaveAs(D://開業登記合並事項實地調查表doc);

  wordclose();

  wordquit();

  }

  catch (Exception e)

  {

  }

  }

  }

  當然要運行上面的代碼需要下載jacob下載地址為project

  還需要將jacobdll放入C:\WINDOWS\systemjacobjar放在classpath下


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26729.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.