天天看点

微软project文件mpp解析

最近在做一个项目管理的项目,主要是将用户在project文件中写的一些东西,读出来,并将其写入到数据库中。

也是借鉴了好多大佬的思想和代码,感觉自己需要整理一遍,所以,接下来就是表演的时候了。

第一步:首先创建一个实体类TaskInfo,这个类的作用就是将原本project文件中读取出的内置Task填充到自己的TaskInfo中进行处理。

还有这里用的jar包是mpxj。

package com.mammoth.entity;

import java.util.Date;

public class TaskInfo {

    private int project_id;                         // 所属项目ID
    private int task_id;                            // 任务ID
    private int task_unique_id;                     // 任务唯一ID
    private String parent_id;                       // 父任务ID
    private String task_type;                       // 任务类型(FS,SS,FF,SF)
    private int task_outline_level;                 // 任务级别
    private String task_name;                       // 任务名称
    private double task_duration;                   // 任务工期
    private String task_start_date;                  // 任务开始时间
    private String task_finish_date;                 // 任务结束时间
    private String task_predecessors;               // 任务流
    private String task_operator;                   // 负责人

  
    public int getProject_id() {
        return project_id;
    }
    public void setProject_id(int project_id) {
        this.project_id = project_id;
    }
    public int getTask_id() {
        return task_id;
    }
    public void setTask_id(int task_id) {
        this.task_id = task_id;
    }
    public int getTask_unique_id() {
        return task_unique_id;
    }
    public void setTask_unique_id(int task_unique_id) {
        this.task_unique_id = task_unique_id;
    }
    public String getParent_id() {
        return parent_id;
    }
    public void setParent_id(String parent_id) {
        this.parent_id = parent_id;
    }
    public String getTask_type() {
        return task_type;
    }
    public void setTask_type(String task_type) {
        this.task_type = task_type;
    }
    public int getTask_outline_level() {
        return task_outline_level;
    }
    public void setTask_outline_level(int task_outline_level) {
        this.task_outline_level = task_outline_level;
    }
    public String getTask_name() {
        return task_name;
    }
    public void setTask_name(String task_name) {
        this.task_name = task_name;
    }
    public double getTask_duration() {
        return task_duration;
    }
    public void setTask_duration(double task_duration) {
        this.task_duration = task_duration;
    }
    public String getTask_start_date() {
        return task_start_date;
    }
    public void setTask_start_date(String task_start_date) {
        this.task_start_date = task_start_date;
    }
    public String getTask_finish_date() {
        return task_finish_date;
    }
    public void setTask_finish_date(String task_finish_date) {
        this.task_finish_date = task_finish_date;
    }
    public String getTask_predecessors() {
        return task_predecessors;
    }
    public void setTask_predecessors(String task_predecessors) {
        this.task_predecessors = task_predecessors;
    }
    public String getTask_operator() {
        return task_operator;
    }
    public void setTask_operator(String task_operator) {
        this.task_operator = task_operator;
    }
    @Override
    public String toString() {
        return "TaskInfo [project_id=" + project_id + ", task_id=" + task_id + ", task_unique_id=" + task_unique_id
                + ", parent_id=" + parent_id + ", task_type=" + task_type + ", task_outline_level=" + task_outline_level
                + ", task_name=" + task_name + ", task_duration=" + task_duration + ", task_start_date="
                + task_start_date + ", task_finish_date=" + task_finish_date + ", task_predecessors="
                + task_predecessors + ", task_operator=" + task_operator + "]";
    }
}      

第二步:写工具类,将mpxj jar包中的Task的对象解析出来,然后再封装到自己的TaskInfo中。

package com.mammoth.utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mammoth.entity.TaskInfo;

import net.sf.mpxj.MPXJException;
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.Relation;
import net.sf.mpxj.Task;
import net.sf.mpxj.mpp.MPPReader;

public class TaskUtils {
    
