`
- 浏览:
427931 次
- 性别:
- 来自:
云南
-
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.Dispatch;
- import com.jacob.com.Variant;
-
-
-
-
-
-
-
- public class WordBean {
-
- private Dispatch doc;
-
-
- private ActiveXComponent word;
-
-
- private Dispatch documents;
-
-
- private Dispatch selection;
-
-
- private boolean saveOnExit = true;
-
- public WordBean() {
- if (word == null) {
- word = new ActiveXComponent("Word.Application");
- word.setProperty("Visible", new Variant(false));
- }
- if (documents == null)
- documents = word.getProperty("Documents").toDispatch();
- }
-
-
-
-
-
-
-
- public void setSaveOnExit(boolean saveOnExit) {
- this.saveOnExit = saveOnExit;
- }
-
-
-
-
-
- public void createNewDocument() {
- doc = Dispatch.call(documents, "Add").toDispatch();
- selection = Dispatch.get(word, "Selection").toDispatch();
- }
-
-
-
-
-
-
- public void openDocument(String docPath) {
- closeDocument();
- doc = Dispatch.call(documents, "Open", docPath).toDispatch();
- selection = Dispatch.get(word, "Selection").toDispatch();
- }
-
-
-
-
-
-
-
- public void moveUp(int pos) {
- if (selection == null)
- selection = Dispatch.get(word, "Selection").toDispatch();
- for (int i = 0; i < pos; i++)
- Dispatch.call(selection, "MoveUp");
-
- }
-
-
-
-
-
-
-
- public void moveDown(int pos) {
- if (selection == null)
- selection = Dispatch.get(word, "Selection").toDispatch();
- for (int i = 0; i < pos; i++)
- Dispatch.call(selection, "MoveDown");
- }
-
-
-
-
-
-
-
- public void moveLeft(int pos) {
- if (selection == null)
- selection = Dispatch.get(word, "Selection").toDispatch();
- for (int i = 0; i < pos; i++) {
- Dispatch.call(selection, "MoveLeft");
- }
- }
-
-
-
-
-
-
-
- public void moveRight(int pos) {
- if (selection == null)
- selection = Dispatch.get(word, "Selection").toDispatch();
- for (int i = 0; i < pos; i++)
- Dispatch.call(selection, "MoveRight");
- }
-
-
-
-
-
- public void moveStart() {
- if (selection == null)
- selection = Dispatch.get(word, "Selection").toDispatch();
- Dispatch.call(selection, "HomeKey", new Variant(6));
- }
-
-
-
-
-
-
-
-
- public boolean find(String toFindText) {
- if (toFindText == null || toFindText.equals(""))
- return false;
-
- Dispatch find = word.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();
- }
-
-
-
-
-
-
-
-
-
-
- public boolean replaceText(String toFindText, String newText) {
- if (!find(toFindText))
- return false;
- Dispatch.put(selection, "Text", newText);
- return true;
- }
-
-
-
-
-
-
-
-
-
- public void replaceAllText(String toFindText, String newText) {
- while (find(toFindText)) {
- Dispatch.put(selection, "Text", newText);
- Dispatch.call(selection, "MoveRight");
- }
- }
-
-
-
-
-
-
-
- public void insertText(String newText) {
- Dispatch.put(selection, "Text", newText);
- }
-
-
-
-
-
-
-
-
-
- public boolean replaceImage(String toFindText, String imagePath) {
- if (!find(toFindText))
- return false;
- Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
- "AddPicture", imagePath);
- return true;
- }
-
-
-
-
-
-
-
-
-
- public void replaceAllImage(String toFindText, String imagePath) {
- while (find(toFindText)) {
- Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
- "AddPicture", imagePath);
- Dispatch.call(selection, "MoveRight");
- }
- }
-
-
-
-
-
-
-
- public void insertImage(String imagePath) {
- Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
- "AddPicture", imagePath);
- }
-
-
-
-
-
-
-
-
-
-
- public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx,
- int secCellRowIdx, int secCellColIdx) {
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
- Dispatch fstCell = Dispatch.call(table, "Cell",
- new Variant(fstCellRowIdx), new Variant(fstCellColIdx))
- .toDispatch();
- Dispatch secCell = Dispatch.call(table, "Cell",
- new Variant(secCellRowIdx), new Variant(secCellColIdx))
- .toDispatch();
- Dispatch.call(fstCell, "Merge", secCell);
- }
-
-
-
-
-
-
-
-
-
- public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx,
- String txt) {
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
- Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
- new Variant(cellColIdx)).toDispatch();
- Dispatch.call(cell, "Select");
- Dispatch.put(selection, "Text", txt);
- }
-
-
-
-
- public void pasteExcelSheet(String pos){
- moveStart();
- if (this.find(pos)) {
- Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
- Dispatch.call(textRange, "Paste");
- }
- }
-
-
-
-
-
-
-
-
-
- public void copyTable(String pos, int tableIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
- Dispatch range = Dispatch.get(table, "Range").toDispatch();
- Dispatch.call(range, "Copy");
- if (this.find(pos)) {
- Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
- Dispatch.call(textRange, "Paste");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex,
- String pos) {
- Dispatch doc2 = null;
- try {
- doc2 = Dispatch.call(documents, "Open", anotherDocPath)
- .toDispatch();
-
- Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item",
- new Variant(tableIndex)).toDispatch();
- Dispatch range = Dispatch.get(table, "Range").toDispatch();
- Dispatch.call(range, "Copy");
- if (this.find(pos)) {
- Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
- Dispatch.call(textRange, "Paste");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (doc2 != null) {
- Dispatch.call(doc2, "Close", new Variant(saveOnExit));
- doc2 = null;
- }
- }
- }
-
-
-
-
-
-
- public void copyImageFromAnotherDoc(String anotherDocPath,int shapeIndex,String pos){
- Dispatch doc2 = null;
- try {
- doc2 = Dispatch.call(documents, "Open", anotherDocPath)
- .toDispatch();
- Dispatch shapes = Dispatch.get(doc2, "InLineShapes").toDispatch();
- Dispatch shape = Dispatch.call(shapes, "Item", new Variant(shapeIndex)).toDispatch();
- Dispatch imageRange = Dispatch.get(shape, "Range").toDispatch();
- Dispatch.call(imageRange, "Copy");
- if (this.find(pos)) {
- Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
- Dispatch.call(textRange, "Paste");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (doc2 != null) {
- Dispatch.call(doc2, "Close", new Variant(saveOnExit));
- doc2 = null;
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
- public void createTable(String pos, int numCols, int numRows) {
- if (find(pos)) {
- 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{
- 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");
- }
- }
-
-
-
-
-
-
-
-
-
- public void addTableRow(int tableIndex, int rowIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
-
- Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
- Dispatch row = Dispatch.call(rows, "Item", new Variant(rowIndex))
- .toDispatch();
- Dispatch.call(rows, "Add", new Variant(row));
- }
-
-
-
-
-
-
-
- public void addFirstTableRow(int tableIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
-
- Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
- Dispatch row = Dispatch.get(rows, "First").toDispatch();
- Dispatch.call(rows, "Add", new Variant(row));
- }
-
-
-
-
-
-
-
- public void addLastTableRow(int tableIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
-
- Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
- Dispatch row = Dispatch.get(rows, "Last").toDispatch();
- Dispatch.call(rows, "Add", new Variant(row));
- }
-
-
-
-
-
-
- public void addRow(int tableIndex) {
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
-
- Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
- Dispatch.call(rows, "Add");
- }
-
-
-
-
-
-
-
- public void addCol(int tableIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
-
- Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
- Dispatch.call(cols, "Add").toDispatch();
- Dispatch.call(cols, "AutoFit");
- }
-
-
-
-
-
-
-
-
-
- public void addTableCol(int tableIndex, int colIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
-
- Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
- System.out.println(Dispatch.get(cols, "Count"));
- Dispatch col = Dispatch.call(cols, "Item", new Variant(colIndex))
- .toDispatch();
-
- Dispatch.call(cols, "Add", col).toDispatch();
- Dispatch.call(cols, "AutoFit");
- }
-
-
-
-
-
-
-
- public void addFirstTableCol(int tableIndex) {
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
-
- Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
- Dispatch col = Dispatch.get(cols, "First").toDispatch();
- Dispatch.call(cols, "Add", col).toDispatch();
- Dispatch.call(cols, "AutoFit");
- }
-
-
-
-
-
-
-
- public void addLastTableCol(int tableIndex) {
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
-
- Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
- Dispatch col = Dispatch.get(cols, "Last").toDispatch();
- Dispatch.call(cols, "Add", col).toDispatch();
- Dispatch.call(cols, "AutoFit");
- }
-
-
-
-
- public void autoFitTable(){
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
- int count = Dispatch.get(tables, "Count").toInt();
- for(int i=0;i<count;i++){
- Dispatch table = Dispatch.call(tables, "Item", new Variant(i+1))
- .toDispatch();
- Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
- Dispatch.call(cols, "AutoFit");
- }
- }
-
-
-
-
- public void callWordMacro(){
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
- int count = Dispatch.get(tables, "Count").toInt();
- Variant vMacroName = new Variant("Normal.NewMacros.tableFit");
- Variant vParam = new Variant("param1");
- Variant para[]=new Variant[]{vMacroName};
- for(int i=0;i<count;i++){ Dispatch table = Dispatch.call(tables, "Item", new Variant(i+1))
- .toDispatch();
- Dispatch.call(table, "Select");
- Dispatch.call(word,"Run","tableFitContent");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void setFont(boolean bold, boolean italic, boolean underLine,
- String colorSize, String size, String name) {
- Dispatch font = Dispatch.get(selection, "Font").toDispatch();
- Dispatch.put(font, "Name", new Variant(name));
- Dispatch.put(font, "Bold", new Variant(bold));
- Dispatch.put(font, "Italic", new Variant(italic));
- Dispatch.put(font, "Underline", new Variant(underLine));
- Dispatch.put(font, "Color", colorSize);
- Dispatch.put(font, "Size", size);
- }
-
-
-
-
-
-
-
- public void save(String savePath) {
- Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),
- "FileSaveAs", savePath);
- }
-
-
-
-
-
- public void closeDocument(int val) {
- Dispatch.call(doc, "Close", new Variant(val));
- doc = null;
- }
-
-
-
-
- public void closeDocument() {
- if (doc != null) {
- Dispatch.call(doc, "Save");
- Dispatch.call(doc, "Close", new Variant(saveOnExit));
- doc = null;
- }
- }
-
-
-
-
-
- public void close() {
-
- if (word != null) {
- Dispatch.call(word, "Quit");
- word = null;
- }
- selection = null;
- documents = null;
- }
-
-
-
-
-
- public void printFile() {
- if (doc != null) {
- Dispatch.call(doc, "PrintOut");
- }
- }
-
-
- public String getContent(int tableIndex, int cellRowIdx, int cellColIdx){
-
- Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
- .toDispatch();
- Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
- new Variant(cellColIdx)).toDispatch();
- Dispatch Range=Dispatch.get(cell,"Range").toDispatch();
- return Dispatch.get(Range,"Text").toString();
-
- }
-
- public static void main(String args[]) {
- WordBean msWordManager = new WordBean();
- try {
-
-
-
-
-
-
-
-
-
-
-
-
- msWordManager.openDocument("c:\\asdf1.doc");
- msWordManager.addRow(1);
- msWordManager.putTxtToCell(1, 4, 1, "gaga");
- } catch (Exception e)
- e.printStackTrace();
- }finally{
- msWordManager.close();
- }
- }
-
- }
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Jacob 操作 Word 文档详解 Jacob 操作 Word 文档概述 Jacob 是一个 Java 库,用于访问和操作 Microsoft Office 应用程序,包括 Word。它提供了一个基于 COM(Component Object Model)的接口,允许 Java 应用...
在这个“Jacob操作word文档的方法集”中,我们将探讨如何使用Jacob 1.5版本来创建和操作Word文档,特别是创建目录。 首先,为了使用Jacob,你需要在你的项目中添加Jacob的jar文件。这通常涉及到将库添加到项目的类...
下面将详细阐述Jacob操作Word文档的核心知识点。 1. **Jacob简介** Jacob全称为Java-COM Bridge,是一个开源项目,它的主要作用是提供了一个桥梁,使得Java应用程序能够调用COM组件,进而与Windows系统上的Office...
在给定的代码片段中,`GetWord` 类提供了使用Jacob操作Word文档的一些基本功能。 首先,`GetWord` 类初始化了一个`ActiveXComponent` 对象 `word`,这是Jacob库中的一个关键类,用于实例化COM对象,这里是Microsoft...
本文主要探讨如何使用Java-JACOB来创建和操作Word文档。 首先,使用JACOB之前,确保你的系统中已经安装了Microsoft Office,并且配置了相应的COM组件。JACOB本身是一个开源项目,可以在SourceForge上找到...
载Jacob_1.11_zip,终于把Jacob处理Word文档搞定了
在处理Word文档时,我们可以利用Jacob调用Word的COM组件,例如Microsoft.Office.Interop.Word,来实现对Word文档的操作。 要使用Jacob,你需要首先在项目中添加Jacob的库文件。在提供的压缩包`jacob-1.15-M2.zip`中...
### jacob给Word文档添加页脚 在日常工作中,Word文档是经常被使用的工具之一,尤其是在需要编写报告、论文或专业文档时。为了使文档更加规范和专业,往往需要添加页眉、页脚等元素。其中,页脚常用于显示页码、...
在本文中,我们将深入探讨如何使用Jacob在Word文档中插入其他文档。 首先,理解Java Jacob的基本使用是至关重要的。Jacob通过COM(Component Object Model)接口与Windows操作系统中的Office应用进行通信。因此,要...
在“如何使用jacob将word转换为PDF.doc”这个示例中,我们将学习如何使用Jacob将Word文档转换成PDF格式。以下是基本步骤: 1. **设置Jacob环境**:首先,你需要在项目中添加Jacob的jar文件,并确保系统路径中包含...
#### 二、Java Jacob 操作 Word 文档的基本流程 1. **初始化 Word 应用程序:** - 创建一个 `ActiveXComponent` 实例,表示 Word 应用程序。 - 设置 Word 应用程序是否可见,这通常取决于是否需要在前台显示 Word...
Jacob允许Java应用程序通过COM接口与Windows操作系统中的Office应用进行通信,从而实现对Word文档的操作。 Jacob库的核心功能是它提供了Java到COM接口的桥接,使得开发者可以调用Office应用的API来处理Word文档。...
标题“jacob读取word表格”涉及到的是使用Jacob库来操作Microsoft Word文档,特别是从中读取表格数据。Jacob是一个Java到COM桥接库,允许Java应用程序调用COM组件,如Microsoft Office的应用程序。 在Java中,直接...
本篇文章将深入探讨如何利用JACOB来自动生成Word文档的目录。 首先,我们需要了解什么是目录(TOC,Table of Contents)。在Word文档中,目录是一份列出文档主要部分及其对应页码的索引,帮助读者快速定位到他们感...
在这个场景下,我们关注的是使用Jacob库来实现对Word文档的高级操作,特别是通过目录结构分析和读取文档内容。Jacob是一个Java COM自动化桥接库,它允许Java应用程序利用微软Office应用的COM接口,包括Word。 首先...
总结来说,Java结合JACOB工具提供了一种有效的方法来操作Word文档,无论是读取还是写入,都为开发者提供了便利。然而,这种方式有一定的局限性,特别是在跨平台应用上。通过理解这些基本操作,你可以根据需求灵活地...
在这个场景中,我们将讨论如何利用JACOB将Word文档转换为PDF。 首先,我们需要理解JACOB的工作原理。JACOB是通过Java的JNI(Java Native Interface)来调用COM接口的,这样就可以在Java代码中直接操纵COM对象,例如...
在这个特定的情况下,Jacob被用来调用打印机来打印Word文档。这个过程涉及到几个关键的知识点: 1. **Jacob库**:Jacob全称为Java COM Bridge,是一个开源的Java库,通过JNI(Java Native Interface)技术实现,...
NULL 博文链接:https://clq9761.iteye.com/blog/1284318