MPXJ 是一個用于處理 Microsoft Project 文件格式(.mpp 和 .mpx)的 Java 庫。它可以幫助您讀取、寫入和轉換這些文件格式。以下是如何使用 MPXJ 插件的簡要指南:
pom.xml
文件中添加以下依賴項: <groupId>net.sf.mpxj</groupId>
<artifactId>mpxj</artifactId>
<version>8.5.3</version>
</dependency>
如果您使用的是 Gradle,請在 build.gradle
文件中添加以下依賴項:
implementation 'net.sf.mpxj:mpxj:8.5.3'
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.reader.ProjectReader;
import java.io.File;
import java.io.IOException;
public class ReadProjectFile {
public static void main(String[] args) {
try {
File file = new File("path/to/your/projectfile.mpp");
ProjectReader reader = new ProjectReader();
ProjectFile projectFile = reader.read(file);
// 獲取項目信息
String projectName = projectFile.getName();
System.out.println("Project Name: " + projectName);
// 遍歷任務
projectFile.getTasks().forEach(task -> {
System.out.println("Task Name: " + task.getName());
System.out.println("Task Duration: " + task.getDuration());
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.Task;
import net.sf.mpxj.writer.ProjectWriter;
import java.io.File;
import java.io.IOException;
public class WriteProjectFile {
public static void main(String[] args) {
ProjectFile projectFile = new ProjectFile();
projectFile.setName("My New Project");
Task task1 = projectFile.addTask();
task1.setName("Task 1");
task1.setDuration(projectFile.getDefaultDuration(1));
Task task2 = projectFile.addTask();
task2.setName("Task 2");
task2.setDuration(projectFile.getDefaultDuration(2));
try {
File file = new File("path/to/your/newprojectfile.mpp");
ProjectWriter writer = new ProjectWriter();
writer.write(projectFile, file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.reader.ProjectReader;
import net.sf.mpxj.writer.ProjectWriter;
import java.io.File;
import java.io.IOException;
public class ConvertProjectFileFormat {
public static void main(String[] args) {
try {
File inputFile = new File("path/to/your/inputfile.mpp");
ProjectReader reader = new ProjectReader();
ProjectFile projectFile = reader.read(inputFile);
File outputFile = new File("path/to/your/outputfile.mpx");
ProjectWriter writer = new ProjectWriter();
writer.write(projectFile, outputFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
這些示例展示了如何使用 MPXJ 插件讀取、寫入和轉換 Microsoft Project 文件。您可以根據自己的需求修改這些示例以滿足您的項目需求。