0001-WordBeanSimple 类如何建立

本文介绍如何利用Jacob库操作MSword,包括新建文档、打开已有文档、移动光标、查找并替换文本以及文件保存或另存为等操作。通过实例演示了如何在模板文件基础上进行内容替换,并最终将修改后的文件保存到指定路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



package test;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * jacob操作MSword类
 *
 * @author
 */
public class WordBeanSimple {
 // word文档
 private Dispatch doc;
 // word运行程序对象
 private ActiveXComponent word;
 // 所有word文档集合
 private Dispatch documents;
 // 选定的范围或插入点
 private Dispatch selection;
 private boolean saveOnExit = true;

 public WordBeanSimple() throws Exception {
  if (word == null) {
   word = new ActiveXComponent("Word.Application");
   word.setProperty("Visible", new Variant(false)); // 不可见打开word
   word.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
  }
  if (documents == null)
   documents = word.getProperty("Documents").toDispatch();
 }

 /**
  * 设置退出时参数
  *
  * @param saveOnExit
  *            boolean true-退出时保存文件,false-退出时不保存文件
  */
 public void setSaveOnExit(boolean saveOnExit) {
  this.saveOnExit = saveOnExit;
 }

 /**
  * 创建一个新的word文档
  *
  */
 public void createNewDocument() {
  doc = Dispatch.call(documents, "Add").toDispatch();
  selection = Dispatch.get(word, "Selection").toDispatch();
 }

 /**
  * 打开一个已存在的文档
  *
  * @param docPath
  */
 public void openDocument(String docPath) {
  doc = Dispatch.call(documents, "Open", docPath).toDispatch();
  selection = Dispatch.get(word, "Selection").toDispatch();
 }

 /**
  * 把插入点移动到文件首位置
  *
  */
 public void moveStart() {
  if (selection == null)
   selection = Dispatch.get(word, "Selection").toDispatch();
  Dispatch.call(selection, "HomeKey", new Variant(6));
 }

 /**
  * 从选定内容或插入点开始查找文本
  *
  * @param toFindText
  *            要查找的文本
  * @return boolean true-查找到并选中该文本,false-未查找到文本
  */
 @SuppressWarnings("static-access")
 public boolean find(String toFindText) {
  if (toFindText == null || toFindText.equals(""))
   return false;
  // 从selection所在位置开始查询
  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();
 }

 /**
  * 把选定选定内容设定为替换文本
  *
  * @param toFindText
  *            查找字符串
  * @param newText
  *            要替换的内容
  * @return
  */
 public boolean replaceText(String toFindText, String newText) {
  this.moveStart();
  if (!find(toFindText))
   return false;
  Dispatch.put(selection, "Text", newText);
  return true;
 }

 /**
  * 文件保存或另存为
  *
  * @param savePath
  *            保存或另存为路径
  */
 public void save(String savePath) {
  Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),
    "FileSaveAs", savePath);
 }

 /**
  * 关闭全部应用
  *
  */
 public void close() {
  // closeDocument();
  if (word != null) {
   Dispatch.call(word, "Quit");
   word = null;
  }
  selection = null;
  documents = null;
 }

 public void openWord(boolean makeVisible) {

  // 启动com的线程
  ComThread.InitSTA();
  // 打開word(如果word未開啟時)
  if (word == null) {
   word = new ActiveXComponent("Word.Application");
  }

  // 設置word是可見或不可見(true:顯示;false:不顯示)
  Dispatch.put(word, "Visible", new Variant(makeVisible));
 }

 public static void main(String[] args) throws Exception {
  WordBeanSimple word2 = new WordBeanSimple();
  String templetPath = "d:\\ssss.doc"; // 模版文件
  String otPath = "d:\\11122.doc"; // 保存文件
  try {
   // 是否显示打开word
   word2.openWord(false);
   // 打开模版文件
   word2.openDocument(templetPath);
   word2.replaceText("$AGE134567$", "18");
   word2.replaceText("$NAME$", "YOUANDME");
   
   
   
   

   // 替换书签内容
   // word2.replaceBookMark("Prj_Name", "项目");
   // 保存到path
   word2.save(otPath);

  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   // 关闭Word
   word2.close();
  }

 }
}

内容概要:该论文聚焦于T2WI核磁共振图像超分辨率问题,提出了一种利用T1WI模态作为辅助信息的跨模态解决方案。其主要贡献包括:提出基于高频信息约束的网络框架,通过主干特征提取分支和高频结构先验建模分支结合Transformer模块和注意力机制有效重建高频细节;设计渐进式特征匹配融合框架,采用多阶段相似特征匹配算法提高匹配鲁棒性;引入模型量化技术降低推理资源需求。实验结果表明,该方法不仅提高了超分辨率性能,还保持了图像质量。 适合人群:从事医学图像处理、计算机视觉领域的研究人员和工程师,尤其是对核磁共振图像超分辨率感兴趣的学者和技术开发者。 使用场景及目标:①适用于需要提升T2WI核磁共振图像分辨率的应用场景;②目标是通过跨模态信息融合提高图像质量,解决传统单模态方法难以克服的高频细节丢失问题;③为临床诊断提供更高质量的影像资料,帮助医生更准确地识别病灶。 其他说明:论文不仅提供了详细的网络架构设计与实现代码,还深入探讨了跨模态噪声的本质、高频信息约束的实现方式以及渐进式特征匹配的具体过程。此外,作者还对模型进行了量化处理,使得该方法可以在资源受限环境下高效运行。阅读时应重点关注论文中提到的技术创新点及其背后的原理,理解如何通过跨模态信息融合提升图像重建效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值