概述:永中Office是使用JAVA开发,所以直接使用JAVA进行二次开发比较方便,但由于在客户端运行时一般使用VB、VC去调用,所以有必要将其封装成COM组件,以便其它语言调用。
今天先将JAVA的二次开发的部分调用实例进行说明:
1.创建空的文档
public static Document createBlankWord() { Application.getInstance(); Application.gotoProduct(GlobalConstants.WORDPROCESSOR); Workbook workbook = Application.getWorkbooks().getActiveWorkbook(); Application.setMainFrameStatus(GlobalConstants.MAINFRAME_MAX); Windows windows = Application.getWindows(); if (windows.getActiveWindow() != null) windows.getActiveWindow().setState(GlobalConstants.MAINFRAME_MAX); return workbook.getDocuments().getActiveDocument(); }
2.打开文档
public static Document openWord(String fileName) { Application.getInstance(); Application.gotoProduct(GlobalConstants.WORDPROCESSOR); //Workbook workbook1 = Application.getWorkbooks().getActiveWorkbook(); Workbook workbook = Application.getWorkbooks().openWorkbook(fileName); //workbook1.close(); Application.setMainFrameStatus(GlobalConstants.MAINFRAME_MAX); return workbook.getDocuments().getActiveDocument(); }
3.关闭文档
public static boolean CloseWord() { Application.exitSystem(); return true; }
4.添加书签
public static boolean AddBookMarkToDocument(Document document, String bookMarkName) { document.getBookmarks().add(document.getRange(document.getOffset()), bookMarkName); return true; }
5.修改书签内容
public static boolean ChangBookMarkText(Document document, String bookMarkName, String text) { BookMark bk = document.getBookmarks().get(bookMarkName); boolean needReturn = false; if(bk == null) return false; if(bk.getValue().length()==0 || bk.getValue().equals("/n"))//空书签以及只包含换行符的书签需要在后面添加换行符 { needReturn = true; } long start = bk.getStartPosition(); bk.setValue(text); bk.setStartPosition(start); bk.setEndPosition(start + text.length()); if(needReturn) document.getSection(0).getBaseText().insertText(start + text.length(), "/n"); return true; }
6.输出文本
public static boolean SetTextRangeStyle(TextRange range, String fontName, double fontSize, boolean isBold) { FontAttribute attr = range.getFontAttribute(); attr.setFontName(fontName); attr.setFontSize(fontSize); attr.setBold(isBold); range.setFontAttribute(attr); return true; }
7.得到Shape文本内容
public static String GetShapeText(String shapeName) { Shape shape = Application.getWorkbooks().getActiveWorkbook().getActiveShapes().getShape(shapeName); if(shape==null) return ""; return shape.getShapeText().getText(); }
8.设置Shape文本内容
public static boolean SetShapeText(String shapeName, String text) { Shape shape = Application.getWorkbooks().getActiveWorkbook().getActiveShapes().getShape(shapeName); if(shape==null) return false; shape.getShapeText().clearAll(); shape.getShapeText().insertText(text); return true; }