Introduction
If the data resides in Excel, then you have a lot of options to extract the data irrespective of technology. But if the same requirement comes for a MPP (Microsoft Project Plan) file, you have a limited number of options.
MPXJ is an open source library that is used to retrieve data from MPP file. This library provides a set of facilities to allow project information to be manipulated in Java and .NET.
MPXJ supports a range of data formats:
- Microsoft Project (MPP, MPT)
- Microsoft Project Exchange (MPX)
- XML
- MPD
- And various others
MPXJ is based around a "neutral" data structure which is used to represent project data, coupled with a set of format-specific reader and writer classes which understand how to read from and write to the various supported file formats. The library is currently based around a set of structures modeled on the data described by the MPP file format. All manipulation of project data takes place using these data structures, which can be read from or written to the various supported file formats.
The figure below illustrates the key entities represented by the MPXJ data structure.
Fig 1.0
MPXJ currently allows project data to be read from eight distinct data sources using the following reader classes:
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.MPDReader
: reads Microsoft MPD files net.sf.mpxj.planner.PlannerReader
: reads Planner (XML) files net.sf.mpxj.primavera.PrimaveraDatabaseReader
: reads from a Primavera database net.sf.mpxj.primavera.PrimaveraPMFileReader
: reads Primavera PM XML files
net.sf.mpxj.primavera.PrimaveraXERFileReader
: reads Primavera XER files
A similar arrangement exists for the writer classes:
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
MPXJ is written and maintained in Java, however this is no barrier to using its functionality in .NET. IKVM.NET allows MPXJ to be used from any .NET programming language without having to be aware that the original code was written in Java. IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:
- A Java virtual machine implemented in .NET
- A .NET implementation of the Java class libraries
- Tools that enable Java and .NET interoperability
In order to use this library, you can use the following steps:
- Add Reference of MPXJ.dll and all necessary DLLs (provided DLLs)
- We need to create class EnumerableCollection.cs to wrap IKVM collection to allow enumeration:
public class EnumerableCollection
{
public EnumerableCollection(Collection collection)
{
m_collection = collection;
}
public IEnumerator GetEnumerator()
{
return new Enumerator(m_collection);
}
private Collection m_collection;
}
public class Enumerator : IEnumerator
{
public Enumerator(Collection collection)
{
m_collection = collection;
m_iterator = m_collection.iterator();
}
public object Current
{
get
{
return m_iterator.next();
}
}
public bool MoveNext()
{
return m_iterator.hasNext();
}
public void Reset()
{
m_iterator = m_collection.iterator();
}
private Collection m_collection;
private Iterator m_iterator;
}
- Import the following namespaces:
using net.sf.mpxj;
using net.sf.mpxj.reader;
using net.sf.mpxj.writer;
using net.sf.mpxj.mpp;
- Create object of ‘
ProjectReader
’ and ‘ProjectFile
’ class
ProjectReader reader = new MPPReader();
ProjectFile projectObj = reader.read(<FileName>);
- For easy enumeration in collection, we can create a method ‘
ToEnumerable
’
private static EnumerableCollection ToEnumerable(Collection javaCollection)
{
return new EnumerableCollection(javaCollection);
}
- Now if we want to access tasks, subtasks, calendar, resources from MPP file, then we need to use methods using
ProjectFile
Object
- Tasks
foreach (Task task in ToEnumerable(projectObj.getAllTasks()))
{
Console.WriteLine("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" + task.getUniqueID());
}
- Resources
foreach (Resource resource in ToEnumerable(projectObj.getAllResources())
{
Console.WriteLine("Resource: " + resource.getName() + "(Unique ID=" + resource.getUniqueID() + ")");
}
- SubTasks
foreach (Task child in ToEnumerable(task.getChildTasks()))
{
Console.WriteLine(indent + "Task: " + child.getName());
listHierarchy(child, indent + " ");
}
- Access Resource and Task by ID
Resource r = project.getResourceByUniqueID(Integer.valueOf(99));
Task t = project.getTaskByUniqueID(Integer.valueOf(99));
- Calendar
ProjectCalendar defaultCalendar = projectObj.getCalendar();
ProjectCalendar taskCalendar = task.getCalendar();
- There are many methods provided by MPXJ library you can use according to your requirements.
- Now if you want to create a MPP file, use the following steps:
- Create Source
ProjectFile
Object
-
ProjectReader reader = new MPPReader();
-
ProjectFile sourceObj = reader.read(<sourcefilename>);
- Create object of ’
ProjectWriter
’ class
ProjectWriter writer = ProjectWriterUtility.getProjectWriter(<outputFile Name>);
writer.write(sourceObj, <outputFile Name>);
Reference(s)