    public static List<TaskInfo> readFile(){
        List<TaskInfo> taskList = new ArrayList<TaskInfo>();
        try{    
            //1. 读取文件
            File file = new File("C:/Users/ruixuan/Desktop/测试project.mpp");
            MPPReader mppRead = new MPPReader();
            ProjectFile pf = mppRead.read(file);
            //2. 拿到task对象
            List<Task> tasks = pf.getAllTasks();
            //3. 遍历task对象,找到其中的值
            for (int i = 0; i < tasks.size(); i++) {
                Task task = tasks.get(i);
                Integer task_id = task.getID();
                Integer task_unique_id = task.getUniqueID();
                Integer task_outline_level = task.getOutlineLevel();
                double task_duration = task.getDuration().getDuration();
                Date task_start_date = task.getStart();
                Date task_finish_date = task.getFinish();
                List<Relation> task_predecessors = task.getPredecessors();                              
                
                //4. 获取前置任务(任务流)
                StringBuilder beforeTaskId = new StringBuilder();
                StringBuilder beforeTaskType = new StringBuilder();
                if(task_predecessors != null){
                    if(task_predecessors.size() > 0){
                        for(Relation relation : task_predecessors){
                            Integer targetTaskId = relation.getTargetTask().getID();
                            if(beforeTaskId.length() == 0){
                                beforeTaskId.append(targetTaskId);
                                beforeTaskType.append(relation.getType());
                            }else{
                                beforeTaskId.append("," + targetTaskId);
                                beforeTaskType.append("," + relation.getType());
                            }
                        }
                    }
                }
                String task_predecessors_str = beforeTaskId.toString(); 
                String task_predecessors_str_type = beforeTaskType.toString();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                //5. 给taskinfo实体类赋值
                TaskInfo taskInfo = new TaskInfo();
                taskInfo.setTask_id(task_id);
                taskInfo.setTask_unique_id(task_unique_id);
                taskInfo.setTask_outline_level(task_outline_level);
                taskInfo.setTask_duration(task_duration);
                taskInfo.setTask_start_date(format.format(task_start_date));
                taskInfo.setTask_finish_date(format.format(task_finish_date));
                taskInfo.setTask_predecessors(task_predecessors_str);
                taskInfo.setTask_type(task_predecessors_str_type);
                taskInfo.setTask_name(task.getName());
                
                //6.自定义列读取
                String task_operator = task.getText(2);
                taskInfo.setTask_operator(task_operator);
                taskList.add(taskInfo);             
            }               
        }catch (MPXJException e) {
            return null;  
        } catch (Exception e) { 
            return null;  
        }       
        return taskList;
    }
    
    
    // NO.2 获取TaskInfo之间的父子关联关系
    public static List<TaskInfo> refreshTaskInfo(List<TaskInfo> taskList){
        List<Map<String,Integer>> tempTaskOutLine = new ArrayList<Map<String,Integer>>();
        for(TaskInfo taskInfo : taskList){
            int taskId = taskInfo.getTask_id();
            int taskOutLineLevel = taskInfo.getTask_outline_level();            
            int listSize = tempTaskOutLine.size();
            // 初始化taskOutLineLevel
            if(listSize > 2){               
                if(taskOutLineLevel == 1){                  
                    for(int i=listSize;i>2;i--){
                        tempTaskOutLine.remove(i-1);
                    }
                    listSize = 2;   
                }               
            }
            Map<String,Integer> map = new HashMap<String,Integer>();
            map.put("taskId", taskId);
            map.put("taskOutLineLevel", taskOutLineLevel);
            if(listSize == 0){
                if(taskOutLineLevel == 0){
                    tempTaskOutLine.add(map);
                }else{
                    return null;
                }
            }else{
                Map<String,Integer> lastMap = tempTaskOutLine.get(listSize-1);
                String lastTaskId = lastMap.get("taskId")+"";
                int lastTaskOutLineLevel = lastMap.get("taskOutLineLevel");
                if(taskOutLineLevel > lastTaskOutLineLevel){
                    tempTaskOutLine.add(map);
                    taskInfo.setParent_id(lastTaskId);
                }else if(taskOutLineLevel == lastTaskOutLineLevel){ 
                    tempTaskOutLine.set(taskOutLineLevel, map);
                    Map<String,Integer> lastMap1 = tempTaskOutLine.get(taskOutLineLevel-1);
                    String lastTaskId1 = lastMap1.get("taskId")+"";
                    taskInfo.setParent_id(lastTaskId1);
                }else if(taskOutLineLevel < lastTaskOutLineLevel){                  
                    tempTaskOutLine.set(taskOutLineLevel, map);
                    Map<String,Integer> lastMap2 = tempTaskOutLine.get(taskOutLineLevel-1);
                    String lastTaskId2 = lastMap2.get("taskId")+"";
                    taskInfo.setParent_id(lastTaskId2);
                }
            }                       
        }
        taskList.remove(0);
        return taskList;
    }   
}      

这里的地址我测试用的是本地的一个地址,在实际开发中会在上传的时候进行解析,拿到文件流的形式即可,接下来我会写一篇关于上传解析文件的博客,也希望大佬们多多指点

第三步:测试

package com.mammoth.test;

import java.util.List;

import org.junit.Test;

import com.mammoth.entity.TaskInfo;
import com.mammoth.utils.TaskUtils;

public class TaskTest {

    @Test
    public void taskTest(){
//        List<TaskInfo> lists = TaskUtils.readFile();
        List<TaskInfo> lists = TaskUtils.refreshTaskInfo(TaskUtils.readFile());
        int i = 1;
        for (TaskInfo taskInfo : lists) {
            System.out.println("第" + i + "次任务:"+ taskInfo.toString());
            i++;
        }
    }
}
      

如果可以正常运行的话,可以看到控制台打印如下内容

微软project文件mpp解析

得到这个list集合之后 ,还不是你想干什么就干什么了。

感谢http://blog.csdn.net/loongshawn/article/details/51038051,这篇博客,感谢大佬