MS Office的SmartArt功能可供用户一键生成各种逻辑图表、图形,常见的流程图、组织结构图都能用它搞定。Spire.Presentation提供了相应的接口ISmartArt来处理SmartArt图形,本文将介绍如何使用Spire.Presentation生成组织结构图。
C#
//实例化Presentation对象
Presentation ppt = new Presentation();
//设置幻灯片大小
ppt.SlideSize.Type = SlideSizeType.Screen16x9;
//添加组织结构图,并指定位置、大小
ISmartArt smartArt = ppt.Slides[0].Shapes.AppendSmartArt(50, 50, 450, 250, SmartArtLayoutType.OrganizationChart);
//设置SmartArt的样式和颜色
smartArt.Style = SmartArtStyleType.IntenceEffect;
smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors3to4;
//移除默认的形状(Node即代表SmartArt中的形状)
foreach (ISmartArtNode node in smartArt.Nodes)
{
smartArt.Nodes.RemoveNode(node);
}
//添加形状并在其下面添加嵌套子形状
ISmartArtNode node1 = smartArt.Nodes.AddNode();
ISmartArtNode node1_1 = node1.ChildNodes.AddNode();
ISmartArtNode node1_1_1 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_2 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_3 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_4 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_5 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_6 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_1 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_2 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_3 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_3_1 = node1_1_3.ChildNodes.AddNode();
ISmartArtNode node1_1_3_2 = node1_1_3.ChildNodes.AddNode();
ISmartArtNode node1_1_6_1 = node1_1_6.ChildNodes.AddNode();
ISmartArtNode node1_1_6_2 = node1_1_6.ChildNodes.AddNode();
ISmartArtNode node1_1_6_3 = node1_1_6.ChildNodes.AddNode();
//在每一个形状上添加文字
node1.TextFrame.Text = "董事会\n" + "Board of Directors";
node1_1.TextFrame.Text = "总经理\n" + "General Manager";
node1_1_1.TextFrame.Text = "供应部\n" + "Supply Dept.";
node1_1_2.TextFrame.Text = "营销部\n" + "Sales Dept.";
node1_1_3.TextFrame.Text = "生产部\n" + "Productive Dept.";
node1_1_4.TextFrame.Text = "财务部\n" + "Finance Dept.";
node1_1_5.TextFrame.Text = "人力资源部\n" + "HR Dept.";
node1_1_6.TextFrame.Text = "质检中心\n" + "Quality Center";
node1_1_1_1.TextFrame.Text = "采购部\n" + "Purchase Dept.";
node1_1_1_2.TextFrame.Text = "仓库管理\n" + "Warehouse Manager";
node1_1_1_3.TextFrame.Text = "物流部\n" + "Logistics Dept.";
node1_1_3_1.TextFrame.Text = "生产车间\n" + "Production Dept.";
node1_1_3_2.TextFrame.Text = "维修部\n" + "Maintenance Dept.";
node1_1_6_1.TextFrame.Text = "生产质量管理\n" + "Production Quality Mgt.";
node1_1_6_2.TextFrame.Text = "生产安全管理\n" + "Production Safety Mgt.";
node1_1_6_3.TextFrame.Text = "环境管理\n" + "Environmental Mgt.";
//保存文档
ppt.SaveToFile("output.pptx", FileFormat.Pptx2013);
VB.NET
'实例化Presentation对象
Dim ppt As New Presentation()
'设置幻灯片大小
ppt.SlideSize.Type = SlideSizeType.Screen16x9
'添加组织结构图,并指定位置、大小
Dim smartArt As ISmartArt = ppt.Slides(0).Shapes.AppendSmartArt(50, 50, 450, 250, SmartArtLayoutType.OrganizationChart)
'设置SmartArt的样式和颜色
smartArt.Style = SmartArtStyleType.IntenceEffect
smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors3to4
'移除默认的形状(Node即代表SmartArt中的形状)
For Each node As ISmartArtNode In smartArt.Nodes
smartArt.Nodes.RemoveNode(node)
Next
'添加形状并在其下面添加嵌套子形状
Dim node1 As ISmartArtNode = smartArt.Nodes.AddNode()
Dim node1_1 As ISmartArtNode = node1.ChildNodes.AddNode()
Dim node1_1_1 As ISmartArtNode = node1_1.ChildNodes.AddNode()
Dim node1_1_2 As ISmartArtNode = node1_1.ChildNodes.AddNode()
Dim node1_1_3 As ISmartArtNode = node1_1.ChildNodes.AddNode()
Dim node1_1_4 As ISmartArtNode = node1_1.ChildNodes.AddNode()
Dim node1_1_5 As ISmartArtNode = node1_1.ChildNodes.AddNode()
Dim node1_1_6 As ISmartArtNode = node1_1.ChildNodes.AddNode()
Dim node1_1_1_1 As ISmartArtNode = node1_1_1.ChildNodes.AddNode()
Dim node1_1_1_2 As ISmartArtNode = node1_1_1.ChildNodes.AddNode()
Dim node1_1_1_3 As ISmartArtNode = node1_1_1.ChildNodes.AddNode()
Dim node1_1_3_1 As ISmartArtNode = node1_1_3.ChildNodes.AddNode()
Dim node1_1_3_2 As ISmartArtNode = node1_1_3.ChildNodes.AddNode()
Dim node1_1_6_1 As ISmartArtNode = node1_1_6.ChildNodes.AddNode()
Dim node1_1_6_2 As ISmartArtNode = node1_1_6.ChildNodes.AddNode()
Dim node1_1_6_3 As ISmartArtNode = node1_1_6.ChildNodes.AddNode()
'在每一个形状上添加文字
node1.TextFrame.Text = "董事会" & vbLf + "Board of Directors"
node1_1.TextFrame.Text = "总经理" & vbLf + "General Manager"
node1_1_1.TextFrame.Text = "供应部" & vbLf + "Supply Dept."
node1_1_2.TextFrame.Text = "营销部" & vbLf + "Sales Dept."
node1_1_3.TextFrame.Text = "生产部" & vbLf + "Productive Dept."
node1_1_4.TextFrame.Text = "财务部" & vbLf + "Finance Dept."
node1_1_5.TextFrame.Text = "人力资源部" & vbLf + "HR Dept."
node1_1_6.TextFrame.Text = "质检中心" & vbLf + "Quality Center"
node1_1_1_1.TextFrame.Text = "采购部" & vbLf + "Purchase Dept."
node1_1_1_2.TextFrame.Text = "仓库管理" & vbLf + "Warehouse Manager"
node1_1_1_3.TextFrame.Text = "物流部" & vbLf + "Logistics Dept."
node1_1_3_1.TextFrame.Text = "生产车间" & vbLf + "Production Dept."
node1_1_3_2.TextFrame.Text = "维修部" & vbLf + "Maintenance Dept."
node1_1_6_1.TextFrame.Text = "生产质量管理" & vbLf + "Production Quality Mgt."
node1_1_6_2.TextFrame.Text = "生产安全管理" & vbLf + "Production Safety Mgt."
node1_1_6_3.TextFrame.Text = "环境管理" & vbLf + "Environmental Mgt."
'保存文档
ppt.SaveToFile("output.pptx", FileFormat.Pptx2013)