ASP.Net Read microsoft project MPP file using opensource mpxj and net.sf.mpxj wih vb

ASP.Net Read microsoft project MPP file using opensource mpxj or net.sf.mpxj in vb language

Due to limitation of interop , i have used mpxj so that it can be used in clients machine for web based access.. Following code is used to read mpp data and bind this to grid which can be later exported to SAP PS Module..

Prerequisite
1. Download mpxj from internet unzip it in root folder of application
2. right click on project add reference
3. browse to unzip folder and add mpx.dll
4. import following classes and add functions mentioned in bottom of this code.


Imports net.sf.mpxj
Imports net.sf.mpxj.reader
Imports System.Collections.Generic
Imports System.Collections
Imports java.util

Partial Class _wbsx
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

 Dim maxlevel = 0
        Dim currentlevel = 0
        Dim wbs = ""
        
        Dim reader = ProjectReaderUtility.getProjectReader(Server.MapPath("./upload/") & "mspfile.mpp")
        Dim pFile As ProjectFile = reader.read(Server.MapPath("./upload/") & "mspfile.mpp")
        Dim id As Integer = 1
        Dim i As Integer
        i = 1
        For Each task As Task In ToEnumerable(pFile.getAllTasks())
            If Not task Is Nothing Then
                If Not task.getSummary Then  'remove yes

                    currentlevel = task.getWBS.ToString.Split(".").Count     '1 will store 3
                    If maxlevel < currentlevel Then
                        maxlevel = currentlevel
                        wbs = task.getWBS
                    End If
                    i = i + 1
                End If
            End If
        Next
        ' temp = " WBS " & wbs & " MaxLevel " & maxlevel.ToString
        Dim basewbs = txtWBS.Text   '"IN-C24320.2.19.04" '"IN-C24320.2.12"  ' "IN-C24320.2.19.10"
        Dim baselevel = basewbs.ToString.Split(".").Count  'Total level should not exceed 7/8. expected is 6
        Dim taskwbs = ""
        '' to generate levels AA,AB,AC ... etc
        Dim AX() = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
        Dim XA() = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
        Dim AXcount = 0
        Dim XAcount = 0
        Dim j = 0
        Dim mytable = New System.Data.DataTable
        mytable.Columns.Add("activity")
        mytable.Columns.Add("Level")
        mytable.Columns.Add("wbs")
        mytable.Columns.Add("descr")
        mytable.Columns.Add("summary")
        mytable.Columns.Add("temp1")
        mytable.Columns.Add("temp2")
        For Each task As Task In ToEnumerable(pFile.getAllTasks())
            Dim myrow = mytable.NewRow
            If Not task Is Nothing Then
                'Rule : If summary is true then unique wbs else copy wbs of the parent
                If task.getSummary Then
                    currentlevel = task.getWBS.ToString.Split(".").Count

                    taskwbs = basewbs
                    Dim s() = task.getWBS.ToString.Split(".")
                    Dim t = ""
                    For j = 0 To s.Length - 1
                        If Not j = 0 And baselevel + j <= 6 Then  'j=0 first octet not required, and j
                            t = t & "." & s(j).PadLeft(2, "0")
                        ElseIf baselevel + j > 6 Then
                            t = Left(t, t.Length - 3) & "." & AX(AXcount).ToString & XA(XAcount).ToString
                            XAcount = XAcount + 1
                            If XAcount = 25 Then  ' reached AZ so reset for BA
                                AXcount = AXcount + 1  'A to B
                                XAcount = 0  'Z to A
                            End If
                        End If

                    Next
                    taskwbs = basewbs & t

                    myrow(0) = task.getID.toString.PadLeft(4, "0")
                    myrow(1) = (baselevel + j - 1).ToString
                    myrow(2) = taskwbs
                    myrow(3) = task.getName
                    myrow(4) = task.getSummary
                    myrow(5) = (Split(taskwbs, ".").Count).ToString
                    myrow(6) = task.getWBS
                    mytable.Rows.Add(myrow)
                    id = id + 1
                Else
          
                    myrow(0) = task.getID.toString.PadLeft(4, "0")
                    myrow(1) = (baselevel + j - 1).ToString
                    myrow(2) = taskwbs
                    myrow(3) = task.getName
                    myrow(4) = task.getSummary
                    myrow(5) = (Split(taskwbs, ".").Count).ToString
                    myrow(6) = task.getWBS
                    mytable.Rows.Add(myrow)

                End If
            End If
        Next

        GridView1.DataSource = mytable
        GridView1.DataBind()

End Sub

Private Shared Function ToEnumerable(javaCollection As Collection) As EnumerableCollection
        Return New EnumerableCollection(javaCollection)
    End Function
    Class EnumerableCollection
        Public Sub New(collection As Collection)
            m_collection = collection
        End Sub

        Public Function GetEnumerator() As IEnumerator
            Return New Enumerator(m_collection)
        End Function

        Private m_collection As Collection
    End Class
    Public Structure Enumerator
        Implements IEnumerator
        Public Sub New(collection As Collection)
            m_collection = collection
            m_iterator = m_collection.iterator()
        End Sub

        Public ReadOnly Property Current() As Object Implements IEnumerator.Current
            Get
                Return m_iterator.[next]()
            End Get
        End Property

        Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
            Return m_iterator.hasNext()
        End Function

        Public Sub Reset() Implements IEnumerator.Reset
            m_iterator = m_collection.iterator()
        End Sub

        Private m_collection As Collection
        Private m_iterator As Iterator
    End Structure
End Class


- Written By
Vinod Kotiya

Comments