프로젝트 파일 읽기

Aspose.Tasks for C ++를 사용하면 Microsoft Project를 설치하거나 Microsoft Office Automation을 사용하지 않고 Microsoft Project (MPP/XML) 파일로 작업 할 수 있습니다. 강력하고 유연한 API Aspose.Tasks 는 C ++ 응용 프로그램에서 프로젝트 파일을 조작하는 데 효율적인 코드를 작성하는 데 필요한 도구를 제공하여 시간과 노력을 절약합니다.

이 기사에서는 프로젝트 파일을 열고 읽는 방법을 설명합니다.

프로젝트 파일 읽기

현재 Aspose.Tasks 는 Microsoft Project를 Microsoft Project의 XML 형식으로 만 내보낼 수 있습니다. 새로운 프로젝트를 만들면 사실입니다. Aspose.Tasks 를 사용하면 기존 MPP 파일을 읽고 업데이트 후 MPP 형식으로 저장할 수 있습니다. 이를 통해 MPP 및 MPT 형식을 입력 템플릿으로 읽을 수 있습니다. 이 기사는 프로젝트 클래스의 생성자를 사용하여 프로젝트 파일 (xml, mpp, mpt)을 읽을 수있는 방법을 보여줍니다.

템플릿으로 프로젝트 파일 읽기

1// The path to the documents directory.
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3    
4// Read existing project template file
5System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"ReadProjectFiles.mpp");

Reading Project File from Stream

 1// The path to the documents directory.
 2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
 3    
 4// Read project xml into file stream
 5{
 6    System::SharedPtr<System::IO::Stream> filesStream = System::MakeObject<System::IO::FileStream>(dataDir + u"ReadProjectFileFromStream.xml", System::IO::FileMode::Open);
 7    // Clearing resources under 'using' statement
 8    System::Details::DisposeGuard<1> __dispose_guard_0({ filesStream});
 9    // ------------------------------------------
10    
11    try
12    {
13        // Create project using file stream
14        System::SharedPtr<Project> project = System::MakeObject<Project>(filesStream);
15    }
16    catch(...)
17    {
18        __dispose_guard_0.SetCurrentException(std::current_exception());
19    }
20}

Ignoring invalid characters during loading Project

Some files may have invalid characters in the custom fields. Microsoft Project does not allow invalid character so the files have been created or manipulated with automation or some other tools. If these files are loaded using the API, they may lead to an exception. In order to ignore such invalid characters, the overloaded constructor of Project class can be used with the delegate method ParseErrorCallBack.

 1void IgnoreInvalidCharactersDuringloadingProject::Run()
 2{
 3    // Open modified xml stream
 4    {
 5        System::SharedPtr<System::IO::MemoryStream> stream = System::MakeObject<System::IO::MemoryStream>(System::Text::Encoding::get_UTF8()->GetBytes(GetModifiedXml()));
 6        // Clearing resources under 'using' statement
 7        System::Details::DisposeGuard<1> __dispose_guard_0({ stream});
 8        // ------------------------------------------
 9        
10        try
11        {
12            System::SharedPtr<Project> project = System::MakeObject<Project>(stream, static_cast<ParseErrorCallback>(CustomDurationHandler));
13        }
14        catch(...)
15        {
16            __dispose_guard_0.SetCurrentException(std::current_exception());
17        }
18    }
19}

Read Password Protected Projects (2003 Format)

The Project class exposes the Project() constructor which is capable of reading password-protected files in 2003 format. Saving a password-protected file is not supported yet.

To read a password-protected project file:

  1. Load a Microsoft Project file.
  2. In the constructor, provide a password as the second argument to the constructor.

The following code example demonstrates how to read password-protected project file.

1// The path to the documents directory.
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3    
4System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"PasswordProtectedProject.mpp", System::String(u"password"));

Working With Encodings

Aspose.Tasks provides support for the encoding of MPX files. The following code example demonstrates how to use the encoding settings.

 1// Specify Encodings
 2{
 3    System::SharedPtr<System::IO::StreamReader> streamReader = System::MakeObject<System::IO::StreamReader>(dataDir + u"Project.mpx", System::Text::Encoding::GetEncoding(u"ISO-8859-1"));
 4    // Clearing resources under 'using' statement
 5    System::Details::DisposeGuard<1> __dispose_guard_0({ streamReader});
 6    // ------------------------------------------
 7    
 8    try
 9    {
10        auto project = System::MakeObject<Project>(streamReader->get_BaseStream());
11    }
12    catch(...)
13    {
14        __dispose_guard_0.SetCurrentException(std::current_exception());
15    }
16}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.