word的模板
package com.ws.base.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.star.container.XNameAccess;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.table.XTableRows;
import com.sun.star.text.XBookmarksSupplier;
import com.sun.star.text.XSentenceCursor;
import com.sun.star.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextTable;
import com.sun.star.text.XTextTablesSupplier;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.ws.base.bootstrapconnector.BootstrapSocketConnector;
public class TestAction {
private static Log logger = LogFactory.getLog(TestAction.class);
/**
* open office的安装路径;
*/
private static String OPENOFFICE_PATH = "C:/Program Files (x86)/OpenOffice 4/program/";
private static XTextDocument mxDoc;
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// 读取文件
XComponentContext xContext = null;
try {
// get the remote office component context
xContext = BootstrapSocketConnector.bootstrap(OPENOFFICE_PATH);
System.out.println("Connected to a running office ...");
// get the remote office service manager
XMultiComponentFactory xMCF = xContext.getServiceManager();
Object desktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
XComponentLoader xCompLoader = (XComponentLoader) UnoRuntime
.queryInterface(XComponentLoader.class, desktop);
args = new String[] { "E:\\1.odt" };
// args=new String[]{"private:factory/swriter"};
String sUrl = args[0];
if (sUrl.indexOf("private:") != 0) {
java.io.File sourceFile = new java.io.File(args[0]);
StringBuffer sbTmp = new StringBuffer("file:///");
sbTmp.append(sourceFile.getCanonicalPath().replace('\\', '/'));
sUrl = sbTmp.toString();
}
// Load a Writer document, which will be automaticly displayed
com.sun.star.lang.XComponent xComp = xCompLoader
.loadComponentFromURL(sUrl, "_blank", 0,
new com.sun.star.beans.PropertyValue[0]);
if (xComp != null) {
mxDoc = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, xComp);
//XText mxDocText = mxDoc.getText();
// 获取要插入标签的值
Map contentMap = new HashMap();
contentMap.put("projectName", "111");
contentMap.put("projectCode", "22222");
insertStrToBookMark(contentMap);
// 表格数据
String[][] tableValueArr = {
{ "2017", "4000.00", "4000.00", "0.00", "0.00" },
{ "合计", "4000.00", "4000.00", "0.00", "0.00" } };
Map tableMap = new HashMap();
tableMap = setTableValue(tableMap, "table1", true,
tableValueArr);
insertTable(tableMap);
} else {
System.exit(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @Description:插入字符串到word文件书签中;
* @Title: insertStrToBookMark
* @param @param contentMap
* @param @throws Exception 设定文件
* @return void 返回类型
* @throws
*/
public static void insertStrToBookMark(Map contentMap)
throws Exception {
try {
XText mxDocText = mxDoc.getText();
// 获取标签集合
XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier) UnoRuntime
.queryInterface(XBookmarksSupplier.class, mxDoc);
XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks();
if (null == xNamedBookmarks) {
return;
}
// 遍历标签集合,并在标签处插入相应的字符串
for (String bookMark : xNamedBookmarks.getElementNames()) {
// 判断是否包含要替换内容的书签
if (contentMap.containsKey(bookMark)) {
// 从书签位置处开始插入、删除或更改文字或属性
XTextContent xTextContent = (XTextContent) UnoRuntime
.queryInterface(XTextContent.class,
xNamedBookmarks.getByName(bookMark));
// 获取书签位置
XTextCursor mxDocCursor = mxDocText
.createTextCursorByRange(xTextContent.getAnchor());
XSentenceCursor xSentenceCursor = (XSentenceCursor) UnoRuntime
.queryInterface(XSentenceCursor.class, mxDocCursor);
// 插入
mxDocText.insertString(xSentenceCursor,
contentMap.get(bookMark), true);
}
}
} catch (Exception e) {
logger.info("插入字符串到文档标签中出错", e);
}
}
/**
*
* @Description: 插入字表格到word文件中;
* @Title: insertTable
* @param @param mxDoc
* @param @param tableMap
* @param @throws Exception 设定文件
* @return void 返回类型
* @throws
*/
@SuppressWarnings("unchecked")
public static void insertTable(Map tableMap) throws Exception {
try {
// 首先从文档中查询 XTextTablesSupplier 接口
XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime
.queryInterface(XTextTablesSupplier.class, mxDoc);
// 获取表格集合
XNameAccess xNamedTables = xTablesSupplier.getTextTables();
// 取表以及表数据 并赋值
if (null == xNamedTables) {
return;
}
for (String tableName : xNamedTables.getElementNames()) {
if (tableMap.containsKey(tableName)) {
XTextTable xTable = (XTextTable) UnoRuntime
.queryInterface(XTextTable.class,
xNamedTables.getByName(tableName));
List
运行效果: