- package com.rsj.test;
-
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
-
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.Dispatch;
- import com.jacob.com.Variant;
- public class JacobWordUtil {
-
-
-
-
-
-
-
- public static void replaceWord(String textPath, String targetWord,
- String replaceWord) {
- ActiveXComponent app = new ActiveXComponent("Word.Application");
- try {
- app.setProperty("Visible", new Variant(false));
-
- Dispatch docs = app.getProperty("Documents").toDispatch();
-
- Dispatch doc = Dispatch.invoke(
- docs,
- "Open",
- Dispatch.Method,
- new Object[] { textPath, new Variant(false),
- new Variant(false) }, new int[1]).toDispatch();
-
-
-
- Dispatch selection = app.getProperty("Selection").toDispatch();
-
- Dispatch.call(selection, "HomeKey", new Variant(6));
-
- Dispatch find = Dispatch.call(selection, "Find").toDispatch();
-
- Dispatch.put(find, "Text", targetWord);
-
- Dispatch.call(find, "Execute");
-
- Dispatch.put(selection, "Text", replaceWord);
-
- Dispatch.call(doc, "Save");
-
- Dispatch.call(doc, "Close", new Variant(false));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- app.invoke("Quit", new Variant[] {});
- app.safeRelease();
- }
- }
-
-
-
-
-
-
- public static void createWordFile(String txtContent, String fileName) {
- ActiveXComponent app = new ActiveXComponent("Word.Application");
-
- try {
- app.setProperty("Visible", new Variant(false));
- Dispatch docs = app.getProperty("Documents").toDispatch();
- Dispatch doc = Dispatch.call(docs, "Add").toDispatch();
- Dispatch selection = Dispatch.get(app, "Selection").toDispatch();
- Dispatch.put(selection, "Text", txtContent);
- Dispatch.call(Dispatch.call(app, "WordBasic").getDispatch(),
- "FileSaveAs", fileName);
- Variant f = new Variant(false);
- Dispatch.call(doc, "Close", f);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- app.invoke("Quit", new Variant[] {});
- app.safeRelease();
- }
- }
-
-
-
-
-
-
- public static void createWordWithTxt(String txt, String wordFile) {
- String txtContent = null;
- try {
- txtContent = bufferedReader(txt);
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- createWordFile(txtContent, wordFile);
- }
-
-
-
-
-
-
-
- public static String bufferedReader(String path) throws IOException {
- File file = new File(path);
- if (!file.exists() || file.isDirectory())
- throw new FileNotFoundException();
- BufferedReader br = new BufferedReader(new FileReader(file));
- String temp = null;
- StringBuffer sb = new StringBuffer();
- temp = br.readLine();
- while (temp != null) {
- sb.append(temp + " ");
- temp = br.readLine();
- }
- return sb.toString();
- }
-
-
-
-
-
-
-
- public static void insertImage(String wordFile, String imagePath,
- String tarStr) {
- ActiveXComponent app = new ActiveXComponent("Word.Application");
- try {
- app.setProperty("Visible", new Variant(false));
-
- Dispatch docs = app.getProperty("Documents").toDispatch();
-
- Dispatch doc = Dispatch.invoke(
- docs,
- "Open",
- Dispatch.Method,
- new Object[] { wordFile, new Variant(false),
- new Variant(false) }, new int[1]).toDispatch();
-
-
-
- Dispatch selection = app.getProperty("Selection").toDispatch();
-
- Dispatch.call(selection, "HomeKey", new Variant(6));
-
- Dispatch find = Dispatch.call(selection, "Find").toDispatch();
-
- Dispatch.put(find, "Text", tarStr);
-
- Dispatch.call(find, "Execute");
-
- Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
- "AddPicture", imagePath);
-
- Dispatch.call(doc, "Save");
-
- Dispatch.call(doc, "Close", new Variant(false));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- app.invoke("Quit", new Variant[] {});
- app.safeRelease();
- }
- }
-
-
-
-
-
-
-
-
- public static void createTable(String wordFile, String pos, int numCols,
- int numRows) {
- ActiveXComponent app = new ActiveXComponent("Word.Application");
- Dispatch selection = null;
- Dispatch doc = null;
- Dispatch docs = null;
- boolean b = false;
- try {
- app.setProperty("Visible", new Variant(false));
-
- docs = app.getProperty("Documents").toDispatch();
-
- doc = Dispatch.invoke(
- docs,
- "Open",
- Dispatch.Method,
- new Object[] { wordFile, new Variant(false),
- new Variant(false) }, new int[1]).toDispatch();
-
-
-
- selection = app.getProperty("Selection").toDispatch();
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (pos == null || pos.equals(""))
- b = false;
-
- Dispatch find = app.call(selection, "Find").toDispatch();
-
- Dispatch.put(find, "Text", pos);
-
- Dispatch.put(find, "Forward", "True");
-
- Dispatch.put(find, "Format", "True");
-
- Dispatch.put(find, "MatchCase", "True");
-
- Dispatch.put(find, "MatchWholeWord", "True");
-
- b = Dispatch.call(find, "Execute").getBoolean();
-
- if (b) {
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
- Dispatch range = Dispatch.get(selection, "Range").toDispatch();
- Dispatch newTable = Dispatch.call(tables, "Add", range,
- new Variant(numRows), new Variant(numCols)).toDispatch();
- Dispatch.call(selection, "MoveRight");
- } else
- System.out.println("沒有找到指定的位置,請檢查是否存在這樣的位置。");
- try {
- Dispatch.call(doc, "Save");
- Dispatch.call(doc, "Close", new Variant(false));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- app.invoke("Quit", new Variant[] {});
- app.safeRelease();
- }
- }
-
-
-
- public static void main(String[] args) {
-
-
- }
- }
|