第一步:安装Visio
安装相应版本的Visio(以64位为例),在安装过程中经常遇到下面的问题
解决方法:
按“win+R”快捷键,打开“运行”,输入“regedit”,打开注册表,找到【HKEY_CLASSES_ROOT】àInstalleràProducts
备份注册表,然后将上面Office 16 Click-to-run Extensiblity Component删除,然后再次安装即可成功。
第二步:添加引用
在项目中添加COM组件引用
我安装的是Visio 2013版本,因此添加15.0版本的类库引用
第三步:准备Viso文件
在Viso中画了如下简单图形,
Visio.Application app = null; Visio.Document doc = null; try { app = new Visio.Application(); app.Visible = false; doc = app.Documents.Open(fname); var shapes = ((Visio.PageClass)((Visio.DocumentClass)doc).Pages[1]).Shapes; var dt = new DataTable(); string[] cols = new string[] { "ID", "名称", "起始坐标", "结束坐标", "角度" }; foreach (var item in cols) { dt.Columns.Add(item); } foreach (Visio.Shape shape in shapes) { var dr = dt.NewRow(); dr[0] = shape.ID; dr[1] = shape.Text; dr[2] = $"({shape.Cells["BeginX"].Formula},{shape.Cells["BeginY"].Formula})"; dr[3] = $"({shape.Cells["EndX"].Formula},{shape.Cells["EndY"].Formula})"; try { var x1 = double.Parse(shape.Cells["BeginX"].Formula.Replace("mm", "").Trim()); var x2 = double.Parse(shape.Cells["EndX"].Formula.Replace("mm", "").Trim()); var y1 = double.Parse(shape.Cells["BeginY"].Formula.Replace("mm", "").Trim()); var y2 = double.Parse(shape.Cells["EndY"].Formula.Replace("mm", "").Trim()); dr[4] = Math.Atan2(y2 - y1, x2 - x1); } catch (Exception e) { } //对于圆形没有起始结束坐标,有圆心坐标 if(string.IsNullOrEmpty(shape.Cells["BeginX"].Formula)) { dr[2] = $"({shape.Cells["PinX"].Formula},{shape.Cells["PinY"].Formula})"; dr[3] = string.Empty; } dt.Rows.Add(dr); } return dt; } catch (Exception ex) { throw; } finally { doc.Close(); app.Quit(); }读取的数据如下:
以上就是一个简单读取Visio中模型数据的Demo,对于读取Viso字段值如果不知道字段名字可以打开Visio文件,进入开发者模式,然后右击选中的Shape展示ShapeSheet,可以看到相关的全部字段及对应的值。
