Java使用MPXJ操作MS Project
MPXJ是一个开源的包,网址http://mpxj.sourceforge.net/index.html
它需要POI的支持。
它可以读取MPP文件(不能写MPP文件!!!要经过XML过渡。。。)
可以读写MPX文件,MPX就是一个纯文本文件,里面有TASK,Resource之类的信息,按一定格式书写的。
Project 98及之前版本可以另存为MPX文件,从Project 2000开始就不支持另存为MPX格式了,
但是,它们仍然可以读取MPX文件。
MPXJ的读写的类主要有这么几个:
读:
net.sf.mpxj.mpp.MPPReader: reads Microsoft Project MPP files
net.sf.mpxj.mpx.MPXReader: reads Microsoft MPX files
net.sf.mpxj.mspdi.MSPDIReader: reads Microsoft MSPDI (XML) files
net.sf.mpxj.mpd.MPDIReader: reads Microsoft MPD files
net.sf.mpxj.planner.PlannerReader: reads Planner (XML) files
写:
net.sf.mpxj.mpx.MPXWriter: writes Microsoft MPX files
net.sf.mpxj.mspdi.MSPDIWriter: writes Microsoft MSPDI (XML) files
net.sf.mpxj.planner.PlannerWriter: writes Planner (XML) files
所以,目前可以做的就是:
1.读取MPP文件信息,写入DB之类的……
2.new 一个ProjectFile,写入Task,Resources,Calendar之类的信息,
然后使用MPXWriter写入MPX文件,然后用Project打开,好像会有个导入数据的对话框。
……
MPXJ的数据结构:
一个Project由Task,Resources,Calendar构成,Task与Calendar,Resources与Calendar都有关系。
Task和Resources通过ResourceAssignment关联。
===========================================================
下面是使用MPXWrite的一段代码,转载的,参考参考。
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);
}
}