问题
I'm having a heckuva time finding any C# code examples using the MPXJ library when connecting to a Microsoft Project file. Can someone please post a snippet demonstrating how to write the contents of a table in an .mpp file to screen?
Bonus points for any links/references!
Thanks!
~Dan
回答1:
hopefully this will help.
First you need to open your project file:
ProjectReader reader = ProjectReaderUtility.getProjectReader(inputFile);
ProjectFile projectFile = reader.read(inputFile);
This assumes that you have a file name in the inputFile string.
The method below should be treated as pseudocode (i.e. I haven't compiled it, shaken the bugs out of it and so on, and it's not the most elegant thing I've ever written), but it illustrates the approach:
public void dumpTables(ProjectFile file)
{
List tables = file.getTables();
Iterator iter = tables.iterator();
while (iter.hasNext())
{
Table table = (Table)iter.next();
if (table.getResourceFlag())
{
List resources = file.getAllResources();
Iterator resourceIter = resources.iterator();
while (resourceIter.hasNext())
{
Resource resource = (Resource)iter.next();
List columns = table.getColumns();
Iterator columnIter = columns.iterator();
while (columnIter.hasNext())
{
Column column = (Column)columnIter.next();
Object columnValue = resource.getCachedValue(column.getFieldType());
Console.Write(columnValue);
Console.Write(",");
}
Console.WriteLine();
}
}
else
{
List tasks = file.getAllTasks();
// etc. as above
}
}
}
The idea is that you are retrieving the list of tables present in the file, and for each one working out if it is a Task or Resource table. Based on this you'll grab the list of tasks or resources, iterate through that, and for each instance pull out the column value and display it. Note that I've not made any attempt to order the tasks or resources in any particular way. I'll leave that as an exercise for the reader!
Hope that helps!
Jon
来源:https://stackoverflow.com/questions/4454923/example-of-mpxj-library-in-c-sharp