ExportToNewMatrixWorkbook() { AssertValid(); // Merge duplicate edges and add an edge weight column. (new DuplicateEdgeMerger()).MergeDuplicateEdges(m_oWorkbookToExport); // Read the workbook, including the edge weight column. ReadWorkbookContext oReadWorkbookContext = new ReadWorkbookContext(); oReadWorkbookContext.ReadEdgeWeights = true; IGraph oGraph = (new WorkbookReader()).ReadWorkbook( m_oWorkbookToExport, oReadWorkbookContext); // Get an array of non-isolated vertices. Isolated vertices don't get // exported. List <IVertex> oNonIsolatedVertices = GraphUtil.GetNonIsolatedVertices(oGraph); Int32 iNonIsolatedVertices = oNonIsolatedVertices.Count; if (iNonIsolatedVertices == 0) { throw new ExportWorkbookException( "There are no edges to export." ); } Workbook oNewWorkbook = m_oWorkbookToExport.Application.Workbooks.Add(Missing.Value); Worksheet oNewWorksheet = (Worksheet)oNewWorkbook.ActiveSheet; // Fill in row 1 and column A with the vertex names, starting at B1 and // A2, respectively. String [,] asVertexNamesForRow1 = ExcelUtil.GetSingleRow2DStringArray( iNonIsolatedVertices); String [,] asVertexNamesForColumnA = ExcelUtil.GetSingleColumn2DStringArray(iNonIsolatedVertices); for (Int32 i = 0; i < iNonIsolatedVertices; i++) { asVertexNamesForRow1[1, i + 1] = asVertexNamesForColumnA[i + 1, 1] = oNonIsolatedVertices[i].Name; } ExcelUtil.SetRangeValues((Range)oNewWorksheet.Cells[1, 2], asVertexNamesForRow1); ExcelUtil.SetRangeValues((Range)oNewWorksheet.Cells[2, 1], asVertexNamesForColumnA); asVertexNamesForRow1 = asVertexNamesForColumnA = null; // Now fill in the edge weights, row by row. Range oFirstColumnCell = (Range)oNewWorksheet.Cells[2, 2]; for (Int32 i = 0; i < iNonIsolatedVertices; i++) { Object [,] aoEdgeWeights = ExcelUtil.GetSingleRow2DArray( iNonIsolatedVertices); IVertex oVertexI = oNonIsolatedVertices[i]; for (Int32 j = 0; j < iNonIsolatedVertices; j++) { aoEdgeWeights[1, j + 1] = EdgeUtil.GetEdgeWeight(oVertexI, oNonIsolatedVertices[j]); } ExcelUtil.SetRangeValues(oFirstColumnCell, aoEdgeWeights); oFirstColumnCell = oFirstColumnCell.get_Offset(1, 0); } return(oNewWorkbook); }
SaveGraphCore ( IGraph graph, Stream stream ) { Debug.Assert(graph != null); Debug.Assert(stream != null); AssertValid(); // Get an array of non-isolated vertices. Isolated vertices don't get // saved. List <IVertex> oNonIsolatedVertices = GraphUtil.GetNonIsolatedVertices(graph); Int32 iNonIsolatedVertices = oNonIsolatedVertices.Count; if (oNonIsolatedVertices.Count == 0) { OnSaveError("There are no edges to export."); } StreamWriter oStreamWriter = new StreamWriter(stream, StreamEncoding); // Write the graph metadata. oStreamWriter.WriteLine( "DL" + "\r\nN={0}" + "\r\nFORMAT = FULLMATRIX DIAGONAL PRESENT" , iNonIsolatedVertices.ToString(CultureInfo.InvariantCulture) ); foreach (String sLabelHeader in new String [] { "ROW LABELS:", "COLUMN LABELS:" }) { oStreamWriter.WriteLine(sLabelHeader); foreach (IVertex oVertex in oNonIsolatedVertices) { oStreamWriter.WriteLine("\"" + oVertex.Name + "\""); } } // Now fill in the edge weights, row by row. oStreamWriter.WriteLine("DATA:"); for (Int32 i = 0; i < iNonIsolatedVertices; i++) { IVertex oVertexI = oNonIsolatedVertices[i]; for (Int32 j = 0; j < iNonIsolatedVertices; j++) { Double dEdgeWeight = EdgeUtil.GetEdgeWeight(oVertexI, oNonIsolatedVertices[j]); oStreamWriter.Write(dEdgeWeight.ToString( CultureInfo.InvariantCulture) + " "); } oStreamWriter.WriteLine(); } oStreamWriter.Flush(); }