这个小主题通过提供有关设置环境和使用示例代码的详细信息,重点介绍如何使用 Java 在 MPP 中添加工作日。它涵盖了有关用于开发此应用程序的所有必需资源的信息,而不依赖于 MS Project 或任何其他 MPP 管理软件来使用 Java 在 Microsoft Project 中定义工作日。
使用 Java 在 MPP 文件中添加工作日的步骤
- 建立开发环境以包含来自存储库管理器的 Aspose.Tasks for Java JAR
- 通过创建 Project 类的实例生成新的 MPP 文件
- 实例化 Calendar 类以添加新的日历和工作日
- 包括特定半天的工作时间并将其添加到日历中
- 将 MPP 项目文件保存在磁盘上
Java 中的上述步骤使用非常简单的 API 接口在 MPP 文件中添加工作日。该过程首先使用 Project 类对象加载现有 MPP 文件或创建新的 MPP 文件,然后使用 Calendar 类的实例添加一个新日历,其中包括星期几。最后,您将使用 SaveFileFormat 枚举器将 MPP 文件保存在磁盘上或内存流中。
使用 Java 在 MPP 中定义工作日的代码
import com.aspose.tasks.Calendar; | |
import com.aspose.tasks.DayType; | |
import com.aspose.tasks.License; | |
import com.aspose.tasks.Project; | |
import com.aspose.tasks.SaveFileFormat; | |
import com.aspose.tasks.SaveOptions; | |
import com.aspose.tasks.SvgOptions; | |
import com.aspose.tasks.Timescale; | |
import com.aspose.tasks.WeekDay; | |
import com.aspose.tasks.WorkingTime; | |
import java.util.Date; | |
public class WeekdaysForCalender { | |
public static void main(String[] args) throws Exception {// Throws exception to add workdays inside the calendar of the MPP file | |
String FilePath = "//Users//test//Documents//KnowledgeBase//"; | |
// Apply the product license to process the MPP file | |
License tasksWorkDayLic = new License(); | |
tasksWorkDayLic.setLicense(FilePath + "Conholdate.Total.Product.Family.lic"); | |
// Create a default project file using an instance of the Project class | |
Project taskProjectFile = new Project(); | |
// Define Calendar | |
Calendar calendar = taskProjectFile.getCalendars().add("Test Calendar1"); | |
// Include the weekly working days Monday through Thursday with their default timings | |
calendar.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Monday)); | |
calendar.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Tuesday)); | |
calendar.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Wednesday)); | |
calendar.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Thursday)); | |
calendar.getWeekDays().add(new WeekDay(DayType.Saturday)); | |
calendar.getWeekDays().add(new WeekDay(DayType.Sunday)); | |
// Set Friday as the short working day | |
WeekDay myFriday = new WeekDay(DayType.Friday); | |
// Set the working time. | |
WorkingTime firstWorkingTime = new WorkingTime(new Date(2022, 1, 1, 9, 0, 0 ), new Date(2022, 1, 1, 12, 0, 0)); | |
WorkingTime secondWorkingTime = new WorkingTime(new Date(2022, 1, 1, 13, 0, 0),new Date(2022, 1, 1, 16, 0, 0)); | |
// Adding working time to the custom working day (Friday) | |
myFriday.getWorkingTimes().add(secondWorkingTime); | |
myFriday.getWorkingTimes().add(secondWorkingTime); | |
myFriday.setDayWorking (true); | |
// Adding the weekdays to the calendar | |
calendar.getWeekDays().add(myFriday); | |
// Saving the generated MPP file on the disk | |
taskProjectFile.save(FilePath+"Project_DefineCalendarWeekdays_out.mpp", SaveFileFormat.Mpp); | |
} | |
} |
在上面的示例中,我们倾向于在 Java 的 MPP 文件中添加工作日。我们将通过加载现有 MPP 文件或从头开始添加新文件来启动。 Calendar 类允许您添加不同类型的日历,包括夜间、标准和 24 小时轮班。您还可以设置任何一天工作,并使用 WorkingTime 类实例设置特定日期的工作时间。项目文件可以保存为其他格式,包括 MPP、PDF 和 XML。
在本文中,我们重点讨论了如何使用 Java 在 MPP 文件中定义工作日。如果您想了解如何将 MPP 转换为 SVG,请阅读 如何使用 Java 将 MPP 转换为 SVG 上的文章。