使用JACOB读取word中的表格信息

acob的使用方法见转帖的文章

以下是读取word中表格的代码

package test;

import java.util.ArrayList;

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

import test.wordBean;

public class wordTest {

 // word文档

 private static Dispatch doc;

 // word运行程序对象

 private ActiveXComponent word;

 // 所有word文档集合

 private Dispatch documents;

 // 选定的范围或插入点

 private Dispatch selection;

 private boolean saveOnExit = true;

 public wordTest() 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();

 }
 
 /**
  *
  * 创建一个新的word文档
  *
  *
  *
  */

 public void createNewDocument() {

  doc = Dispatch.call(documents, "Add").toDispatch();

  selection = Dispatch.get(word, "Selection").toDispatch();

 }

 /**
  *
  * 打开一个已存在的文档
  *
  *
  *
  * @param docPath
  *
  */

 public void openDocument(String docPath) {

  createNewDocument();

  doc = Dispatch.call(documents, "Open", docPath).toDispatch();

  selection = Dispatch.get(word, "Selection").toDispatch();

 }

 /**
  *
  * 获得指定的单元格里数据
  *
  *
  *
  * @param tableIndex
  *
  * @param cellRowIdx
  *
  * @param cellColIdx
  *
  * @return
  *
  */

 public String getTxtFromCell(int tableIndex, int cellRowIdx, int cellColIdx) {

  // 所有表格

  Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 

  // 要填充的表格

  Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
  
  Dispatch rows = Dispatch.call(table, "Rows").toDispatch();
  
  Dispatch columns = Dispatch.call(table, "Columns").toDispatch();
  
  Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),new Variant(cellColIdx)).toDispatch();

  Dispatch Range=Dispatch.get(cell,"Range").toDispatch();
  
  System.out.println(Dispatch.get(Range,"Text").toString());
  
  Dispatch.call(cell, "Select");

  String ret = "";

  ret = Dispatch.get(selection, "Text").toString();

  ret = ret.substring(0, ret.length() - 2); // 去掉最后的回车符;

  return ret;

 }
 

 public void closeDocumentWithoutSave() {

  if (doc != null) {

   Dispatch.call(doc, "Close", new Variant(false));

   doc = null;

  }

 }

 
 /**

 * 关闭全部应用

 *

 */

 public void close() {

 //closeDocument();

 if (word != null) {

 Dispatch.call(word, "Quit");

 word = null;

 }

 selection = null;

 documents = null;

 }

 public static void main(String[] args)throws Exception{

  wordTest word = new wordTest();
  // 打开word
  word.openDocument("e:\\test.doc");
  
  ArrayList warningList = new ArrayList();
  
  // 所有表格
  Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 
  // 获取表格数目
  int tablesCount = Dispatch.get(tables,"Count").toInt();
  // 循环获取表格
  for(int i=1;i<=tablesCount;i++)
  {
   // 生产warningBean
   warningBean warning = new warningBean();
   // 获取第i个表格
   Dispatch table = Dispatch.call(tables, "Item", new Variant(i)).toDispatch();
   // 获取该表格所有行
   Dispatch rows = Dispatch.call(table, "Rows").toDispatch();
   // 获取该表格所有列
   Dispatch columns = Dispatch.call(table, "Columns").toDispatch();
   // 获取该表格行数
   int rowsCount = Dispatch.get(rows,"Count").toInt();
   // 获取该表格列数
   int columnsCount = Dispatch.get(columns,"Count").toInt();
   // 循环遍历行
   for(int j=1;j<=rowsCount;j++)
   {
    // 获取第i个表格第j行第一列标题
    String title = word.getTxtFromCell(i, j, 1);
    // 获取第i个表格第j行第二列内容
    if("漏洞名称,病毒名称,补丁名称".contains(title))
     warning.setWarningName(word.getTxtFromCell(i, j, 2));
    else if("发布日期".contains(title))
     warning.setIssuerTime(word.getTxtFromCell(i, j, 2));
    else if("漏洞级别,风险级别".contains(title))
     warning.setCategoryName(word.getTxtFromCell(i, j, 2));
    else if("领信ID".contains(title))
     warning.setLinkTrustId(word.getTxtFromCell(i, j, 2));
    else if("受影响系统/软件,受影响的操作系统,版本信息".contains(title))
     warning.setAffectedSystems(word.getTxtFromCell(i, j, 2));
    else if("漏洞简述,病毒简述,补丁简述".contains(title))
     warning.setSummary(word.getTxtFromCell(i, j, 2));
    else if("检验方法,病毒清除方法,补丁位置".contains(title))
     warning.setCheckMethod(warning.getCheckMethod()+"\r"+word.getTxtFromCell(i, j, 2));
    else if("解决措施,病毒解决,安装说明".contains(title))
     warning.setCheckMethod(warning.getCheckMethod()+"\r"+word.getTxtFromCell(i, j, 2));
    else if("参考链接,参考信息".contains(title))
     warning.setReferenceURL(word.getTxtFromCell(i, j, 2));
    
   }
   // 放入List
   warningList.add(warning);
  }  
  // 关闭该文档
  word.closeDocumentWithoutSave();
  // 关闭word
  word.close();
  
  }
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值