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."
);
}
}