Asp.net 甘特图数据导出Project(.mpp)文件

一、cs后台代码 

  1.1 主函数

private void ExportMpp(HttpContext context)
        {
            string ret = "";

            try
            {
                string KEY = Operate.getParameter(context, "KEY ") != "" ? Operate.getParameter(context, "KEY ") : "";
                
                string strMPPNAME = "任务_"+ KDJHNO +"_"+ DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + ".mpp";
                string strDirectory = context.Server.MapPath("~/tempfile");
                string strFullName = strDirectory +"/" + strMPPNAME;

                checkAndCreatePath(strDirectory);
                //string DataJson = Operate.getParameter(context, "JSON_DATA") != "" ? Operate.getParameter(context, "JSON_DATA") : "";

                BLL.AA bll = new BLL.AA();
                List<DataModel> lstModels = bll.GetListForGantt(KEY);

                createMppFromDB(strFullName, lstModels);

                ret = Operate.getJSONResult("ok", strMPPNAME);
            }
            catch (Exception ex)
            {
                ret = Operate.getJSONResult("error", "导出失败:"+ex.Message);
            }

            context.Response.Write(ret);
        }
  1.2 导出函数

  

private void createMppFromDB(string prjFileName, List<DataModel> lstModels)
        {
            MSProject.ApplicationClass prj = null;
            int i = 0;
            try
            {
                Object missing = Type.Missing;
                prj = new MSProject.ApplicationClass();
                MSProject.PjFileFormat format = MSProject.PjFileFormat.pjMPP;//format定义   

                prj.Visible = false;
                prj.FileNew(Type.Missing, Type.Missing, Type.Missing, false);
                MSProject.Project myProject = prj.ActiveProject;
                MSProject.Task task = null;

                Dictionary<int, int> dicIndex = new Dictionary<int, int>();
                Dictionary<int, short> dicTaskLevel = new Dictionary<int, short>();
                DataModel model = null;
                if (lstModels != null && lstModels.Count > 0)
                {
                    for (i = 0; i < lstModels.Count; i++)
                    {
                        model = lstModels[i];
                        model.JHNAME = model.JHNAME.Replace('\n', ' ');
                        model.DEPT = model.DEPT.Replace('\n', ' ');

                        task = myProject.Tasks.Add(model.JHNAME, Type.Missing);
                        task.Start = model.PBDATE.Value;
                        task.Finish = model.PEDATE.Value;
                        if (model.ISFINISH == 1 || model.PERATE >= 100)
                        {
                            if (model.BDATE != null && model.EDATE != null)
                            {
                                task.ActualStart = model.BDATE.Value;
                                task.ActualFinish = model.EDATE.Value;
                            }
                        }

                        task.PercentComplete = model.PERATE;

                        short level = 1;
                        if (model.PNO != null)
                        {
                            if (dicTaskLevel.ContainsKey(model.PNO.Value))
                            {
                                short iParentLevel = dicTaskLevel[model.PNO.Value];
                                level = iParentLevel;
                                level++;
                            }
                        }
                        if (!dicTaskLevel.ContainsKey(model.JHNO))
                        {
                            dicTaskLevel.Add(model.JHNO, level);
                        }

                        if (level > 0)
                        {
                            task.OutlineLevel = level;
                        }

                        task.SetField(MSProject.PjField.pjTaskResourceNames, model.DEPT + "(" + model.DUSER + ")");

                        if (!dicIndex.ContainsKey(model.JHNO))
                        {
                            dicIndex.Add(model.JHNO, i);
                        }
                    }

                    for (i = 0; i < lstModels.Count; i++)
                    {
                        model = lstModels[i];

                        if (!string.IsNullOrEmpty(model.QZJH))
                        {
                            string[] arrQZNO = model.QZJH.Split(',');
                            foreach (string preNO in arrQZNO)
                            {
                                int iPreNO = int.Parse(preNO);
                                if (dicIndex.ContainsKey(iPreNO))
                                {
                                    int index = dicIndex[iPreNO];
                                    string JHNAME = lstModels[index].JHNAME;
                                    //myProject.Tasks[i].Parent.Add(JHNAME, Type.Missing);
                                    //myProject.Tasks[i].Predecessors = myProject.Tasks[index].ID.ToString();

                                    myProject.Tasks[i+1].TaskDependencies.Add(myProject.Tasks[index+1], MSProject.PjTaskLinkType.pjFinishToStart, Type.Missing);
                                }
                            }
                        }
                    }
                }

                prj.FileSaveAs(prjFileName, format
                                , Type.Missing, false, Type.Missing, Type.Missing, Type.Missing
                                , Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
                                , Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }
            catch (Exception ex)
            {

                throw new Exception("[任务行(" + (i + 1) + ")]" + ex.Message);
            }
            finally
            {
                if (prj != null)
                {
                    try { 
                        prj.FileClose(MSProject.PjSaveType.pjDoNotSave);
                        prj.Quit(MSProject.PjSaveType.pjDoNotSave);
                    }
                    catch { }
                }
            }
        }

 1.3 下载处理函数

  

private void DownloadMpp(HttpContext context)
        {
            string ret = "";

            try
            {
                string fileName = Operate.getParameter(context, "FILE") != "" ? Operate.getParameter(context, "FILE") : "";
                string filePath = context.Server.MapPath("~/tempfile/" + fileName);

                downloadMSProject(context, fileName, filePath);
            }
            catch (Exception ex)
            {
                ret = Operate.getJSONResult("error", ex.Message);
            }
        }

        private void downloadMSProject(HttpContext context, string fileName, string strFullPath)
        {
            // 取得下载文件
            string strFileName = fileName;

            {
                context.Response.Buffer = true;
                context.Response.Clear();

                context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                context.Response.ContentType = "application/ms-project;charset=gb2312";

                string downFile = System.IO.Path.GetFileName(strFileName);
                string EncodeFileName = HttpUtility.UrlEncode(downFile, System.Text.Encoding.UTF8);
                context.Response.AddHeader("Content-Disposition", "attachment;filename=" + EncodeFileName + ";");
                context.Response.BinaryWrite(System.IO.File.ReadAllBytes(strFullPath));//返回文件数据给客户端下载
                context.Response.Flush();

                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
        }

        private void checkAndCreatePath(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

以上仅供参考。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值