php没有找到相应的包
Java的mpxj可以实现
所以借助JavaBridge.jar JavaBridge介绍
1.安装jdk,设置环境变量(我的版本jdk1.8.0_131)
2.下载mpjx
在http://www.mpxj.org/官网MPXJ in Java,进入下载页
(我的版本mpxj-7.0.2)
解压,将mpxj.jar和lib下面的commons-collections4-4.1.jar、poi-3.17.jar、rtfparserkit-1.10.0.jar放到Java安装目录下的jre/lib/ext下面
测试
aa.mpp
在www/mpp新建mpxjtest.java
import net.sf.mpxj.mpp.*; import net.sf.mpxj.MPXJException; import net.sf.mpxj.Resource; import net.sf.mpxj.ResourceAssignment; import net.sf.mpxj.Task; import net.sf.mpxj.ProjectFile; import java.util.List; import java.io.*; public class mpxjtest { public static void main(String[] args) throws MPXJException { File file = new File("aa.mpp"); MPPReader mppRead = new MPPReader(); ProjectFile pf = mppRead.read(file); System.out.println(pf); List tasks = pf.getAllTasks(); mpxjtest mt = new mpxjtest(); System.out.println("总任务数: " + tasks.size()); System.out.println("ID|任务名|资源|开始时间|结束时间|完成百分比"); for (int i = 1; i < tasks.size(); i++) { System.out.println(((Task) tasks.get(i)).getUniqueID() + "|" + ((Task) tasks.get(i)).getName() + "|" + mt.getResource((Task) tasks.get(i)) + "|" + ((Task) tasks.get(i)).getStart() + "|" + ((Task) tasks.get(i)).getFinish() + "|" + ((Task) tasks.get(i)).getPercentageComplete()); } } public String getResource(Task task) { StringBuffer buf = new StringBuffer(); List assignments = task.getResourceAssignments(); for (int i = 0; i < assignments.size(); i++) { ResourceAssignment assignment = (ResourceAssignment) assignments .get(i); Resource resource = assignment.getResource(); if (resource != null) { buf.append(resource.getName()).append(" "); } } return buf.toString(); } }
3.php调用java
JavaBridge.jar--用于搭建WEB服务器接口的核心JAVA应用程序包
Java.inc--供PHP语言调用的方法包,主要是Java类
首先运行JavaBridge.jar启动WEB服务器接口,在JavaBridge.jar所在目录运行命令
java -jar JavaBridge.jar SERVLET_LOCAL:8089
注:
不加SERVLET_LOCAL:8089,默认的就是8080
修改php.ini里的allow_url_include为ON
测试
新建aa.php
<?php require_once("http://localhost:8080/JavaBridge/java/Java.inc"); $System = java("java.lang.System"); echo $System->getProperties();
成功
注:
要调用自己开发的类,类必须是public类型的。要调用的方法也必须是public类型的
4.php解析mpp
<?php $fileName = "aa.mpp"; $data = parseMSPJ($fileName); echo "success"; $fp = fopen('./a.txt', 'a+b'); fwrite($fp, var_export($data, true)); fclose($fp); function parseMSPJ($fileName) { $file_path = $fileName; 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()); // ID|任务名|开始时间|结束时间|完成百分比|资源|工期 for ($i = 1; $i < $Tasks_size; $i++) { $separator = '|'; $tmp= getResource($Alltasks[$i]); $string = $Alltasks[$i]->getUniqueID() . $separator . $Alltasks[$i]->getName() . $separator .$tmp.$separator.$Alltasks[$i]->getStart() . $separator . $Alltasks[$i]->getFinish() . $separator . $Alltasks[$i]->getPercentageComplete(); $data[] = explode('|', $string); } return $data; } function getResource($task) { $resource = $task->getResourceAssignments(); $size = java_values($resource->size()); $buf = ''; if($size>0){ for ($i = 0; $i < $size; $i++) { $tmp = $resource[$i]->getResource(); if(!(java_is_null($tmp))){ $buf[] = $tmp->getName(); } } } return $buf; }
将解析出的文件内容,写入a.txt
只是简单的解析,还有待完善