`

Java操作visio文档

阅读更多
最近写了个java操作visio文档的小工具.使用了javacom & jacob,参考了c++操作visio的com技术,并请教了javacom的作者Miika.
public class JVisio {    
   
    private static Log log = LogFactory.getLog(JVisio.class);    
   
    /**   
     * Visio 程序   
     */   
    IVApplication visioApp = null;    
   
    /**   
     *    
     * 默认visio可见   
     *    
     */   
    public JVisio() {    
        this(true);    
    }    
   
    /**   
     *    
     * @param visible   
     */   
    public JVisio(boolean visible) {    
        this.visioApp = new IVApplication();    
        this.visioApp.setVisible(visible);    
    }    
   
    /**   
     * 退出   
     *    
     */   
    public void quit() {    
        this.visioApp.Quit();    
    }    
   
    /**   
     * 打开文档   
     *    
     * @param visioFile   
     *            visio file name.   
     * @return   
     */   
    public IVDocument addDocument(String visioFile) {    
        IVDocument doc = null;    
        try {    
            doc = this.visioApp.getDocuments().Add(visioFile);    
        } catch (Exception ex) {    
            log.error("Error of open the file '" + visioFile + "'!");    
            ex.printStackTrace();    
        }    
        return doc;    
    }    
   
    /**   
     * 文件另存为   
     *    
     * @param document   
     * @param distFile   
     *            这里的路径分隔符一定是要是 \\,例如E:\\workspace\\jvisio\\test\\tt_out.vsd   
     * @return   
     */   
    public short saveAs(IVDocument document, String distFile) {    
        return document.SaveAs(distFile);    
    }    
   
    /**   
     * 获取visio文档里的一个master   
     *    
     * @param document   
     *            文档   
     * @param masterNameUIDOrIndex   
     *            master的索引或者名称   
     * @return   
     */   
    public IVMaster getMaster(IVDocument document, String masterNameUIDOrIndex) {    
        IVMasters masters = document.getPages().getItem(new Integer(1))    
                .getDocument().getMasters();    
        IVMaster master = masters.getItem(masterNameUIDOrIndex);    
        log.info("Get the master :"   
                + (master == null ? null : master.getName()));    
        return master;    
    }    
   
    /**   
     * 获取单元格   
     *  
 

  
     * for example :   
     *  
 

  
     * double pageWidth = getCells(bts,"PageWidth").getResultIU();   
     *    
     * @param master   
     * @param localeSpecificCellName   
     * @return   
     */   
    public IVCell getCells(IVMaster master, String localeSpecificCellName) {    
        return master.getPageSheet().getCells(localeSpecificCellName);    
    }    
   
    /**   
     * 画模具   
     *    
     * @param document   
     *            文档   
     * @param master   
     *            模具   
     * @param xPos   
     *            x坐标   
     * @param yPos   
     *            y坐标   
     * @return   
     */   
    public IVShape drop(IVDocument document, IVMaster master, double xPos,    
            double yPos) {    
        IVPage tpage = document.getPages().getItem(new Integer(1));    
        return tpage.Drop(master.getDispatch(), xPos, yPos);    
    }    
   
    /**   
     * 画折线   
     *    
     * @param document 目标document   
     * @param fromShape 起点的模具   
     * @param fromPointName 起点的名称   
     * @param toShape 目标点的模具   
     * @param toPointName 目标点的名称   
     * @param connectLine 线模具   
     * @param needTab    
     */   
    public void visioDrawOrthLine(IVDocument document, IVShape fromShape,    
            String fromPointName, IVShape toShape, String toPointName,    
            IVShape connectLine, boolean needTab) {    
   
        // 要连线的起点    
        IVCell fromCell = fromShape.getCells(fromPointName);    
   
        // 要连线的终点    
        IVCell toCell = toShape.getCells(toPointName);    
   
        // 线的起终点    
        IVCell beginOfLine = connectLine.getCells("BeginX");    
        IVCell endOfLine = connectLine.getCells("EndX");    
   
        // 连接    
        beginOfLine.GlueTo(fromCell);    
        endOfLine.GlueTo(toCell);    
   
        if (needTab) {    
            IVCell x2 = connectLine.getCells("Geometry1.X2");    
            double k = x2.getResultIU();    
            String v = String.valueOf(k * 2);    
            x2.setFormulaU(v);    
            connectLine.getCells("Geometry1.X3").setFormulaU(v);    
        }    
    }    
   
    /**   
     * 标注文字   
     *    
     * @param document   
     * @param text   
     *            标注的文字   
     * @param rectangle   
     *            矩形   
     * @param vertAlign   
     *            1表示yes,0表示no   
     * @param horzAlign   
     *            1表示yes,0表示no   
     * @param textColor   
     *            "RGB(128,32,64)"   
     */   
    public void visioDrawText(IVDocument document, String text,    
            Rectangle rectangle, int vertAlign, int horzAlign, String textColor) {    
   
        IVPage page = document.getPages().getItem(new Integer(1));    
        IVShape textShape = page.DrawRectangle(rectangle.getX1(), rectangle    
                .getY1(), rectangle.getX2(), rectangle.getY2());    
        // some properties:    
        // 字体大小    
        IVCell cell = textShape.getCells("Char.Size");    
        if (cell != null)    
            cell.setFormulaU("8pt");    
   
        // 垂直居中    
        cell = textShape.getCells("VerticalAlign");    
        if (cell != null)    
            cell.setFormulaU(String.valueOf(vertAlign));    
   
        // Para.HorzAlign    
        cell = textShape.getCells("Para.HorzAlign");    
        if (cell != null)    
            cell.setFormulaU(String.valueOf(horzAlign));    
   
        // text color    
        cell = textShape.getCells("Char.Color");    
        if (cell != null)    
            cell.setFormulaU(textColor);    
   
        textShape.setText(text);    
   
    }   

使用示例:

public class Demo {    
   
    private static Log log = LogFactory.getLog(Demo.class);    
   
    public static void main(String[] args) {    
   
        JVisio visio = new JVisio();    
   
        String basedir = "E:\\workspace\\jvisio\\test\\";   
  
        try {   
            // 打开模具   
            IVDocument model = visio.addDocument(basedir + "model.vss");   
            // 打开模板   
            IVDocument template = visio.addDocument(basedir + "template.vst");   
  
            // 获取bts模型   
            IVMaster bts = visio.getMaster(model, "BTS");   
            IVMaster gfq = visio.getMaster(model, "功分器");   
            log.info(bts.getName());   
            log.info(gfq.getName());   
            // 标注文字   
            /*   
             * Rectangle rectangle = new Rectangle(0, 5, 2, 7);   
             * visio.visioDrawText(template, "哈哈", rectangle, 0, 0,   
             * "RGB(128,32,64)");   
             */   
  
            // 连线   
            IVShape btsShape = visio.drop(template, bts, 1, 5);   
            IVShape gfqShape = visio.drop(template, gfq, 2, 7);   
  
            IVMaster line = visio.getMaster(model, "1/2馈线");   
            IVShape lineShape = visio.drop(template, line, 1, 1);   
  
            visio.visioDrawOrthLine(template, btsShape, "Connections.X1",   
                    gfqShape, "Connections.X1", lineShape, false);   
  
            // 另存为   
            visio.saveAs(template, basedir + "out.vsd");    
                
        } catch (Exception ex) {// 捕捉Runtime Exception,并关闭visio.    
            visio.quit();    
        }    
   
    }    
   
}   


分享到:
评论
4 楼 ietttt 2013-01-17  
楼主方便给个QQ吗?遇到紧急问题想请教楼主。
3 楼 lliiqiang 2010-11-17  
相应的jar包呢
2 楼 xiongx 2009-04-29  
在你这儿代码能直接copy,不错
1 楼 xiongx 2009-04-29  
这篇文章应该是全文转载的吧,怎么不留个链接啊

相关推荐

Global site tag (gtag.js) - Google Analytics