脱离Office约束,C#结合Mpxj组件完美解析MSProject(.mpp)文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using net.sf.mpxj;
using net.sf.mpxj.MpxjUtilities;
using net.sf.mpxj.reader;
using System.Data;
namespace ConstructionProcessManage_Mpxj
{
    public class MpxjHelper
    {
        /// <summary>
        /// 导入MSProject文件
        /// </summary>
        /// <param name="filename">文件地址</param>
        /// <param name="projectId"></param>
        /// <param name="usercode"></param>
        /// <param name="dt1">任务列表</param>
        /// <param name="dt2">前置任务列表</param>
        /// <returns></returns>
        public void ImportTasks(string filename, out DataTable dt1, out DataTable dt2)
        {
            dt1 = GetTableSchema1();
            dt2 = GetTableSchema2();
            ProjectReader reader = ProjectReaderUtility.getProjectReader(filename);
            ProjectFile file = reader.read(filename);
            foreach (net.sf.mpxj.Task task in file.Tasks.ToIEnumerable())
            {
                if (task.ID.toString()=="0") //自动创建的节点,一般为文件名,不需要
                {
                    continue;
                }
                DataRow dr = dt1.NewRow();
                DateTime dtstartdate = task.Start.ToDateTime(); //开始时间
                DateTime dtenddate = task.Finish.ToDateTime();//结束时间
                TimeSpan tspan = dtenddate.Subtract(dtstartdate);
                dr["TaskId"] = Guid.Parse(task.GUID.toString());
                dr["TaskName"] = task.Name;
                dr["Guid"] = null;
                dr["Unit"] = "";
                dr["TaskDeys"] = tspan.Days + 1;
                dr["UserNumber"] = 0;
                dr["EquipmentShow"] = "";
                dr["MaterialShow"] = "";
                dr["Technology"] = "";
                dr["TaskType"] = 0;
                if (task.ParentTask != null && Convert.ToInt32(task.OutlineLevel.toString())>1) //是否根节点,mpp文件必须只有一个根节点,否则会报错
                {
                    dr["ParentId"] = Guid.Parse(task.ParentTask.GUID.toString());
                }
                dr["PstartDate"] = DateTime.Parse(string.Format("{0:d}", dtstartdate));
                dr["PendDate"] = DateTime.Parse(string.Format("{0:d}", dtenddate));
                dr["VersionCode"] = projectId;
                dr["EditDate"] = DateTime.Now;
                dr["Note"] = "";
                dr["TasksTatus"] = 0;
                dr["RStartDate"] = DBNull.Value; //new Nullable<DateTime>();
                dr["REndDate"] = DBNull.Value;//new Nullable<DateTime>();
                dr["Pstatus"] = 0;
                dr["TaskPro"] = 0;
                dr["TaskLevel"] = Convert.ToInt32(task.OutlineLevel.toString())-1;
                dr["TaskSource"] = 1;
                dr["SortOrder"] = Convert.ToInt32(task.UniqueID.toString());//0; //task.OutlineLevel;
                dr["MileageID"] = 0;//---------
                dr["RelevanceModel"] = false;
                if (task.HasChildTasks())//------------------------------------
                {
                    dr["LeafNode"] = true;
                }
                else
                {
                    dr["LeafNode"] = false;
                }
                dr["DelayDays"] = 0;

                dt1.Rows.Add(dr);


                if (task.Predecessors != null && task.Predecessors.isEmpty() == false)
                {
                    foreach (Relation relation in task.Predecessors.ToIEnumerable())
                    {
                        DataRow dr2 = dt2.NewRow();
                        dr2["RelyId"] = Guid.NewGuid();
                        dr2["VersionCode"] = projectId;
                        dr2["TaskId"] = Guid.Parse(relation.SourceTask.GUID.toString());
                        dr2["RelyTask"] = Guid.Parse(task.GUID.toString());
                        dr2["RelyType"] = 0; //int.Parse(task.Type.ToString());
                        dr2["CreateDate"] = DateTime.Now;
                        dt2.Rows.Add(dr2);
                    }
                }
            }
        }
       
        #region 自定义任务表(列必须与数据库表列一一对应,最终采用SqlBulkCopy批量插入)
        public DataTable GetTableSchema1()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] {   
            new DataColumn("TaskId",typeof(Guid)),  
            new DataColumn("TaskName",typeof(string)),  
            new DataColumn("Guid",typeof(string)),
            new DataColumn("ProjectId",typeof(Int32)),

            new DataColumn("Unit",typeof(string)),  
            new DataColumn("TaskDeys",typeof(Int32)),  
            new DataColumn("UserNumber",typeof(Int32)),
            new DataColumn("EquipmentShow",typeof(string)),

            new DataColumn("MaterialShow",typeof(string)),  
            new DataColumn("Technology",typeof(string)),  
            new DataColumn("TaskType",typeof(Int32)),
            new DataColumn("ParentId",typeof(Guid)),

            new DataColumn("PstartDate",typeof(DateTime)),  
            new DataColumn("PendDate",typeof(DateTime)),  
            new DataColumn("VersionCode",typeof(string)),
            new DataColumn("EditUser",typeof(string)),

            new DataColumn("EditDate",typeof(DateTime)),  
            new DataColumn("Note",typeof(string)),  
            new DataColumn("TasksTatus",typeof(Int32)),
            new DataColumn("RStartDate",typeof(DateTime)),

            new DataColumn("REndDate",typeof(DateTime)),  
            new DataColumn("PStatus",typeof(Int32)),  //
            new DataColumn("TaskPro",typeof(decimal)),
            new DataColumn("TaskLevel",typeof(Int32)),

            new DataColumn("TaskSource",typeof(Int32)),  
            new DataColumn("SortOrder",typeof(Int32)),  
            new DataColumn("MileageID",typeof(Int32)),//
            new DataColumn("RelevanceModel",typeof(bool)),

            new DataColumn("LeafNode",typeof(bool)),  
            new DataColumn("DelayDays",typeof(Int32))
            
            });
            return dt;
        }
        #endregion
        #region 自定义前置任务表(列必须与数据库表列一一对应,最终采用SqlBulkCopy批量插入)
        public DataTable GetTableSchema2()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] {   
            new DataColumn("RelyId",typeof(Guid)),  
            new DataColumn("VersionCode",typeof(string)),  
            new DataColumn("TaskId",typeof(Guid)),
            new DataColumn("RelyTask",typeof(Guid)),

            new DataColumn("RelyType",typeof(Int32)),  
            new DataColumn("CreateDate",typeof(DateTime)),  
            new DataColumn("CreateUser",typeof(Int32))
            
            });
            return dt;
        }
        #endregion
    }
}

Mpxj组件已打包,请在本人百度网盘下载

链接:https://pan.baidu.com/s/1qV2ZgzwsPfY4kH-xPkvRzw
提取码:g9ri

原创文章,转载请注明

转载于:https://www.cnblogs.com/fengyeqingxiang/p/10948190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值