📄 project.cs
字号:
using System;
using System.Collections;
using MSProject = Microsoft.Office.Interop.MSProject;
namespace PrjXlsRpt
{
/// <summary>
/// Project
///
/// Revision History
/// ----------------
/// 20060103 mcarey file created
///
/// Copyright (c) 2006 Michael Carey. All Rights Reserved/
/// </summary>
public class Project
{
private ArrayList tasks;
private ArrayList current;
private ArrayList future;
private ArrayList late;
public Project()
{
Initialize();
}
private void Initialize() {
tasks = new ArrayList();
current = new ArrayList();
future = new ArrayList();
late = new ArrayList();
}
/// <summary>
/// Loads all the tasks from the Microsoft Project file into
/// this class.
/// </summary>
/// <param name="fileName">The full path name of the Microsoft Project file.</param>
/// <returns>On succes an empty string. On error, the error description.</returns>
public string Load(string fileName) {
MSProject.ApplicationClass app = null;
string retVal = "";
Initialize();
try {
// execute the Microsoft Project Application
app = new MSProject.ApplicationClass();
// Do not display Microsoft Project
app.Visible = false;
// open the project file.
if(app.FileOpen(fileName, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing)) {
// go through all the open projects--there should only be one
foreach(MSProject.Project proj in app.Projects) {
// go through all the tasks in the project
foreach(MSProject.Task task in proj.Tasks) {
// we are only interested in tasks that do not have
// any child tasks--these are the tasks that we
// want to track.
if(task.OutlineChildren.Count == 0) {
// copy the Microsoft Project Task to our task
// and add it to our task list.
tasks.Add(new PrjXlsRpt.Task(task));
}
}
}
} else {
retVal = "The MS Project file " + fileName + " could not be opened.";
}
} catch (Exception ex) {
retVal = "Could not process the MS Project file " + fileName + "." + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.StackTrace;
}
// close the application if is was opened.
if(app != null) {
app.Quit(MSProject.PjSaveType.pjDoNotSave);
}
return retVal;
}
/// <summary>
/// Evalutes the tasks based upon day that you want to evalute the task
/// from, and the number of days to look into the future.
/// </summary>
/// <param name="dayToEvaluateFrom">The day that you want to start evaluating the tasks from.</param>
/// <param name="pastDays">The number of days to look into the past for the data to be considered current.</param>
/// <param name="futureDays">The number of days that you want to look into the future.</param>
public void EvaluateTasks(System.DateTime dayToEvaluateFrom, int pastDays, int futureDays) {
System.DateTime currentDate = dayToEvaluateFrom.AddDays(-pastDays);
System.DateTime futureDate = dayToEvaluateFrom.AddDays(futureDays);
// clear out all of our data storage.
late.Clear();
current.Clear();
future.Clear();
// go through an evaluate each task
foreach(Task task in tasks) {
// evaluate the late tasks
if(task.FinishDate <= dayToEvaluateFrom && task.PercentComplete != 100) {
late.Add(task);
}
// evaluate the current tasks. we have for scenarios
// 1. the task started after currentDate but before datToEvaluateFrom
// 2. the task finished after currentDate but before datToEvaluateFrom
// 3. the task started before currentDate and ends after the dayToEvaluateFrom
if((task.StartDate >= currentDate && task.StartDate <= dayToEvaluateFrom) || // 1
(task.FinishDate >= currentDate && task.FinishDate <= dayToEvaluateFrom) || // 2
(task.StartDate < currentDate && task.FinishDate > dayToEvaluateFrom)) { // 3
current.Add(task);
}
// evaluate the future tasks
if(task.StartDate > dayToEvaluateFrom && task.StartDate <= futureDate) {
future.Add(task);
}
}
}
/// <summary>
/// Saves the data to the XML file.
/// </summary>
/// <param name="fileName">The full path name of the file to save to.</param>
/// <returns>On success an empty string; on error the error message.</returns>
public string Save(string fileName) {
System.IO.StreamWriter writer = null;
string retVal = "";
try {
writer = new System.IO.StreamWriter(fileName, false, System.Text.Encoding.ASCII);
writer.WriteLine(XmlPart1());
foreach(Task task in current) {
writer.WriteLine(task.ToXml());
}
writer.WriteLine(XmlPart2());
foreach(Task task in late) {
writer.WriteLine(task.ToXml());
}
writer.WriteLine(XmlPart3());
foreach(Task task in future) {
writer.WriteLine(task.ToXml());
}
writer.WriteLine(XmlPart4());
} catch (Exception ex) {
retVal = "Could not save the file to " + fileName + "." + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.StackTrace;
} finally {
if(writer != null) {
writer.Close();
}
}
return retVal;
}
#region XML
/// <summary>
/// Returns the first part of the Excel XML file. All the XML up to the
/// items in the Current wrok sheet.
/// </summary>
/// <returns>The first part of the Excel XML file.</returns>
private string XmlPart1() {
string s = "";
s += "<?xml version=\"1.0\"?>" + System.Environment.NewLine;
s += "<?mso-application progid=\"Excel.Sheet\"?>" + System.Environment.NewLine;
s += "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"" + System.Environment.NewLine;
s += "xmlns:o=\"urn:schemas-microsoft-com:office:office\"" + System.Environment.NewLine;
s += "xmlns:x=\"urn:schemas-microsoft-com:office:excel\"" + System.Environment.NewLine;
s += "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"" + System.Environment.NewLine;
s += "xmlns:html=\"http://www.w3.org/TR/REC-html40\">" + System.Environment.NewLine;
s += "<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">" + System.Environment.NewLine;
s += "<Author>mcarey</Author>" + System.Environment.NewLine;
s += "<LastAuthor>mcarey</LastAuthor>" + System.Environment.NewLine;
s += "<Created>2007-01-03T19:00:53Z</Created>" + System.Environment.NewLine;
s += "<LastSaved>2007-01-03T21:07:24Z</LastSaved>" + System.Environment.NewLine;
s += "<Company></Company>" + System.Environment.NewLine;
s += "<Version>11.8107</Version>" + System.Environment.NewLine;
s += "</DocumentProperties>" + System.Environment.NewLine;
s += "<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">" + System.Environment.NewLine;
s += "<WindowHeight>8700</WindowHeight>" + System.Environment.NewLine;
s += "<WindowWidth>15195</WindowWidth>" + System.Environment.NewLine;
s += "<WindowTopX>480</WindowTopX>" + System.Environment.NewLine;
s += "<WindowTopY>135</WindowTopY>" + System.Environment.NewLine;
s += "<ProtectStructure>False</ProtectStructure>" + System.Environment.NewLine;
s += "<ProtectWindows>False</ProtectWindows>" + System.Environment.NewLine;
s += "</ExcelWorkbook>" + System.Environment.NewLine;
s += "<Styles>" + System.Environment.NewLine;
s += "<Style ss:ID=\"Default\" ss:Name=\"Normal\">" + System.Environment.NewLine;
s += "<Alignment ss:Vertical=\"Bottom\"/>" + System.Environment.NewLine;
s += "<Borders/>" + System.Environment.NewLine;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -