一、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);
}
}
以上仅供参考。。。