java中替换 杠_利用替换法, 生成指定模板的 Word 文档 (Java)

/**

* @(#) WordBean.java 2006-9-18

* Copyright 2006 Banny. All rights reserved.

*/

package edu.gdcc.banny;

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

/**

* @author Banny

*

*/

public class WordBean {

private static final String ZIP_TEMPLATE = "${zipcode}";

private static final String ADDR_TEMPLATE = "${addr}";

private static final String S_ADDR_TEMPLATE = "${s_addr}";

private static final String TARGET = "${target}";

private static final Variant V_FALSE = new Variant(

false);

private static final Variant V_TRUE = new Variant(true);

private static final Variant REPLACE = new Variant(1);

private static final Variant WRAP = new Variant(1);

private String path;

private boolean visible;

private static ActiveXComponent application;

private static Object appLock = new Object();

private Dispatch document;

private Dispatch selection;

private Dispatch find;

public WordBean() {

this(null);

}

public WordBean(String path) {

this(path, false);

}

public WordBean(String path, boolean visible) {

this.path = path;

this.visible = visible;

}

public void openDoc() {

openDoc(path);

}

public void openDoc(String path) {

if (path == null)

throw new NullPointerException(

"没有指定 Word 文档的路径");

openApp();

application.setProperty("Visible", new Variant(

visible));

document = Dispatch.invoke(

application.getProperty("Documents")

.toDispatch(),

"Open",

Dispatch.Method,

new Object[] { path, new Variant(false),

new Variant(true) }, new int[1])

.toDispatch();

selection = application.getProperty("Selection")

.toDispatch();

find = Dispatch.call(selection, "Find")

.toDispatch();

}

private static void openApp() {

if (application == null) {

synchronized (appLock) {

if (application == null) {

application = new ActiveXComponent(

"Word.Application");

}

}

}

}

public void saveDoc(String path) {

if (path.trim().equals(this.path.trim()))

throw new RuntimeException("目标文件不能与源文件为同一个文件");

Dispatch.call(document, "SaveAS", path);

}

public void closeDoc() {

Dispatch.call(document, "Close", new Variant(0));

closeApp();

}

private void closeApp() {

Dispatch.call(application, "Quit");

application = null;

document = null;

}

public void add(User user) {

String zip = user.getZipcode();

String addr = user.getAddr();

String s_addr = user.getSaddr();

String target = user.getTarget();

if (zip == null || zip.trim().equals("")

|| addr == null || addr.trim().equals("")

|| s_addr == null

|| s_addr.trim().equals("")) {

System.err.println(user);

return;

}

if (document == null)

throw new NullPointerException("没有打开文档");

Dispatch.call(find, "ClearFormatting");

Dispatch replacement = Dispatch.call(find,

"Replacement").toDispatch();

Dispatch.call(replacement, "ClearFormatting");

replace(ZIP_TEMPLATE, zip);

replace(ADDR_TEMPLATE, addr);

replace(S_ADDR_TEMPLATE, s_addr);

replace(TARGET, target);

}

private void replace(String oldStr, String newStr) {

Variant findText = new Variant(oldStr);

Variant replaceWith = new Variant(newStr);

Dispatch.callN(find, "Execute", new Variant[] {

findText, V_TRUE, V_FALSE, V_FALSE,

V_FALSE, V_FALSE, V_TRUE, WRAP, V_FALSE,

replaceWith, REPLACE });

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值