//*************************************************************************
        //  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."
                );
            }
        }
Exemple #2
0
        //*************************************************************************
        //  Method: ImportGraph()
        //
        /// <summary>
        /// Imports a graph's edges and vertices into the workbook.
        /// </summary>
        ///
        /// <param name="oGraph">
        /// The graph to import.
        /// </param>
        ///
        /// <param name="oEdgeAttributes">
        /// Array of edge attribute keys that have been added to the metadata of
        /// the graph's vertices.  Can be null.
        /// </param>
        ///
        /// <param name="oVertexAttributes">
        /// Array of vertex attribute keys that have been added to the metadata of
        /// the graph's vertices.  Can be null.
        /// </param>
        //*************************************************************************
        private void ImportGraph(
            IGraph oGraph,
            String [] oEdgeAttributes,
            String [] oVertexAttributes
            )
        {
            Debug.Assert(oGraph != null);
            AssertValid();

            GraphImporter oGraphImporter = new GraphImporter();

            this.ScreenUpdating = false;

            try
            {
            oGraphImporter.ImportGraph(oGraph, oEdgeAttributes,
                oVertexAttributes, this.Ribbon.ClearTablesBeforeImport,
                this.InnerObject);

            this.ScreenUpdating = true;
            }
            catch (Exception oException)
            {
            this.ScreenUpdating = true;
            ErrorUtil.OnException(oException);
            return;
            }

            GraphDirectedness eGraphDirectedness = GraphDirectedness.Undirected;

            switch (oGraph.Directedness)
            {
            case GraphDirectedness.Undirected:

                break;

            case GraphDirectedness.Directed:

                eGraphDirectedness = GraphDirectedness.Directed;
                break;

            case GraphDirectedness.Mixed:

                FormUtil.ShowInformation( String.Format(

                    "The file contains both undirected and directed edges,"
                    + " which {0} does not allow.  All edges are being"
                    + " converted to directed edges."
                    ,
                    FormUtil.ApplicationName
                    ) );

                eGraphDirectedness = GraphDirectedness.Directed;
                break;

            default:

                Debug.Assert(false);
                break;
            }

            this.GraphDirectedness = eGraphDirectedness;
            this.Ribbon.GraphDirectedness = eGraphDirectedness;
        }