当前位置: 首页 > 工具软件 > MPXJ > 使用案例 >

java mpxj mpxwriter_Java使用MPXJ操作MS Project

施兴言
2023-12-01

package mpx;

import java.util.Locale;

import net.sf.mpxj.DateRange;

import net.sf.mpxj.Day;

import net.sf.mpxj.Duration;

import net.sf.mpxj.FieldType;

import net.sf.mpxj.ProjectCalendar;

import net.sf.mpxj.ProjectCalendarHours;

import net.sf.mpxj.ProjectFile;

import net.sf.mpxj.ProjectHeader;

import net.sf.mpxj.Relation;

import net.sf.mpxj.RelationType;

import net.sf.mpxj.Resource;

import net.sf.mpxj.Task;

import net.sf.mpxj.TaskField;

import net.sf.mpxj.TimeUnit;

import net.sf.mpxj.mpx.MPXWriter;

import net.sf.mpxj.utility.NumberUtility;

public class CreateMPX

{

public CreateMPX()

{

}

public static void main(String args[])

{

try

{

create("d:\\output.MPX");

}

catch (Exception ex)

{

ex.printStackTrace(System.out);

}

}

private static void create(String filename) throws Exception

{

ProjectFile file =new ProjectFile();

Task task1 = file.addTask();

task1.setName("task1");

task1.setUniqueID(1);

task1.setID(1);

task1.setOutlineLevel(task0.getOutlineLevel()+1);

Task task11 = task1.addTask();

task11.setName("task1-1");

task11.setDuration(Duration.getInstance(6, TimeUnit.DAYS));

task11.setStart(java.sql.Date.valueOf("2008-8-21"));

task11.setFinish(java.sql.Date.valueOf("2008-8-26"));

task11.setPercentageComplete(NumberUtility.getDouble(90D));

task11.setUniqueID(2);

task11.setID(2);

task11.setResourceNames("one");

Task task12 = task1.addTask();

task12.setName("task12");

task12.setDuration(Duration.getInstance(5, TimeUnit.DAYS));

task12.setStart(java.sql.Date.valueOf("2008-8-27"));

task12.setFinish(java.sql.Date.valueOf("2008-8-31"));

task12.setResourceNames("two");

task12.setUniqueID(3);

task12.setID(3);

//前置关系

Relation r=task12.addPredecessor(task11);

r.setType(RelationType.FINISH_START);

r.setTaskUniqueID(task11.getUniqueID());

ProjectCalendar pc=file.addBaseCalendar();

//设置工作日历 这里设的周1-7全部工作,呵呵 如不设默认为5天工作制

pc.setName("压榨民工");

pc.setWorkingDay(Day.SUNDAY, true);

pc.setWorkingDay(Day.MONDAY, true);

pc.setWorkingDay(Day.TUESDAY, true);

pc.setWorkingDay(Day.WEDNESDAY, true);

pc.setWorkingDay(Day.THURSDAY, true);

pc.setWorkingDay(Day.FRIDAY, true);

pc.setWorkingDay(Day.SATURDAY, true);

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));

ProjectHeader ph=file.getProjectHeader();

ph.setCalendarName(pc.getName());

MPXWriter writer = new MPXWriter();

//设置中文

writer.setLocale(Locale.CHINESE);

writer.write(file, filename);

}

}

 类似资料: