我正在尝试使用vb.net将网格数据导出到ms项目。我在网格中有这些数据
ID Task
1 Task1
1.1 Task2
1.1.1 Task2.1
1.1.1.2 Task2.2
1.2 Task3
1.2.1 Task3.1
但在将其导出到MS Project之后我得到的输出为
Task Name OutlineLevel OutlineNumber
Task1 1 1
Task2 2 1.1
Task3 2 1.2
Task2.2 3 1.2.1
Task2.3 3 1.2.2
Task3.1 3 1.2.3
虽然任务顺序添加,但会生成错误的大纲编号。它还会在两者之间添加一个空行。 我使用了以下代码
Private Sub ubtnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ubtnExport.Click
Try
Dim ProjectApp As New Microsoft.Office.Interop.MSProject.ApplicationClass()
ProjectApp.AppMaximize()
ProjectApp.FileNew(Type.Missing, Type.Missing, Type.Missing, Type.Missing)
Dim Project As Microsoft.Office.Interop.MSProject.Project = ProjectApp.ActiveProject
Dim Task As Microsoft.Office.Interop.MSProject.Task
dtTempWBSDetail = CType(Grid.DataSource, DataTable)
For Each dtRow As DataRow In dtTempWBSDetail.Select("Hlevel=1")
Task = Project.Tasks.Add(dtRow("Particular"), dtRow("UniqueNo"))
Task.OutlineLevel = dtRow("HLevel")
Task.Text1 = dtRow("Id")
If Val(Convert.ToString(dtRow("% Complete"))) <> 0 Then
Task.PercentComplete = dtRow("% Complete")
End If
CreateSubTask(dtRow("WBSNo"), dtRow("WBSTaskNo"), dtRow("HLevel"), Task, Project)
Next
Project = Nothing
ProjectApp = Nothing
Catch ex As Exception
Throw ex
End Try
End Sub
Sub CreateSubTask(ByVal WBSNo As Integer, ByVal WBSTaskNo As Integer, ByVal HLevel As Short, ByVal Task As Microsoft.Office.Interop.MSProject.Task, ByVal Project As Microsoft.Office.Interop.MSProject.Project)
For Each dtRow As DataRow In dtTempWBSDetail.Select("IsNull(WBSParentTaskNo,0) =" & WBSTaskNo.ToString & " and IsNull(WBSParentNo,0)=" & WBSNo)
Dim ChildTask As Microsoft.Office.Interop.MSProject.Task = Task.OutlineChildren.Add(dtRow("Particular"), dtRow("UniqueNo"))
ChildTask.OutlineLevel = dtRow("HLevel")
ChildTask.Text1 = dtRow("Id")
If Val(Convert.ToString(dtRow("% Complete"))) <> 0 Then
ChildTask.PercentComplete = dtRow("% Complete")
End If
CreateSubTask(dtRow("WBSNo"), dtRow("WBSTaskNo"), dtRow("HLevel"), ChildTask, Project)
Next
End Sub
答案 0 :(得分:0)
首先在Task.add()方法中出现问题。 第一个参数是任务名称和 第二个参数是任务在其包含集合中的位置。 我提供的第二个参数是错误的,生成的空白行仅由它决定。
其次,仅创建任务的大纲结构Task.OutlineLevel是不够的,因为它指定了层次结构中的任务级别。 Task.outlineNumber精确指定任务在层次结构中的位置,但它是一个只读属性。 所以我使用了Task.OutlineIndent()和Task.OutlineOutdent()方法。
我使用了代码 昏暗的iTaskIndex作为整数 Dim PrevOutlineLevel As Short
Private Sub ubtnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ubtnExport.Click
Try
Dim ProjectApp As New Microsoft.Office.Interop.MSProject.ApplicationClass()
ProjectApp.AppMaximize()
ProjectApp.FileNew(Type.Missing, Type.Missing, Type.Missing, Type.Missing)
Dim Project As Microsoft.Office.Interop.MSProject.Project = ProjectApp.ActiveProject
Dim Task As Microsoft.Office.Interop.MSProject.Task
dtTempWBSDetail = CType(ugdTaskDetails.DataSource, DataTable)
iTaskIndex = 1
PrevOutlineLevel = 1
For Each dtRow As DataRow In ugdTaskDetails.Rows
If Convert.ToString(dtRow("Id")) <> "" Then
AddTaskToProject(Project, dtRow)
iTaskIndex = iTaskIndex + 1
End If
Next
Project = Nothing
ProjectApp = Nothing
Catch ex As Exception
Throw ex
End Try
End Sub
Sub AddTaskToProject(ByVal Project As Microsoft.Office.Interop.MSProject.Project, ByVal dtRow As Datarow)
Dim Task As Microsoft.Office.Interop.MSProject.Task
Task = Project.Tasks.Add(dtRow("Particular"), iTaskIndex)
If Val(Convert.ToString(dtRow("HLevel"))) - PrevOutlineLevel > 0 Then
Task.OutlineIndent()
Else
For Ol As Short = 1 To PrevOutlineLevel - Val(Convert.ToString(dtRow("HLevel")))
Task.OutlineOutdent()
Next
End If
Task.Text1 = dtRow("Id")
If Val(Convert.ToString(dtRow("% Complete"))) <> 0 Then
Task.PercentComplete = dtRow("% Complete")
End If
PrevOutlineLevel = dtRow("HLevel")
End Sub