ReadWorkbookInternal ( Microsoft.Office.Interop.Excel.Workbook workbook, ReadWorkbookContext readWorkbookContext ) { Debug.Assert(readWorkbookContext != null); Debug.Assert(workbook != null); AssertValid(); if (readWorkbookContext.PopulateVertexWorksheet) { // Create and use the object that fills in the vertex worksheet. VertexWorksheetPopulator oVertexWorksheetPopulator = new VertexWorksheetPopulator(); try { oVertexWorksheetPopulator.PopulateVertexWorksheet( workbook, false); } catch (WorkbookFormatException) { // Ignore this type of error, which occurs when the vertex // worksheet is missing, for example. } } // Create a graph with the appropriate directedness. PerWorkbookSettings oPerWorkbookSettings = new PerWorkbookSettings(workbook); IGraph oGraph = new Graph(oPerWorkbookSettings.GraphDirectedness); // Read the edge worksheet. This adds data to oGraph, // ReadWorkbookContext.VertexNameDictionary, and // ReadWorkbookContext.EdgeIDDictionary. EdgeWorksheetReader oEdgeWorksheetReader = new EdgeWorksheetReader(); oEdgeWorksheetReader.ReadWorksheet(workbook, readWorkbookContext, oGraph); oEdgeWorksheetReader = null; // Read the vertex worksheet. This adds metadata to the vertices in // oGraph; adds any isolated vertices to oGraph and // ReadWorkbookContext.VertexNameDictionary; and removes any skipped // vertices (and their incident edges) from // ReadWorkbookContext.VertexNameDictionary, // ReadWorkbookContext.EdgeIDDictionary, and oGraph. VertexWorksheetReader oVertexWorksheetReader = new VertexWorksheetReader(); oVertexWorksheetReader.ReadWorksheet(workbook, readWorkbookContext, oGraph); oVertexWorksheetReader = null; if (readWorkbookContext.ReadAllEdgeAndVertexColumns) { // The other worksheets should be ignored. return(oGraph); } if (readWorkbookContext.ReadGroups) { // Read the group worksheets. This adds metadata to the vertices // in oGraph and to oGraph itself. GroupWorksheetReader oGroupWorksheetReader = new GroupWorksheetReader(); oGroupWorksheetReader.ReadWorksheet(workbook, readWorkbookContext, oGraph); oGroupWorksheetReader = null; } // Read the per-workbook settings that are stored directly on the // graph. oPerWorkbookSettings.ReadWorksheet(workbook, readWorkbookContext, oGraph); return(oGraph); }
//************************************************************************* // Method: ReadWorkbookInternal() // /// <summary> /// Creates a NodeXL graph from the contents of an Excel workbook. /// </summary> /// /// <param name="workbook"> /// Workbook containing the graph data. /// </param> /// /// <param name="readWorkbookContext"> /// Provides access to objects needed for converting an Excel workbook to a /// NodeXL graph. /// </param> /// /// <returns> /// A new graph. /// </returns> /// /// <remarks> /// If <paramref name="workbook" /> contains valid graph data, a new <see /// cref="IGraph" /> is created from the workbook contents and returned. /// Otherwise, a <see cref="WorkbookFormatException" /> is thrown. /// </remarks> //************************************************************************* protected IGraph ReadWorkbookInternal( Microsoft.Office.Interop.Excel.Workbook workbook, ReadWorkbookContext readWorkbookContext ) { Debug.Assert(readWorkbookContext != null); Debug.Assert(workbook != null); AssertValid(); if (readWorkbookContext.PopulateVertexWorksheet) { // Create and use the object that fills in the vertex worksheet. VertexWorksheetPopulator oVertexWorksheetPopulator = new VertexWorksheetPopulator(); try { oVertexWorksheetPopulator.PopulateVertexWorksheet( workbook, false); } catch (WorkbookFormatException) { // Ignore this type of error, which occurs when the vertex // worksheet is missing, for example. } } // Create a graph with the appropriate directedness. PerWorkbookSettings oPerWorkbookSettings = new PerWorkbookSettings(workbook); IGraph oGraph = new Graph(oPerWorkbookSettings.GraphDirectedness); // Read the edge worksheet. This adds data to oGraph, // ReadWorkbookContext.VertexNameDictionary, and // ReadWorkbookContext.EdgeIDDictionary. EdgeWorksheetReader oEdgeWorksheetReader = new EdgeWorksheetReader(); oEdgeWorksheetReader.ReadWorksheet(workbook, readWorkbookContext, oGraph); oEdgeWorksheetReader = null; // Read the vertex worksheet. This adds metadata to the vertices in // oGraph; adds any isolated vertices to oGraph and // ReadWorkbookContext.VertexNameDictionary; and removes any skipped // vertices (and their incident edges) from // ReadWorkbookContext.VertexNameDictionary, // ReadWorkbookContext.EdgeIDDictionary, and oGraph. VertexWorksheetReader oVertexWorksheetReader = new VertexWorksheetReader(); oVertexWorksheetReader.ReadWorksheet(workbook, readWorkbookContext, oGraph); oVertexWorksheetReader = null; if (readWorkbookContext.ReadAllEdgeAndVertexColumns) { // The other worksheets should be ignored. return (oGraph); } if (readWorkbookContext.ReadGroups) { // Read the group worksheets. This adds metadata to the vertices // in oGraph and to oGraph itself. GroupWorksheetReader oGroupWorksheetReader = new GroupWorksheetReader(); oGroupWorksheetReader.ReadWorksheet(workbook, readWorkbookContext, oGraph); oGroupWorksheetReader = null; } // Read the per-workbook settings that are stored directly on the // graph. oPerWorkbookSettings.ReadWorksheet(workbook, readWorkbookContext, oGraph); return (oGraph); }
//************************************************************************* // Method: PopulateVertexWorksheet() // /// <summary> /// Populates the vertex worksheet with the name of each unique vertex in /// the edge worksheet. /// </summary> /// /// <param name="activateVertexWorksheetWhenDone"> /// true to activate the vertex worksheet after it is populated. /// </param> /// /// <param name="notifyUserOnError"> /// If true, the user is notified when an error occurs. If false, an /// exception is thrown when an error occurs. /// </param> /// /// <returns> /// true if successful. /// </returns> //************************************************************************* public Boolean PopulateVertexWorksheet( Boolean activateVertexWorksheetWhenDone, Boolean notifyUserOnError ) { AssertValid(); if ( !this.ExcelApplicationIsReady(true) ) { return (false); } // Create and use the object that fills in the vertex worksheet. VertexWorksheetPopulator oVertexWorksheetPopulator = new VertexWorksheetPopulator(); this.ScreenUpdating = false; try { oVertexWorksheetPopulator.PopulateVertexWorksheet( this.InnerObject, activateVertexWorksheetWhenDone); this.ScreenUpdating = true; return (true); } catch (Exception oException) { // Don't let Excel handle unhandled exceptions. this.ScreenUpdating = true; if (notifyUserOnError) { ErrorUtil.OnException(oException); return (false); } else { throw oException; } } }