Example #1
0
        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."
                            );
            }
        }
 public void Awake()
 {
     instance = this;
     Import();
 }