在做HR系统的时候,往往需要导出Visio格式的组织结构图。
以前很少接触Office的二次开发,这个东西足足搞了我一个礼拜,汗~~~~~
我的环境为Visio2003+VS2005
主要是利用OrgCWiz,这个Visio提供的接口,具体的参数请参考MS的网站
http://office.microsoft.com/zh-cn/visio/HA010774642052.aspx
个人认为http://visio.mvps.org/VBA.htm这个网站比较专业,很多关于Visio的资料可以参考一下
导出的代码很简单,主要为
Microsoft.Office.Interop.Visio.ApplicationClass _app
=
new
ApplicationClass();

Microsoft.Office.Interop.Visio.Addon ac
=
_app.Addons.get_ItemU(
"
OrgCWiz
"
);

ac.Run(
"
/S-INIT
"
);

string
commandargs
=
"
...............................
"
;

ac.Run(
"
/S-ARGSTR
"
+
commandargs);

ac.Run(
"
/S-RUN
"
);

_app.ActiveDocument.SaveAsEx(destfile,
6
);

_app.Quit();

GC.Collect();
也可以参考http://www.cnblogs.com/invinboy/archive/2006/05/19/404725.html这篇文章
以下代码为设置文本框的背景色
Cell cell
=
shape.get_CellsSRC((
short
)VisSectionIndices.visSectionObject,
(
short
)VisRowIndices.visRowFill, (
short
)VisCellIndices.visFillForegnd);
cell.FormulaU
=
"
RGB(176,197,227)
"
;
以下代码为设置Title(字体大小及颜色)
Shape shapeTitle
=
page.Drop(doc.Masters.get_ItemU(
"
Title
"
),
6
,
7.8
);
shapeTitle.Text
=
""
;
Cell cellTitle
=
shapeTitle.get_CellsSRC((
short
)VisSectionIndices.visSectionCharacter,
0
, (
short
)VisCellIndices.visCharacterColor);
cellTitle.FormulaU
=
"
RGB(153,0,51)
"
;
shapeTitle.Characters.Text
=
"
组织结构图
"
;
shapeTitle.Characters.set_CharProps(
(
short
)VisCellIndices.visCharacterStyle,
(
short
)VisCellVals.visBold);
shapeTitle.Characters.set_CharProps((
short
)VisCellIndices.visCharacterSize,
24
);
以下代码为导出员工照片
Shape visShapeImage
=
shape.Import(filepath);
Cell vsoCell
=
visShapeImage.get_Cells(
"
PinX
"
);
vsoCell.Formula
=
shape.NameID
+
"
!Width*0.3
"
;
vsoCell
=
visShapeImage.get_Cells(
"
PinY
"
);
vsoCell.Formula
=
shape.NameID
+
"
!Height*0.5
"
;
vsoCell
=
visShapeImage.get_Cells(
"
Width
"
);
vsoCell.Formula
=
shape.NameID
+
"
!Width*0.5
"
;
vsoCell
=
visShapeImage.get_Cells(
"
Height
"
);
vsoCell.Formula
=
shape.NameID
+
"
!Height*0.5
"
;

//
设置user-defined cell属性
if
(shape.get_CellExists(
"
User.ShowPicture
"
,
0
)
!=
0
)

...
{
Cell showPicture = shape.get_CellsSRC((short)VisSectionIndices.visSectionUser,
(short)VisRowIndices.visRowUser + 2,
(short)VisCellIndices.visUserValue);
showPicture.Formula = "1";
}

if
(shape.get_CellExists(
"
User.HasPicture
"
,
0
)
!=
0
)

...
{
Cell hasPicture = shape.get_CellsSRC((short)VisSectionIndices.visSectionUser,
(short)VisRowIndices.visRowUser + 10,
(short)VisCellIndices.visUserValue);
hasPicture.Formula = "1";
}

if
(shape.get_CellExists(
"
User.PictureID
"
,
0
)
!=
0
)

...
{
Cell pictureID = shape.get_CellsSRC((short)VisSectionIndices.visSectionUser,
(short)VisRowIndices.visRowUser + 15,
(short)VisCellIndices.visUserValue);
pictureID.Formula = """ + visShapeImage.NameID + """;
}
欢迎转载,请注明出处~~