SaveGraphToNodeXLWorkbook ( DateTime oStartTime, XmlDocument oXmlDocument, String sNetworkConfigurationFilePath, String sNetworkFileFolderPath, Boolean bAutomate, Application oExcelApplication, out String sWorkbookPath ) { Debug.Assert(oXmlDocument != null); Debug.Assert(!String.IsNullOrEmpty(sNetworkConfigurationFilePath)); Debug.Assert(!String.IsNullOrEmpty(sNetworkFileFolderPath)); Debug.Assert(oExcelApplication != null); // Create a new workbook from the NodeXL template. String sNodeXLTemplatePath; if (!ExcelTemplate.ApplicationUtil.TryGetTemplatePath( out sNodeXLTemplatePath)) { throw new SaveGraphToNodeXLWorkbookException( ExitCode.CouldNotFindNodeXLTemplate, String.Format( "The NodeXL Excel template file couldn't be found. It's" + " supposed to be at {0}, but it's not there. Is the NodeXL" + " Excel Template installed? It's required to run this" + " program." , sNodeXLTemplatePath )); } Workbook oNodeXLWorkbook = null; try { oNodeXLWorkbook = oExcelApplication.Workbooks.Add( sNodeXLTemplatePath); } catch (Exception oException) { OnException(oException, ExitCode.CouldNotCreateNodeXLWorkbook, "A NodeXL workbook couldn't be created." ); } // Create a NodeXL graph from the XML document. IGraph oGraph = (new GraphMLGraphAdapter()).LoadGraphFromString( oXmlDocument.OuterXml); try { // Import the graph into the workbook. // // Note that the GraphMLGraphAdapter stored String arrays on the // IGraph object that specify the names of the attributes that it // added to the graph's edges and vertices. These get used by the // ImportGraph method to determine which columns need to be added // to the edge and vertex worksheets. GraphImporter oGraphImporter = new GraphImporter(); oGraphImporter.ImportGraph(oGraph, ( String[] )oGraph.GetRequiredValue( ReservedMetadataKeys.AllEdgeMetadataKeys, typeof(String[])), ( String[] )oGraph.GetRequiredValue( ReservedMetadataKeys.AllVertexMetadataKeys, typeof(String[])), false, oNodeXLWorkbook); // Store the graph's directedness in the workbook. PerWorkbookSettings oPerWorkbookSettings = new ExcelTemplate.PerWorkbookSettings(oNodeXLWorkbook); oPerWorkbookSettings.GraphDirectedness = oGraph.Directedness; if (bAutomate) { // Store an "automate tasks on open" flag in the workbook, // indicating that task automation should be run on it the next // time it's opened. (It is up to the caller of this method to // open the workbook to trigger automation.) oPerWorkbookSettings.AutomateTasksOnOpen = true; } } catch (Exception oException) { OnException(oException, ExitCode.CouldNotImportGraphIntoNodeXLWorkbook, "The network couldn't be imported into the NodeXL workbook." ); } // Save the workbook. Sample workbook path: // // C:\NetworkConfiguration_2010-06-01_02-00-00.xlsx sWorkbookPath = FileUtil.GetOutputFilePath(oStartTime, sNetworkConfigurationFilePath, sNetworkFileFolderPath, String.Empty, "xlsx"); Console.WriteLine( "Saving the network to the NodeXL workbook \"{0}\"." , sWorkbookPath ); try { oNodeXLWorkbook.SaveAs(sWorkbookPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); } catch (Exception oException) { OnException(oException, ExitCode.SaveNetworkFileError, "The NodeXL workbook couldn't be saved." ); } try { oNodeXLWorkbook.Close(false, Missing.Value, Missing.Value); } catch (Exception oException) { OnException(oException, ExitCode.SaveNetworkFileError, "The NodeXL workbook couldn't be closed." ); } }
SaveGraphToNodeXLWorkbook ( XmlDocument oGraphMLDocument, String sGraphMLFilePath, String sNodeXLWorkbookPath, String sNodeXLWorkbookSettingsFilePath, Boolean bSetAutomateTasksOnOpen, Application oExcelApplication ) { Debug.Assert(oGraphMLDocument != null); Debug.Assert(sNodeXLWorkbookPath == null || sNodeXLWorkbookPath.Length > 0); Debug.Assert(oExcelApplication != null); String sWorkbookSettings = null; if (sNodeXLWorkbookSettingsFilePath != null) { try { sWorkbookSettings = GetWorkbookSettings( sNodeXLWorkbookSettingsFilePath); } catch (Exception oException) { OnException(oException, ErrorCode.CouldNotReadWorkbookSettingsFile, String.Format( "The NodeXL options file \"{0}\" couldn't be read." , sNodeXLWorkbookSettingsFilePath ) ); } } // Create a new workbook from the NodeXL template. Workbook oNodeXLWorkbook = null; try { oNodeXLWorkbook = ApplicationUtil.CreateNodeXLWorkbook( oExcelApplication); } catch (IOException oIOException) { throw new ConvertGraphMLToNodeXLWorkbookException( ErrorCode.CouldNotFindNodeXLTemplate, oIOException.Message); } catch (Exception oException) { OnException(oException, ErrorCode.CouldNotCreateNodeXLWorkbook, "A NodeXL workbook couldn't be created." ); } // Create a NodeXL graph from the XML document. IGraph oGraph = (new GraphMLGraphAdapter()).LoadGraphFromString( oGraphMLDocument.OuterXml); try { // Turn off text wrap if necessary to speed up the import. GraphImportTextWrapManager.ManageTextWrapBeforeImport( oGraph, oNodeXLWorkbook, false); // Import the graph into the workbook. // // Note that the GraphMLGraphAdapter stored String arrays on the // IGraph object that specify the names of the attributes that it // added to the graph's edges and vertices. These get used by the // ImportGraph method to determine which columns need to be added // to the edge and vertex worksheets. GraphImporter.ImportGraph(oGraph, ( String[] )oGraph.GetRequiredValue( ReservedMetadataKeys.AllEdgeMetadataKeys, typeof(String[])), ( String[] )oGraph.GetRequiredValue( ReservedMetadataKeys.AllVertexMetadataKeys, typeof(String[])), false, oNodeXLWorkbook); // Store the graph's directedness in the workbook. PerWorkbookSettings oPerWorkbookSettings = new ExcelTemplate.PerWorkbookSettings(oNodeXLWorkbook); oPerWorkbookSettings.GraphDirectedness = oGraph.Directedness; if (sWorkbookSettings != null) { oPerWorkbookSettings.WorkbookSettings = sWorkbookSettings; } Object oGraphDescriptionAsObject; if (!String.IsNullOrEmpty(sGraphMLFilePath)) { // The GraphML came from a file. GraphImporter.UpdateGraphHistoryAfterImport(oNodeXLWorkbook, GraphImporter.GetImportedGraphMLFileDescription( sGraphMLFilePath, oGraph), GraphImporter.GetImportedGraphMLFileTitle(oGraph), null); } else if (oGraph.TryGetValue(ReservedMetadataKeys.GraphDescription, typeof(String), out oGraphDescriptionAsObject)) { // The GraphML came from the NetworkServer program. // // Note that we can't have GraphImporter check the user's // ImportUserSettings object here to determine if the import // description should be saved. Accessing user setting objects // requires access to Globals.ThisWorkbook, which is null when // GraphImporter is called from another process. GraphImporter .UpdateGraphHistoryAfterImportWithoutPermissionCheck( oNodeXLWorkbook, (String)oGraphDescriptionAsObject, null, null, oPerWorkbookSettings); } if (bSetAutomateTasksOnOpen) { // Store an "automate tasks on open" flag in the workbook, // indicating that task automation should be run on it the next // time it's opened. (It is up to the user of this class to // open the workbook to trigger automation.) oPerWorkbookSettings.AutomateTasksOnOpen = true; } } catch (Exception oException) { OnException(oException, ErrorCode.CouldNotImportGraphMLIntoNodeXLWorkbook, "The GraphML couldn't be imported into the NodeXL workbook." ); } if (sNodeXLWorkbookPath == null) { return; } try { ExcelUtil.SaveWorkbookAs(oNodeXLWorkbook, sNodeXLWorkbookPath); } catch (Exception oException) { OnException(oException, ErrorCode.SaveNodeXLWorkbookFileError, "The NodeXL workbook couldn't be saved." ); } try { oNodeXLWorkbook.Close(false, Missing.Value, Missing.Value); } catch (Exception oException) { OnException(oException, ErrorCode.SaveNodeXLWorkbookFileError, "The NodeXL workbook couldn't be closed." ); } }
SaveGraphToNodeXLWorkbook ( XmlDocument oGraphMLDocument, String sGraphMLFilePath, String sNodeXLWorkbookPath, String sNodeXLWorkbookSettingsFilePath, Boolean bSetAutomateTasksOnOpen, Application oExcelApplication ) { Debug.Assert(oGraphMLDocument != null); Debug.Assert(sNodeXLWorkbookPath == null || sNodeXLWorkbookPath.Length > 0); Debug.Assert(oExcelApplication != null); String sWorkbookSettings = null; if (sNodeXLWorkbookSettingsFilePath != null) { try { sWorkbookSettings = GetWorkbookSettings( sNodeXLWorkbookSettingsFilePath); } catch (Exception oException) { OnException(oException, ErrorCode.CouldNotReadWorkbookSettingsFile, String.Format( "The NodeXL options file \"{0}\" couldn't be read." , sNodeXLWorkbookSettingsFilePath ) ); } } // Create a new workbook from the NodeXL template. Workbook oNodeXLWorkbook = null; try { oNodeXLWorkbook = ApplicationUtil.CreateNodeXLWorkbook( oExcelApplication); } catch (IOException oIOException) { throw new ConvertGraphMLToNodeXLWorkbookException( ErrorCode.CouldNotFindNodeXLTemplate, oIOException.Message); } catch (Exception oException) { OnException(oException, ErrorCode.CouldNotCreateNodeXLWorkbook, "A NodeXL workbook couldn't be created." ); } // Create a NodeXL graph from the XML document. IGraph oGraph = ( new GraphMLGraphAdapter() ).LoadGraphFromString( oGraphMLDocument.OuterXml); try { // Turn off text wrap if necessary to speed up the import. GraphImportTextWrapManager.ManageTextWrapBeforeImport( oGraph, oNodeXLWorkbook, false); // Import the graph into the workbook. // // Note that the GraphMLGraphAdapter stored String arrays on the // IGraph object that specify the names of the attributes that it // added to the graph's edges and vertices. These get used by the // ImportGraph method to determine which columns need to be added // to the edge and vertex worksheets. GraphImporter.ImportGraph(oGraph, ( String[] )oGraph.GetRequiredValue( ReservedMetadataKeys.AllEdgeMetadataKeys, typeof( String[] ) ), ( String[] )oGraph.GetRequiredValue( ReservedMetadataKeys.AllVertexMetadataKeys, typeof( String[] ) ), false, oNodeXLWorkbook); // Store the graph's directedness in the workbook. PerWorkbookSettings oPerWorkbookSettings = new ExcelTemplate.PerWorkbookSettings(oNodeXLWorkbook); oPerWorkbookSettings.GraphDirectedness = oGraph.Directedness; if (sWorkbookSettings != null) { oPerWorkbookSettings.WorkbookSettings = sWorkbookSettings; } Object oGraphDescriptionAsObject; if ( !String.IsNullOrEmpty(sGraphMLFilePath) ) { // The GraphML came from a file. GraphImporter.UpdateGraphHistoryAfterImport(oNodeXLWorkbook, GraphImporter.GetImportedGraphMLFileDescription( sGraphMLFilePath, oGraph), null); } else if ( oGraph.TryGetValue(ReservedMetadataKeys.GraphDescription, typeof(String), out oGraphDescriptionAsObject) ) { // The GraphML came from the NetworkServer program. // // Note that we can't have GraphImporter check the user's // ImportUserSettings object here to determine if the import // description should be saved. Accessing user setting objects // requires access to Globals.ThisWorkbook, which is null when // GraphImporter is called from another process. GraphImporter .UpdateGraphHistoryAfterImportWithoutPermissionCheck( oNodeXLWorkbook, (String)oGraphDescriptionAsObject, null, oPerWorkbookSettings); } if (bSetAutomateTasksOnOpen) { // Store an "automate tasks on open" flag in the workbook, // indicating that task automation should be run on it the next // time it's opened. (It is up to the user of this class to // open the workbook to trigger automation.) oPerWorkbookSettings.AutomateTasksOnOpen = true; } } catch (Exception oException) { OnException(oException, ErrorCode.CouldNotImportGraphMLIntoNodeXLWorkbook, "The GraphML couldn't be imported into the NodeXL workbook." ); } if (sNodeXLWorkbookPath == null) { return; } try { ExcelUtil.SaveWorkbookAs(oNodeXLWorkbook, sNodeXLWorkbookPath); } catch (Exception oException) { OnException(oException, ErrorCode.SaveNodeXLWorkbookFileError, "The NodeXL workbook couldn't be saved." ); } try { oNodeXLWorkbook.Close(false, Missing.Value, Missing.Value); } catch (Exception oException) { OnException(oException, ErrorCode.SaveNodeXLWorkbookFileError, "The NodeXL workbook couldn't be closed." ); } }
//************************************************************************* // Method: SaveGraphToNodeXLWorkbook() // /// <summary> /// Saves a graph to a NodeXL Excel workbook given an Excel Application /// object. /// </summary> /// /// <param name="oStartTime"> /// Time at which the network download started. /// </param> /// /// <param name="oXmlDocument"> /// The XML document containing the network as GraphML. /// </param> /// /// <param name="sNetworkConfigurationFilePath"> /// The path of the specified network configuration file. /// </param> /// /// <param name="sNetworkFileFolderPath"> /// The full path to the folder where the network files should be written. /// </param> /// /// <param name="bAutomate"> /// True to automate the NodeXL workbook. /// </param> /// /// <param name="oExcelApplication"> /// An open Excel Application object. /// </param> /// /// <param name="sWorkbookPath"> /// Where the path to the saved workbook gets stored. /// </param> /// /// <remarks> /// If an error occurs, a <see cref="SaveGraphToNodeXLWorkbookException" /> /// is thrown. /// </remarks> //************************************************************************* private static void SaveGraphToNodeXLWorkbook( DateTime oStartTime, XmlDocument oXmlDocument, String sNetworkConfigurationFilePath, String sNetworkFileFolderPath, Boolean bAutomate, Application oExcelApplication, out String sWorkbookPath ) { Debug.Assert(oXmlDocument != null); Debug.Assert( !String.IsNullOrEmpty(sNetworkConfigurationFilePath) ); Debug.Assert( !String.IsNullOrEmpty(sNetworkFileFolderPath) ); Debug.Assert(oExcelApplication != null); // Create a new workbook from the NodeXL template. String sNodeXLTemplatePath; if ( !ExcelTemplate.ApplicationUtil.TryGetTemplatePath( out sNodeXLTemplatePath) ) { throw new SaveGraphToNodeXLWorkbookException( ExitCode.CouldNotFindNodeXLTemplate, String.Format( "The NodeXL Excel template file couldn't be found. It's" + " supposed to be at {0}, but it's not there. Is the NodeXL" + " Excel Template installed? It's required to run this" + " program." , sNodeXLTemplatePath ) ); } Workbook oNodeXLWorkbook = null; try { oNodeXLWorkbook = oExcelApplication.Workbooks.Add( sNodeXLTemplatePath); } catch (Exception oException) { OnException(oException, ExitCode.CouldNotCreateNodeXLWorkbook, "A NodeXL workbook couldn't be created." ); } // Create a NodeXL graph from the XML document. IGraph oGraph = ( new GraphMLGraphAdapter() ).LoadGraphFromString( oXmlDocument.OuterXml); try { // Import the graph into the workbook. // // Note that the GraphMLGraphAdapter stored String arrays on the // IGraph object that specify the names of the attributes that it // added to the graph's edges and vertices. These get used by the // ImportGraph method to determine which columns need to be added // to the edge and vertex worksheets. GraphImporter oGraphImporter = new GraphImporter(); oGraphImporter.ImportGraph(oGraph, ( String[] )oGraph.GetRequiredValue( ReservedMetadataKeys.AllEdgeMetadataKeys, typeof( String[] ) ), ( String[] )oGraph.GetRequiredValue( ReservedMetadataKeys.AllVertexMetadataKeys, typeof( String[] ) ), false, oNodeXLWorkbook); // Store the graph's directedness in the workbook. PerWorkbookSettings oPerWorkbookSettings = new ExcelTemplate.PerWorkbookSettings(oNodeXLWorkbook); oPerWorkbookSettings.GraphDirectedness = oGraph.Directedness; if (bAutomate) { // Store an "automate tasks on open" flag in the workbook, // indicating that task automation should be run on it the next // time it's opened. (It is up to the caller of this method to // open the workbook to trigger automation.) oPerWorkbookSettings.AutomateTasksOnOpen = true; } } catch (Exception oException) { OnException(oException, ExitCode.CouldNotImportGraphIntoNodeXLWorkbook, "The network couldn't be imported into the NodeXL workbook." ); } // Save the workbook. Sample workbook path: // // C:\NetworkConfiguration_2010-06-01_02-00-00.xlsx sWorkbookPath = FileUtil.GetOutputFilePath(oStartTime, sNetworkConfigurationFilePath, sNetworkFileFolderPath, String.Empty, "xlsx"); Console.WriteLine( "Saving the network to the NodeXL workbook \"{0}\"." , sWorkbookPath ); try { oNodeXLWorkbook.SaveAs(sWorkbookPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); } catch (Exception oException) { OnException(oException, ExitCode.SaveNetworkFileError, "The NodeXL workbook couldn't be saved." ); } try { oNodeXLWorkbook.Close(false, Missing.Value, Missing.Value); } catch (Exception oException) { OnException(oException, ExitCode.SaveNetworkFileError, "The NodeXL workbook couldn't be closed." ); } }