C# 中的 MPXJ 库示例

发布于 2024-10-07 19:12:15 字数 141 浏览 9 评论 0原文

连接到 Microsoft Project 文件时,我很难找到任何使用 MPXJ 库的 C# 代码示例。有人可以发布一段片段,演示如何将 .mpp 文件中的表格内容写入屏幕吗?

任何链接/参考的奖励积分!

谢谢!

~丹

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一直在等你来 2024-10-14 19:12:15

希望这会有所帮助。

首先,您需要打开项目文件:

ProjectReader reader = ProjectReaderUtility.getProjectReader(inputFile);
ProjectFile projectFile = reader.read(inputFile);

这假设您在 inputFile 字符串中有一个文件名。

下面的方法应该被视为伪代码(即我还没有编译它,摆脱其中的错误等等,并且它不是我写过的最优雅的东西),但它说明了该方法:

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
        }
    }
}

这个想法是您正在检索文件中存在的表列表,并为每个表确定它是任务表还是资源表。在此基础上,您将获取任务或资源列表,对其进行迭代,并为每个实例提取列值并显示它。请注意,我没有尝试以任何特定方式订购任务或资源。我将把它作为练习留给读者!

希望有帮助!

乔恩

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文