php解析出带层级关系的mpp文件
本来要使用DHX gantt插件自带的API做导入,可是做完后,又发现不稳定,不能访问了
可能是屏蔽掉了
所以又想起可以使用javaBridge,借用java的MPXJ
php解析mpp的 上一篇介绍
1.简单输出所有任务
$file_path = "aa.mpp"; require_once 'http://localhost:8089/JavaBridge/java/Java.inc'; $data = array(); if (empty($file_path)) { return $data; } $mppRead = new Java('net.sf.mpxj.mpp.MPPReader'); $Filecontent = $mppRead->read($file_path); $Alltasks = $Filecontent->getAllTasks(); $Tasks_size = java_values($Alltasks->size()); for ($i = 1; $i < $Tasks_size; $i++) { $separator = '|'; $tmp= getResource($Alltasks[$i]); $items = array(); $items['id'] = $Alltasks[$i]->getID().''; $items['level'] = $Alltasks[$i]->getOutlineLevel().''; $items['uniqueID'] = $Alltasks[$i]->getUniqueID().''; $items['name'] = $Alltasks[$i]->getName().''; $items['duration'] = $Alltasks[$i]->getDuration().''; $items['start'] = $Alltasks[$i]->getStart().''; $items['finish'] = $Alltasks[$i]->getFinish().''; $items['percentComplete'] = $Alltasks[$i]->getPercentageComplete().''; $data[] = $items; } var_dump(json_encode($data));
这种解析的方式,忽略了微软Project允许用户创建的分层任务结构,虽然level可以表明他处于第几级
所以需要改进
2.输出带层级关系所有任务
将子任务放到children里
$file_path = "aa.mpp"; require_once 'http://localhost:8089/JavaBridge/java/Java.inc'; $data = array(); if (empty($file_path)) { return $data; } $mppRead = new Java('net.sf.mpxj.mpp.MPPReader'); $Filecontent = $mppRead->read($file_path); $Alltasks = $Filecontent->getChildTasks(); $Tasks_size = java_values($Alltasks->size()); $data = array(); if($Tasks_size){ $children = getChildren($Alltasks[0]); if($children){ $data = $children; } } var_dump(json_encode($data));die; function getChildren($task){ $child = $task->getChildTasks(); $Tasks_size = java_values($child->size()); $data = array(); $formatter = new Java('java.text.SimpleDateFormat', "yyyy-MM-dd HH:mm:ss"); for ($i = 0; $i < $Tasks_size; $i++) { $separator = '|'; $tmp= getResource($child[$i]); $items = array(); $items['id'] = $child[$i]->getID().''; $items['level'] = $child[$i]->getOutlineLevel().''; $items['uniqueID'] = $child[$i]->getUniqueID().''; $items['name'] = $child[$i]->getName().''; $items['duration'] = $child[$i]->getDuration().''; $start = $child[$i]->getStart(); $items['start'] = $formatter->format($start).''; $finish = $child[$i]->getFinish(); $items['finish'] = $formatter->format($finish).''; $items['percentComplete'] = $child[$i]->getPercentageComplete().''; $children = getChildren($child[$i]); if($children){ $items['children'] = $children; } $data[] = $items; } return $data; }
解析后输出的json结构式带有层级关系的
说明:
$Alltasks = $Filecontent->getChildTasks();获取到的是文件的信息,一般就一个元素,
把$Alltasks[0]传给getChildren()获取的就是任务了
$Alltasks = $Filecontent->getChildTasks(); $Tasks_size = java_values($Alltasks->size()); $formatter = new Java('java.text.SimpleDateFormat', "yyyy-MM-dd HH:mm:ss"); for ($i = 0; $i < $Tasks_size; $i++) { $separator = '|'; $tmp= getResource($Alltasks[$i]); $items = array(); $items['id'] = $Alltasks[$i]->getID().''; $items['level'] = $Alltasks[$i]->getOutlineLevel().''; $items['uniqueID'] = $Alltasks[$i]->getUniqueID().''; $items['name'] = $Alltasks[$i]->getName().''; $items['duration'] = $Alltasks[$i]->getDuration().''; $start = $Alltasks[$i]->getStart(); $items['start'] = $formatter->format($start).''; $finish = $Alltasks[$i]->getFinish(); $items['finish'] = $formatter->format($finish).''; $items['percentComplete'] = $Alltasks[$i]->getPercentageComplete().''; $data[] = $items; } var_dump(json_encode($data));die;
输出的和用project看到的内容是一样的
MPXJ这个插件的功能还有很多,还有待继续研究