`
- 浏览:
420870 次
- 性别:
- 来自:
云南
-
- 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操作word 文档的方法集!(含创建目录),使用的Jacob版本是1.5
如果你要对 MS Word、Excel 进行处理,JACOB 是一个好的选择。JACOB目前已经成为sourceforge(http://sourceforge.net/projects/jacob- project/)的一个开源项目,本文使用的版本是1.10.1。 这篇文章可能不能完全...
该word中是一些关于jacob如何操作word的范例代码!互相交流学习
载Jacob_1.11_zip,终于把Jacob处理Word文档搞定了
java jacob操作word,往word中插入文档文件等等功能webservice
NULL 博文链接:https://clq9761.iteye.com/blog/1284318
Jacob控制word文档,用java组件生成word
非常实用哦,对于入门者更是只剩的法宝哦。还在等什么,快下载吧。
可以给Word文档使用jacob控件来添加页脚,然而使用jacob来处理Word文档的页脚自己找了很久都没能找到解决的办法,只有自己动手解决了。 此函数可以自动把页码作为文档的页脚,页脚可以随着页码的值自动改变,页码的...
java使用jacob工具操作word文档,读取写入文档。详细的介绍使用方式。
ava_jacob_操作word_文档_进行写操作_如生成表格_添加_图片.docx
/** word文档 * * 在本类中有两种方式可以进行文档的创建, * 第一种调用 createNewDocument * 第二种调用 openDocument */
任务目的 1自动生成word文档目录。 用例测试操作步骤 在一个word文档的第二页填写占位符: {目录}保存。调用程序读取目标文档,自动根据标题生成目录到{目录}位置。
一次使用jacob在服务器端操作word文档的总结
jacob 导出word 整理 ............
运用jacob将word文件转成pdf文件,word文件支持xml格式,首先将文档中的dll放到jdk的bin文件夹下,运行java文件即可
java项目, 直接导入eclipse里面或者myeclipse里面就可以运行....通过jacob插件,通过word模板自动填充相应的数据,并且产生文件,为了解决有的tomcat下会乱码, 生成完成之后会转码一下, 解决了中文乱码问题.
java_jacob_操作word_文档,进行写操作.docx