通过JAVA读取Visio
最近做的項目需要將VISIO的數據保存到數據庫中,參考網絡上的方法以及自己的嘗試終于得到了想要獲取的數據,
參考資料可參照:http://itindex.net/detail/45235-java-visio-com4j (讀取VISIO)
? ? ? ? ? ? ? ? ? ? ? ??http://wenku.baidu.com/view/fdcd512bbd64783e09122b74.html (VISIO圖形介紹)
在使用JAVA讀取VISIO時需先做好事前準備:
1、準備COM4J包,這個在百度搜索就能搜索到,文件太大了16M沒法上傳
2、將VISIO轉換成JAVA庫
? 2.1?解壓縮com4j包,把args4j-2.0.1.jar,tlbimp.jar,com4j.jar放入JDK的bin目錄下。
? 2.2 CMD進入JDK的bin目錄,執行:java -jar tlbimp.jar -o visio -p test ?"D:\Program Files (x86)\Microsoft Office\Office14\VISLIB.DLL"
? ?生成的java庫會在bin目錄下生成一個VISIO文件夾,里面就是VISIO的JAVA庫,在項目中使用
3、就是:http://itindex.net/detail/45235-java-visio-com4j 這里的代碼啦,記住要把VISIO庫以及com4j.jar導入到項目中
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;import test.ClassFactory;
import test.IVApplication;
import test.IVDocument;
import test.IVPage;
import test.IVShape;
import test.IVShapes;
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) { } }
}
總結
以上是生活随笔為你收集整理的通过JAVA读取Visio的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 减肥晚上不能吃水果
- 下一篇: word文档及其历史版本的备份