网上找到资料提供了遍历.mpp文件中任务的功能:
http://blog.csdn.net/gxf36/article/details/5253792
=========================================================
但未提供遍历任务边关系(依赖关系)的功能。尝试并经过测试形成如下代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.MSProject; namespace JDPrj_Demo_DataTransfm { class MppIO { /// <summary> /// 获取.mpp文件中所有依赖关系 /// </summary> /// <param name="filepath"></param> /// <returns></returns> public List<Edge> getEdges(string filepath) { List<Edge> list = new List<Edge>(); Object missing = Type.Missing; ApplicationClass prj = new ApplicationClass(); prj.FileOpen(filepath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, PjPoolOpen.pjPoolReadWrite, missing, missing, missing, missing); Edge edge = null; int graph = new Random().Next(100000); //遍历所有工程 foreach (Project proj in prj.Projects) { //遍历所有任务 foreach (Microsoft.Office.Interop.MSProject.Task task in proj.Tasks) { //遍历任务的依赖关系(边关系) foreach (TaskDependency dep in task.TaskDependencies) { //.mpp任务采用双向链表的方式存储边关系 //只取边的起始任务为当前任务的边,去重 if (dep.From.Index == task.Index) { edge = new Edge(graph, dep.From.Index, dep.To.Index, 0); list.Add(edge); //根据需要只需去双向链表的一条边,去重 break; } } } } return list; } } /// <summary> /// 边,任务与任务间,节点与节点间的边 /// </summary> public class Edge { public int graph_id = 0; //图编号 public int start_node = 0; //开始节点 public int end_node = 0; //结束节点 public int weight = 0; //边权重 public Edge() { } public Edge(int graph_id, int start_node, int end_node, int weight) { this.graph_id = graph_id; this.start_node = start_node; this.end_node = end_node; this.weight = weight; } } }