EN · DE · RU · FR · ES

#454: ExportMSProject.java

projectforge-business/src/main/java/org/projectforge/business/gantt/ExportMSProject.java Type: Java · Role: Gantt Chart · Source: projectforge-business/src/main/java/org/projectforge/business/gantt/ExportMSProject.java 231 lines · 151 code · 60 comments · 20 blank
Excel/CSV export utility for MSProject data. Configures column headers (i18n), field formatters (currency, date), and populates rows from data collections.

Code Structure

Package: org.projectforge.business.gantt

Classes: ExportMSProject

Methods (3): addTask, setPredecessors, getRelationType

Imports: 15 packages

Source Code (abridged)

package org.projectforge.business.gantt;

import net.sf.mpxj.*;
import net.sf.mpxj.mpx.MPXWriter;
import net.sf.mpxj.mspdi.MSPDIWriter;
import net.sf.mpxj.writer.ProjectWriter;
import org.projectforge.framework.calendar.Holidays;
import org.projectforge.framework.time.PFDateTime;
import org.projectforge.framework.time.PFDayUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Uses the implementation of http://mpxj.sourceforge.net/, which is distributed under the terms of the GNU LGPL.
 *
 * @author Kai Reinhard (k.reinhard@micromata.de)
 */
public class ExportMSProject {
  private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExportMSProject.class);

  public static byte[] exportXml(final GanttChart ganttChart) {
    return export(new MSPDIWriter(), ganttChart);
  }

  public static byte[] exportMpx(final GanttChart ganttChart) {
    return export(new MPXWriter(), ganttChart);
  }

  private static byte[] export(final ProjectWriter result, final GanttChart ganttChart) {
    final ProjectFile file = new ProjectFile();

    //
    // Configure the file to automatically generate identifiers for tasks.
    //
    file.setAutoTaskID(true);
    file.setAutoTaskUniqueID(true);

    //
    // Configure the file to automatically generate identifiers for resources.
    //
    file.setAutoResourceID(true);
    file.setAutoResourceUniqueID(true);

    //
    // Configure the file to automatically generate outline levels
    // and outline numbers.
    //
    file.setAutoOutlineLevel(true);
    file.setAutoOutlineNumber(true);

    //
    // Configure the file to automatically generate WBS labels
    //
    file.setAutoWBS(true);

    //
    // Configure the file to automatically generate identifiers for calendars
    // (not strictly necessary here, but required if generating MSPDI files)
    //
    file.setAutoCalendarUniqueID(true);

    //
    // Retrieve the project header and set the start date. Note Microsoft
    // Project appears to reset all task dates relative to this date, so this
    // date must match the start date of the earliest task for you to see
    // the expected results. If this value is not set, it will default to
    // today's date.
    //
    ganttChart.recalculate();
    final ProjectHeader header = file.getProjectHeader();
    header.setStartDate(PFDayUtils.convertToUtilDate(ganttChart.getCalculatedStartDate()));

    //
    // Add a default calendar called "Standard"
    //
    final ProjectCalendar calendar = file.addDefaultBaseCalendar();
    calendar.setWorkingDay(Day.SATURDAY, false);
    calendar.setWorkingDay(Day.SUNDAY, false);
    PFDateTime dt = PFDateTime.from(ganttChart.getCalculatedStartDate()); // not null
    for (int i = 0; i < 3000; i++) { // Endless loop protection (paranoia)
      dt = dt.plusDays(1);
      Holidays holidays = Holidays.getInstance();
      if (!holidays.isWorkingDay(dt.getDateTime()) && holidays.isHoliday(dt) && !dt.isWeekend()) {
        // Add this holiday to the calendar:
        final Date date = dt.getSqlDate();
        calendar.addCalendarException(date, date);
        if (log.isDebugEnabled()) {
          log.debug("Add holiday: " + date);
        }
      }
      PFDateTime dtEnd = PFDateTime.from(ganttChart.getCalculatedEndDate()); // not null
      if (!dt.isBefore(dtEnd)) {
        break;
      }
    }

    final List<GanttTask> children = ganttChart.getRootNode().getChildren();
    if (children != null) {
      final Map<Serializable, Task> taskMap = new HashMap<>();
      for (final GanttTask child : children) {
        addTask(file, taskMap, null, child);
      }
      for (final GanttTask child : children) {
        setPredecessors(taskMap, child);
      }
    }

    //
    // Write the file
    //
    final ByteArrayOutputStream ba = new ByteArrayOutputStream();
    try {
// ... (truncated, total 209 lines)

Git History

868d6abb7 2025 -> 2026
63081666f Source file headers: 2024-> 2025.
b6092df09 Copyright 2023 -> 2024
ab45d51fa Copyright 2001-2022 -> 2001-2023.
5f7ef41b8 Copyright 2021 -> 2022