如何在C#中读取Visio文档内容

4

我的DLL库代码如下:

using System;
using IVisio=Microsoft.Office.Interop.Visio;

namespace Emix
{
public class Visio
{
    protected String path;

    public Visio(String path)
    {
        this.path = path;
    }

    public void open()
    {
        try
        {
            IVisio.Document doc = new IVisio.Application().Documents.Add(this.path);
            Console.WriteLine("Number of pages: " + doc.Pages.Count);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
}

然而,这段代码会打开 Visio 编辑器并显示文档中的页面数量。

是否有可能在不打开 Visio 的情况下读取该文件内容

5个回答

2
这是一个关于如何在 Visio 中读取形状属性的程序......
    namespace VisioEventsExample
    {
    using System;
    using Microsoft.Office.Interop.Visio;

    class Program
    {
        public static void Main(string[] args)
        {
            // Open up one of Visio's sample drawings.
            Application app = new Application();
            Document doc = app.Documents.Open(
                @"C:\Program Files\Microsoft Office\Office14\visio content\1033\ASTMGT_U.VST");

            // Get the first page in the sample drawing.
            Page page = doc.Pages[1];

            // Start with the collection of shapes on the page and 
            // print the properties we find,
            printProperties(page.Shapes);
        }

        /* This function will travel recursively through a collection of 
         * shapes and print the custom properties in each shape. 
         * 
         * The reason I don't simply look at the shapes in Page.Shapes is 
         * that when you use the Group command the shapes you group become 
         * child shapes of the group shape and are no longer one of the 
         * items in Page.Shapes.
         * 
         * This function will not recursive into shapes which have a Master. 
         * This means that shapes which were created by dropping from stencils 
         * will have their properties printed but properties of child shapes 
         * inside them will be ignored. I do this because such properties are 
         * not typically shown to the user and are often used to implement 
         * features of the shapes such as data graphics.
         * 
         * An alternative halting condition for the recursion which may be 
         * sensible sense for many drawing types would be to stop when you 
         * find a shape with custom properties.
         */
        public static void printProperties(Shapes shapes)
        {
            // Look at each shape in the collection.
            foreach (Shape shape in shapes)
            {               
                // Use this index to look at each row in the properties 
                // section.
                short iRow = (short) VisRowIndices.visRowFirst;

                // While there are stil rows to look at.
                while (shape.get_CellsSRCExists(
                    (short) VisSectionIndices.visSectionProp, 
                    iRow, 
                    (short) VisCellIndices.visCustPropsValue,
                    (short) 0) != 0)
                {
                    // Get the label and value of the current property.
                    string label = shape.get_CellsSRC(
                            (short) VisSectionIndices.visSectionProp, 
                            iRow,
                            (short) VisCellIndices.visCustPropsLabel
                        ).get_ResultStr(VisUnitCodes.visNoCast);

                    string value = shape.get_CellsSRC(
                            (short) VisSectionIndices.visSectionProp, 
                            iRow,
                            (short) VisCellIndices.visCustPropsValue
                        ).get_ResultStr(VisUnitCodes.visNoCast);

                    // Print the results.
                    Console.WriteLine(string.Format(
                        "Shape={0} Label={1} Value={2}",
                        shape.Name, label, value));

                    // Move to the next row in the properties section.
                    iRow++;
                }

                // Now look at child shapes in the collection.
                if (shape.Master == null && shape.Shapes.Count > 0)
                    printProperties(shape.Shapes);
            }
        }
    }
}

上面的例子假设你已经在你的电脑上安装了Visio Primary Interop Assembly,并且在你的项目中引用了Microsoft.Office.Interop.Visio。

希望这能对你有所帮助....


2
MULTIPLE DOTS!!! - Jeff Mercado

0
如果您将应用程序设置为不可见,您将看不到新的 Visio 实例正在打开(仍然会有一个 Visio 实例在运行,只是不可见)。
Application application = new Application();  
application.Visible = false; 
Document doc = application.Documents.Add(this.path);
Console.WriteLine("Number of pages: " + doc.Pages.Count);

如果运行此应用程序的任何用户计算机上没有Visio怎么办? - Mike Doe
我认为用户需要安装Visio。否则,您需要找到一种读取vdx文件的方法。 - gcores

0

正如其他答案所指出的那样,您可以使用Application.Visible属性来隐藏正在运行的Visio实例。

另一个选项是直接读取Visio的VDX文件格式,以检索页面数量、自定义属性等。这避免了必须启动Visio才能从文件中读取信息的情况。但是,此解决方案要求您了解VDX文件的XML模式。


你有检查问题的日期吗? - Mike Doe

0

0

正如其他答案所指出的那样,您可以使用Application.Visible属性来隐藏正在运行的Visio实例。

另一个选项是直接读取Visio的VDX文件格式,以检索页面数量、自定义属性等。这避免了必须启动Visio才能从文件中读取信息的情况。但是,此解决方案要求您成为VDX文件的XML模式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接