java 操作visio-- com4j方式

visio是一款强大的软件,制作各种图表,流程图等,近段时间研究visio摆放平面图,想把平面图中数据保存到数据库中,网上资料极少,研究了很多。特此写下供大家分享!

希望大家多回贴,以此表达尊重和鼓励我的劳动成果。

第一步,下载com4j包 ,可以我的资源里直接下载。

 

第二步,解压缩com4j包,把args4j-2.0.1.jar,tlbimp.jar,com4j.jar放入JDK的bin目录下。


第三步,cmd进入JDK安装目录,运行 java -jar tlbimp.jar -o visio -p test  "D:\Program Files\Microsoft Office\Office14\VISLIB.DLL"。在JDK安装目录出现一套操作visio的类库

你可以省略一二步,在我的资源里下载 visio类库。

第四步,操作visio.看代码

 

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

public class VisioMain {
 
 public static void main(String[] args){
  String filePath = "C:\\network.vsd";
  String outputDir = filePath + ".output";
  // 创建Visio对象
  IVApplication app = ClassFactory.createApplication();
  // Visio对象设置为可见
  app.visible(true);
  // 打开一个Visio文件
  IVDocument doc = app.documents().open(filePath);
  // 创建一个Dom4j类型的Document对象
  Document xmlDoc = DocumentHelper.createDocument();
  Element root = xmlDoc.addElement("page");
  try {
  // 只读取Visio文档中第一个页面的信息
   IVPage page = doc.pages().itemFromID(0);
  // 读取Page对象的长和宽,并转化为像素单位(乘以96)
   root.addAttribute("width", "" + ((int) (page.pageSheet().cells("PageWidth").resultIU() * 96)));
   root.addAttribute("height", "" + ((int) (page.pageSheet().cells("PageHeight").resultIU() * 96)));
   IVShapes shapes = page.shapes();
   System.out.println("shapes="+shapes.count());
  // 遍历该Page对象中所有的Shape对象
   for (int shapeCount = 1; shapeCount <= shapes.count(); shapeCount++) {
    IVShape shape = shapes.itemU(shapeCount);
   
    String shapeId = shape.text();
    System.out.println("shapeName="+shape.name());
    System.out.println("PinX="+shape.cells("PinX").resultIU()*25.4);
   // System.out.println("自定义属性="+shape.cellsU("Prop.equ").result(new String()));
    
   // 找出被我们事先标识了的Shape对象进行进一步处理
    if (shapeId.length() > 0) {
    // 在page元素下面新建一个shape元素
     Element shapeElement = root.addElement("shape");
    // 为shape元素添加id,height,width,x,y等属性
     shapeElement.addAttribute("id", shapeId);
     shapeElement.addAttribute("height", "" + ((int) (shape.cells("Height").resultIU() * 96)));
     shapeElement.addAttribute("width", "" + ((int) (shape.cells("Width").resultIU() * 96)));
     shapeElement.addAttribute("x", "" + ((int) (shape.cells("PinX").resultIU() * 96)));
     shapeElement.addAttribute("y", "" + ((int) (shape.cells("PinY").resultIU() * 96)));
     shape.text("");
     shape.export(outputDir + "\\" + shapeId + ".gif");
    }
   }
   doc.saved(true);
  }finally {
   doc.close();// 关闭打开的文件
   app.quit();// 退出Visio应用程序
  }
  try {
  // lets write to a file
   XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
   writer.write(xmlDoc);
   writer.close();
  }catch (IOException e) {
  
  }
 }
}

评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值