DEV Community

loading...

Create MS Project converter for .NET 5.0 in 5 steps

alexanderefremov profile image Alexander Efremov ・2 min read

Writing the Microsoft Project Converter

Aspose.Tasks is one of the leading C#/Java/C++ on-premises API for Microsoft Project/Primavera project management file processing. And today I want to show how to develop your own .NET MS Project converter (commonly misspelled as "MS Project convertor") in just a few steps.

Step 1. Creating a new .NET Core Console App

Let's create a new C# project and add Aspose.Tasks for .NET NuGet package:

mkdir MSProjectConverter && cd MSProjectConverter
Enter fullscreen mode Exit fullscreen mode
dotnet new console && dotnet add package Aspose.Tasks
Enter fullscreen mode Exit fullscreen mode

Step 2. Writing a Converter

The next step is to implement the converter class that uses Aspose.Tasks as a converter engine:

namespace MSProjectConverter
{
    using System.IO;

    using Aspose.Tasks;
    using Aspose.Tasks.Saving;

    public class MppToExcelConverter
    {
        public MemoryStream ConvertToXlsx(Project project)
        {
            MemoryStream stream = new MemoryStream();
            project.Save(stream, SaveFileFormat.XLSX);
            return stream;
        }

        public MemoryStream ConvertToSpreadsheet2003(Project project)
        {
            MemoryStream stream = new MemoryStream();
            project.Save(stream, SaveFileFormat.Spreadsheet2003);
            return stream;
        }

        public MemoryStream ConvertToCsv(Project project)
        {
            MemoryStream stream = new MemoryStream();
            project.Save(stream, SaveFileFormat.CSV);
            return stream;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3. Using the Converter

Use the converter in user code:

namespace MSProjectConverter
{
  using System;
  using System.IO;

  using Aspose.Tasks;

  public static class Program
  {
    public static void Main(string[] args)
    {
      if (args.Length < 3)
      {
        throw new 
          ArgumentException(
            "At least 3 arguments must be passed.");
      }

      string mppPath = args[0];
      string type = args[1];
      string outPath = args[2];

      Project project = new Project(mppPath);
      MppToExcelConverter converter = new MppToExcelConverter();

      MemoryStream stream = null;
      try
      {

        switch (type)
        {
          case "XLSX":
            stream = converter.ConvertToXlsx(project);
            break;
          case "XML":
            stream = converter.ConvertToSpreadsheet2003(project);
            break;
          case "CSV":
            stream = converter.ConvertToCsv(project);
            break;
          default:
            throw new 
             ArgumentException("The conversion type is unknown.");
        }

        File.WriteAllBytes(outPath, stream.ToArray());
      }
      finally
      {
        if (stream != null)
        {
          stream.Dispose();
        }
      }

      Console.WriteLine(
        "The file " + 
        $"{Path.GetFileName(mppPath)} was successfully " + 
        "converted to " + 
        $"{Path.GetFileName(outPath)}.");
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 4. Build the Console App

Let's build the project:

dotnet build
Enter fullscreen mode Exit fullscreen mode

Step 5. Using MPP to Excel converter

Try to use the converter app and convert project to Excel, e.g. to CSV format:

MSProjectConverter.exe example.mpp CSV example.csv
Enter fullscreen mode Exit fullscreen mode

The output is:

The file example.mpp was successfully converted to example.csv.
Enter fullscreen mode Exit fullscreen mode

Presented below are the input MPP file and the output CSV file:
ms project convertor

mpp to excel converter

The created application can be found here.

If you would like to take more control over MPP to Excel export e.g. apply custom filters for CSV export or customize the exported data fields (columns) for XLSX export then you can refer to our conversion guide.

How to convert MS Project to Excel Online

You can try our free web conversion apps to convert MPP to Excel or to 10+ other file formats - these applications use the up-to-date version Aspose.Tasks for .NET under the hood.

Discussion (1)

pic
Editor guide
Collapse
filatovv profile image
Yuri Filatov

Great article