java读取微软project文件数据

如果需要安装激活微软project工具,可以去下面地址中下载:
链接:https://pan.baidu.com/s/1CCja8qPJSMJ8ZfG9wHVePQ 
提取码:k2p5

注:
目前只读取微软project文件数据,没有写和其他操作。
点我下载demo
1.jar包:

<dependency>
    <groupId>net.sf.mpxj</groupId>
    <artifactId>mpxj</artifactId>
    <version>5.2.2</version>
</dependency>

2.创建实体类(project的信息):

package com.fmz.ttt;

/**
 * @author fmz
 * @date 2021/8/12
 */
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 + "]";
    }
}

3.创建解析工具类:

package com.fmz.ttt;

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;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author fmz
 * @date 2021/8/12
 */
public class TaskUtils {
    public static List<TaskInfo> readFile(String fileUrl){
        List<TaskInfo> taskList = new ArrayList<TaskInfo>();
        try{
            //1. 读取文件
            File file = new File(fileUrl);
            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;
    }
}

3.测试类:

package com.fmz.ttt;

import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author fmz
 * @date 2021/8/12
 */
public class mainTest {
    @Test
    public void method(){
//        List<TaskInfo> lists = TaskUtils.readFile();
        List<TaskInfo> lists = TaskUtils.refreshTaskInfo(TaskUtils.readFile("D://固定资产信息系统项目.mpp"));
        int i = 1;
        for (TaskInfo taskInfo : lists) {
            System.out.println("第" + i + "次任务:"+ taskInfo.toString());
            i++;
        }
    }
}

4.效果图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
由于微软Project存盘文件使用的是二进制格式,因此读取需要使用相应的库。以下是一份使用Apache POI库读取Project文件Java代码示例: ```java import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.DocumentInputStream; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import java.io.FileInputStream; import java.io.IOException; public class ProjectReader { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("path/to/project.mpp"); POIFSFileSystem fileSystem = new POIFSFileSystem(fis); // 获取DocumentSummaryInformation和SummaryInformation DocumentSummaryInformation dsi = fileSystem.getDocumentSummaryInformation(); SummaryInformation si = fileSystem.getSummaryInformation(); // 打印文档信息 System.out.println("标题:" + si.getTitle()); System.out.println("主题:" + si.getSubject()); System.out.println("作者:" + si.getAuthor()); System.out.println("关键字:" + si.getKeywords()); System.out.println("注释:" + si.getComments()); System.out.println("创建时间:" + si.getCreateDateTime()); System.out.println("修改时间:" + si.getLastSaveDateTime()); // 读取工作表 DocumentInputStream dis = fileSystem.createDocumentInputStream("Workbook"); HSSFWorkbook workbook = new HSSFWorkbook(dis); Sheet sheet = workbook.getSheetAt(0); // 遍历行和列 for (Row row : sheet) { for (Cell cell : row) { System.out.print(cell.toString() + "\t"); } System.out.println(); } fis.close(); } } ``` 需要注意的是,该代码使用的是Apache POI的旧版本,如果要使用最新的POI库,可能需要做出一些调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哎呦喂O_o嗨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值