ImportMatrixWorkbook
(
Microsoft.Office.Interop.Excel.Application application,
String sourceWorkbookName,
Boolean sourceWorkbookHasVertexNames,
GraphDirectedness sourceWorkbookDirectedness
)
{
Debug.Assert(application != null);
Debug.Assert( !String.IsNullOrEmpty(sourceWorkbookName) );
AssertValid();
// Get the active worksheet of the source workbook.
Worksheet oSourceWorksheet = GetActiveSourceWorksheet(
application, sourceWorkbookName);
// Read or create the names of the vertices in the source workbook.
String [] asVertexNames;
Int32 iFirstEdgeWeightRowOneBased, iFirstEdgeWeightColumnOneBased;
if (sourceWorkbookHasVertexNames)
{
asVertexNames = ReadVertexNames(oSourceWorksheet);
iFirstEdgeWeightRowOneBased = iFirstEdgeWeightColumnOneBased = 2;
}
else
{
asVertexNames = CreateVertexNames(oSourceWorksheet);
iFirstEdgeWeightRowOneBased = iFirstEdgeWeightColumnOneBased = 1;
}
Int32 iVertices = asVertexNames.Length;
switch (sourceWorkbookDirectedness)
{
case GraphDirectedness.Directed:
break;
case GraphDirectedness.Undirected:
CheckSymmetryOfUndirectedMatrix(oSourceWorksheet,
iFirstEdgeWeightRowOneBased,
iFirstEdgeWeightColumnOneBased, iVertices);
break;
default:
Debug.Assert(false);
break;
}
// Create a graph and populate it with the vertices.
IGraph oGraph = new Graph(sourceWorkbookDirectedness);
IVertexCollection oVertices = oGraph.Vertices;
IVertex[] aoOrderedVertices = new IVertex[iVertices];
for (Int32 i = 0; i < iVertices; i++)
{
IVertex oVertex = oVertices.Add();
oVertex.Name = asVertexNames[i];
aoOrderedVertices[i] = oVertex;
}
// Read the edges and import them into the graph.
ReadEdges(oSourceWorksheet, iFirstEdgeWeightRowOneBased,
iFirstEdgeWeightColumnOneBased, oGraph, aoOrderedVertices);
return (oGraph);
}