Created
November 29, 2012 14:24
-
-
Save johnathan-sewell/4169425 to your computer and use it in GitHub Desktop.
Example of saving XML to the Excel document file from a VSTO Excel Ribbon Addin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void AddCustomXmlPartToWorkbook() | |
{ | |
var workbook = Globals.ThisAddIn.Application.ActiveWorkbook; | |
const string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + | |
"<employees xmlns=\"http://myapi.com\">" + | |
"<employee>" + | |
"<name>Karina Leal</name>" + | |
"<hireDate>1999-04-01</hireDate>" + | |
"<title>Manager</title>" + | |
"</employee>" + | |
"</employees>"; | |
Microsoft.Office.Core.CustomXMLPart employeeXMLPart = workbook.CustomXMLParts.Add(xmlString, System.Type.Missing); | |
} | |
public string RetrieveCustomXmlPartFromWorkbook() | |
{ | |
var workbook = Globals.ThisAddIn.Application.ActiveWorkbook; | |
var retrievedXMLParts = workbook.CustomXMLParts.SelectByNamespace("http://myapi.com"); | |
var customXMLPart = retrievedXMLParts.Cast<CustomXMLPart>().FirstOrDefault() ; | |
return customXMLPart != null ? customXMLPart.XML : String.Empty; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/13567921/how-can-i-persist-data-for-an-excel-ribbon-addin-in-excel