WriteOverallMetricsToNewWorkbook ( Microsoft.Office.Interop.Excel.Application oApplicationForNewWorkbook, IList <OverallMetricsInfo> oOverallMetricsInfos ) { Debug.Assert(oApplicationForNewWorkbook != null); Debug.Assert(oOverallMetricsInfos != null); AssertValid(); Workbook oNewWorkbook = oApplicationForNewWorkbook.Workbooks.Add(Missing.Value); Int32 iOverallMetricsInfos = oOverallMetricsInfos.Count; // There are 16 overall metrics, plus one extra column for the file // name. const Int32 Columns = 1 + 16; // There is one extra row for the headers. Object [,] aoCellValues = ExcelUtil.Get2DArray <Object>(1 + iOverallMetricsInfos, Columns); String [] asColumnHeaders = { "File Name", OverallMetricNames.Directedness, OverallMetricNames.Vertices, OverallMetricNames.UniqueEdges, OverallMetricNames.EdgesWithDuplicates, OverallMetricNames.TotalEdges, OverallMetricNames.SelfLoops, OverallMetricNames.ReciprocatedVertexPairRatio, OverallMetricNames.ReciprocatedEdgeRatio, OverallMetricNames.ConnectedComponents, OverallMetricNames.SingleVertexConnectedComponents, OverallMetricNames.MaximumConnectedComponentVertices, OverallMetricNames.MaximumConnectedComponentEdges, OverallMetricNames.MaximumGeodesicDistance, OverallMetricNames.AverageGeodesicDistance, OverallMetricNames.GraphDensity, OverallMetricNames.Modularity, }; Debug.Assert(asColumnHeaders.Length == Columns); for (Int32 iColumn = 0; iColumn < Columns; iColumn++) { aoCellValues[1, iColumn + 1] = asColumnHeaders[iColumn]; } for (Int32 i = 0; i < iOverallMetricsInfos; i++) { OverallMetricsInfo oOverallMetricsInfo = oOverallMetricsInfos[i]; OverallMetrics oOverallMetrics = oOverallMetricsInfo.OverallMetrics; Object [] aoColumnValues = { oOverallMetricsInfo.NodeXLWorkbookFileName, oOverallMetrics.Directedness.ToString(), oOverallMetrics.Vertices, oOverallMetrics.UniqueEdges, oOverallMetrics.EdgesWithDuplicates, oOverallMetrics.TotalEdges, oOverallMetrics.SelfLoops, OverallMetricCalculator2.NullableToGraphMetricValue <Double>( oOverallMetrics.ReciprocatedVertexPairRatio), OverallMetricCalculator2.NullableToGraphMetricValue <Double>( oOverallMetrics.ReciprocatedEdgeRatio), oOverallMetrics.ConnectedComponents, oOverallMetrics.SingleVertexConnectedComponents, oOverallMetrics.MaximumConnectedComponentVertices, oOverallMetrics.MaximumConnectedComponentEdges, OverallMetricCalculator2.NullableToGraphMetricValue <Int32>( oOverallMetrics.MaximumGeodesicDistance), OverallMetricCalculator2.NullableToGraphMetricValue <Double>( oOverallMetrics.AverageGeodesicDistance), OverallMetricCalculator2.NullableToGraphMetricValue <Double>( oOverallMetrics.GraphDensity), OverallMetricCalculator2.NullableToGraphMetricValue <Double>( oOverallMetrics.Modularity), }; Debug.Assert(aoColumnValues.Length == Columns); for (Int32 iColumn = 0; iColumn < Columns; iColumn++) { aoCellValues[i + 2, iColumn + 1] = aoColumnValues[iColumn]; } } Worksheet oNewWorksheet = (Worksheet)oNewWorkbook.Worksheets[1]; // Insert the overall metrics into a table. Range oTableRange = ExcelUtil.SetRangeValues( (Range)oNewWorksheet.Cells[1, 1], aoCellValues); ExcelTableUtil.AddTable(oNewWorksheet, oTableRange, null, null); }
TryGetGraphMetricsForOneNodeXLWorkbook ( String sNodeXLWorkbookFilePath, out OverallMetricsInfo oOverallMetricsInfo ) { Debug.Assert( !String.IsNullOrEmpty(sNodeXLWorkbookFilePath) ); AssertValid(); oOverallMetricsInfo = null; // Create a new instance of Excel. Do not use the instance that was // passed to AggregateGraphMetricsAsync(), because when a NodeXL // workbook is opened and closed in Excel, its memory is not released // and the machine will eventually run out of memory. Application oExcelApplication = new Application(); if (oExcelApplication == null) { throw new Exception("Excel couldn't be opened."); } // ExcelApplicationKiller requires that the application be visible. oExcelApplication.Visible = true; ExcelApplicationKiller oExcelApplicationKiller = new ExcelApplicationKiller(oExcelApplication); Workbook oNodeXLWorkbook = null; try { oNodeXLWorkbook = ExcelUtil.OpenWorkbook(sNodeXLWorkbookFilePath, oExcelApplication); OverallMetrics oOverallMetrics; if ( ( new OverallMetricsReader() ).TryReadMetrics( oNodeXLWorkbook, out oOverallMetrics ) ) { oOverallMetricsInfo = new OverallMetricsInfo(); oOverallMetricsInfo.NodeXLWorkbookFileName = Path.GetFileName(sNodeXLWorkbookFilePath); oOverallMetricsInfo.OverallMetrics = oOverallMetrics; return (true); } } finally { if (oNodeXLWorkbook != null) { oNodeXLWorkbook.Close(false, Missing.Value, Missing.Value); } oExcelApplication.Quit(); // Quitting the Excel application does not remove it from // memory. Kill its process. oExcelApplicationKiller.KillExcelApplication(); } return (false); }
TryGetGraphMetricsForOneNodeXLWorkbook ( String sNodeXLWorkbookFilePath, out OverallMetricsInfo oOverallMetricsInfo ) { Debug.Assert(!String.IsNullOrEmpty(sNodeXLWorkbookFilePath)); AssertValid(); oOverallMetricsInfo = null; // Create a new instance of Excel. Do not use the instance that was // passed to AggregateGraphMetricsAsync(), because when a NodeXL // workbook is opened and closed in Excel, its memory is not released // and the machine will eventually run out of memory. Application oExcelApplication = new Application(); if (oExcelApplication == null) { throw new Exception("Excel couldn't be opened."); } // ExcelApplicationKiller requires that the application be visible. oExcelApplication.Visible = true; ExcelApplicationKiller oExcelApplicationKiller = new ExcelApplicationKiller(oExcelApplication); Workbook oNodeXLWorkbook = null; try { oNodeXLWorkbook = ExcelUtil.OpenWorkbook(sNodeXLWorkbookFilePath, oExcelApplication); OverallMetrics oOverallMetrics; if ((new OverallMetricsReader()).TryReadMetrics( oNodeXLWorkbook, out oOverallMetrics)) { oOverallMetricsInfo = new OverallMetricsInfo(); oOverallMetricsInfo.NodeXLWorkbookFileName = Path.GetFileName(sNodeXLWorkbookFilePath); oOverallMetricsInfo.OverallMetrics = oOverallMetrics; return(true); } } finally { if (oNodeXLWorkbook != null) { oNodeXLWorkbook.Close(false, Missing.Value, Missing.Value); } oExcelApplication.Quit(); // Quitting the Excel application does not remove it from // memory. Kill its process. oExcelApplicationKiller.KillExcelApplication(); } return(false); }