So konvertieren Sie Microsoft -Projektdateien in das CSV -Format
Dieser Artikel enthält detaillierte Anweisungen und Code -Beispiele für das Konvertieren von Microsoft -Projektdateien MPP in CSV -Format mit Aspose.Tasks für .NET. Es werden auch verschiedene Exportoptionen wie Filterung, Sortierung, Sortierung, Codierung, Trennzeicheneinstellungen und Header -Sichtbarkeit gezeigt.
MS -Projektdateien in CSV konvertieren
Es gibt zwei Möglichkeiten, das Microsoft-Projektprojekt in das CSV-Format (Comma-getrennte Werte) umzuwandeln: Verwenden Sie SaveFileformat Aufzählung oder die CSVOPTIONS -Klasse.
Um die MP -Projekt -MPP -Datei in das CSV -Format mit Standardeinstellungen mit SaveFileFormat umzuwandeln:
- Erstellen Sie eine neue Projektinstanz und laden Sie die MPP -Datei.
- Konvertieren Sie das Projekt in CSV unter Verwendung von Project.Save -Methode und geben Sie die SaveFileFileformat.csv als Argument an.
1Project project = new Project("New Project.mpp");
2project.Save("SaveProjectAsCSV_out.csv", SaveFileFormat.CSV);
To convert MPP files with a non-default settings the CsvOptions class can be used. The CsvOptions class allows specification of additional export parameters while rendering project pages to CSV.
- Create a new project instance and load the MPP file.
- Convert the project to CSV using the Project.Save method and pass the CsvOptions instance as the argument.
Presented below is .NET example showing how to change text delimiter using the options:
1Project project = new Project("New Project.mpp");
2CsvOptions options = new CsvOptions();
3options.TextDelimiter = CsvTextDelimiter.Semicolon;
4project.Save("UsingCsvOptions_out.csv", options);
How to change the view while convert MPP to CSV
By default the Gantt Chart view is used to decide what column headers will be exported. To change the view the DataCategory property can be used:
1Project project = new Project("New Project.mpp");
2CsvOptions options = new CsvOptions();
3// to change what columns will be exported the DataCategory property can be used
4// changing the data category from DataCategory.Tasks to DataCategory.Resources
5options.DataCategory = DataCategory.Resources;
6project.Save("CsvOptionsWithResourceView.csv", options);
How to sort tasks while converting to CSV
By using CsvOptions one can tune the export parameters e.g. control the view to export and/or filter exported tasks. Lets take a look at some of the options available.
For example, one can customize the sorting order of the tasks by assigning a custom comparer to the TasksComparer property. Presented below is the example where tasks sorted by name are exported reversed order:
1Project project = new Project("New Project.mpp");
2project.RootTask.Children.Add("Task");
3CsvOptions options = new CsvOptions
4{
5 TasksComparer = new ReversedByNameTaskComparer()
6};
7project.Save("CsvOptionsWithReversedTasks.csv", options);
8
9// ...
10
11private sealed class ReversedByNameTaskComparer : IComparer<Task>
12{
13 public int Compare(Task x, Task y)
14 {
15 if (x == null)
16 {
17 throw new ArgumentNullException("x");
18 }
19
20 if (y == null)
21 {
22 throw new ArgumentNullException("y");
23 }
24
25 return -1 * string.Compare(x.Get(Tsk.Name), y.Get(Tsk.Name), StringComparison.Ordinal);
26 }
27}
How to Filter Tasks While Convert to CSV
In order to filter the exported tasks one can specify the task filter by assigning a custom condition to the TasksFilter property:
1Project project = new Project("New Project.mpp");
2CsvOptions options = new CsvOptions();
3options.TasksFilter = new OutlineCodeFilter();
4project.Save("CsvOptionsWithFilteredTasks.csv", options);
5
6// ...
7
8class OutlineCodeFilter : ICondition<Task>
9{
10 public bool Check(Task el)
11 {
12 var code1 = el.OutlineCodes.GetByFieldId((int)ExtendedAttributeTask.OutlineCode1);
13 var code2 = el.OutlineCodes.GetByFieldId((int)ExtendedAttributeTask.OutlineCode2);
14 return code1 != null && code2 != null;
15 }
16}
How to change delimiter while converting to CSV
CSV is a text format that uses text delimiters to split data fields. To change the text delimiter one can set the TextDelimiter property to one of the next values:
- Comma;
- Semicolon (the default delimiter);
- Space;
- Tab.
The available delimiters are defined in CsvTextDelimiter enumeration.
Presented below is the example in which data fields are being split by tab symbol:
1Project project = new Project("New Project.mpp");
2CsvOptions options = new CsvOptions();
3// changing text delimiter to tabs
4options.TextDelimiter = CsvTextDelimiter.Tab;
5project.Save("CsvOptionsWithCustomDelimiter.csv", options);
How to change encoding while converting to CSV
By default CSV is being exported in Encoding.Default encoding. To change the encoding the Encoding property can be used:
1Project project = new Project("New Project.mpp");
2CsvOptions options = new CsvOptions();
3// changing the text encoding
4options.Encoding = Encoding.UTF8;
5project.Save("CsvOptionsWithCustomEncoding.csv", options);
How to hide column headers while converting to CSV
By default column headers are being exported in CSV format. To suppress export of column headers the IncludeHeaders property can be used:
1Project project = new Project("New Project.mpp");
2CsvOptions options = new CsvOptions();
3// suppress export of column headers
4options.IncludeHeaders = false;
5project.Save("CsvOptionsWithoutColumnHeaders.csv", options);
In summary, Aspose.Tasks for .NET provides a flexible and extensible API for exporting project data to CSV format. The CsvOptions class allows customization of output according to specific project requirements, such as task filtering, sorting, and text encoding.