介绍
mpxj是一个开源项目,可以在mvnrepository中直接搜索到,最新版本已经更新到了9.5.1版本:
各种插件皆可使用。
使用
该插件可以直接读取project文件,但是不能写为project文件,只能通过转换为XML格式来对project文件进行操作。
读:
写:
/*
* 新建一个计划文件
* @Author 清风
*/
public static String newProject (String name, Date startDate) {
ProjectFile prj = new ProjectFile();
prj.getProjectProperties().setProjectTitle(name);
// 添加计划根任务
Task root = prj.addTask();
root.setStart(startDate);
root.setName("第一个任务");
root.setDuration(Duration.getInstance(1, TimeUnit.DAYS));
root.setStart(new Date());
root.setFinish(new Date());
root.setID(1);
root.setUniqueID(1);
Task root2 = prj.addTask();
root2.setStart(startDate);
root2.setName("第二个任务");
root2.setDuration(Duration.getInstance(1, TimeUnit.DAYS));
root2.setStart(new Date());
root2.setFinish(new Date());
root2.setID(2);
root2.setUniqueID(2);
//前置关系
Relation r=root2.addPredecessor(root);
r.setType(RelationType.FINISH_START);
r.setTaskUniqueID(root.getUniqueID());
//日历,如果不设置工作天数则默认为五天工作制
ProjectCalendar pc=prj.addBaseCalendar();
ProjectCalendarHours h1=pc.addCalendarHours(Day.SUNDAY);
//设置工作时间
h1.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START1, ProjectCalendar.DEFAULT_END1));
h1.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START2, ProjectCalendar.DEFAULT_END2));
ProjectCalendarHours h2=pc.addCalendarHours(Day.MONDAY);
h2.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START1, ProjectCalendar.DEFAULT_END1));
h2.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START2, ProjectCalendar.DEFAULT_END2));
ProjectCalendarHours h3=pc.addCalendarHours(Day.TUESDAY);
h3.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START1, ProjectCalendar.DEFAULT_END1));
h3.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START2, ProjectCalendar.DEFAULT_END2));
ProjectCalendarHours h4=pc.addCalendarHours(Day.WEDNESDAY);
h4.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START1, ProjectCalendar.DEFAULT_END1));
h4.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START2, ProjectCalendar.DEFAULT_END2));
ProjectCalendarHours h5=pc.addCalendarHours(Day.THURSDAY);
h5.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START1, ProjectCalendar.DEFAULT_END1));
h5.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START2, ProjectCalendar.DEFAULT_END2));
ProjectCalendarHours h6=pc.addCalendarHours(Day.FRIDAY);
h6.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START1, ProjectCalendar.DEFAULT_END1));
h6.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START2, ProjectCalendar.DEFAULT_END2));
ProjectCalendarHours h7=pc.addCalendarHours(Day.SATURDAY);
h7.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START1, ProjectCalendar.DEFAULT_END1));
h7.addDateRange(new DateRange (ProjectCalendar.DEFAULT_START2, ProjectCalendar.DEFAULT_END2));
//更新项目结构
prj.updateStructure();
MSPDIWriter writer = new MSPDIWriter();
try(ByteArrayOutputStream buffer = new ByteArrayOutputStream()){
writer.write(prj, buffer);
return buffer.toString("utf-8");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
注:该插件计算父子级是按照ID读取顺序及OutlineLevel属性来决定父子级的,注意注意。