How do I add a custom XML to an open Excel 2007 workbook using C#? How do I add a custom XML to an open Excel 2007 workbook using C#? xml xml

How do I add a custom XML to an open Excel 2007 workbook using C#?


The MSDN Docs give an example using VSTO and I've adapted this to work with a Winform application.

The trick (in this situation) is to reference the PIA see the tooltip path of the Excel Reference in the screenshot below. NOTE: I didn't use the .Net or COM reference tabs, I had to "Browse" for the Excel DLL.


Below is the Winform Code of a working example using the Excel PIA (also version 12.0.4518.1014). See screenshot for more detailed info of the Book1.xlsx renamed to a zip and extracted after I ran the code, along with the resulting item1.xml file in the CustomXML folder:

private void button1_Click(object sender, EventArgs e){    string path = @"c:\temp\test\Book1.xlsx";    var xlApp = new Microsoft.Office.Interop.Excel.Application();    Workbook wb = xlApp.Workbooks.Open(path);    string xmlString =    "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +    "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +        "<employee>" +            "<name>Karina Leal</name>" +            "<hireDate>1999-04-01</hireDate>" +            "<title>Manager</title>" +        "</employee>" +    "</employees>";    wb.CustomXMLParts.Add(xmlString, Type.Missing);    wb.Save();}

Large view of screenshot: http://i.stack.imgur.com/O8Qhm.png

enter image description here

If you want to fetch customXML from a Workbook see this answer: https://stackoverflow.com/a/8488072/495455.


EDIT:

I also have Microsoft.Office.Core ActiveX referenced from the GAC: C:\Windows\assembly\GAC_MSIL\Office\15.0.0.0__71e9bce111e9429c\Office.dll and Microsoft VBIDE.Interop from the GAC as well C:\Windows\assembly\GAC_MSIL\Microsoft.Vbe.Interop\15.0.0.0__71e9bce111e9429c\Microsoft.Vbe.Interop.dll.

Here is my project, please try it out and hopefully you can see whats missing from your solution vs my one that works: http://JeremyThompson.Net/Rocks/OfficeExcelCustomXML.zip

Please note the zip contains the GAC DLLs in the Bin\Debug folder.


I think it would be even more suitable for you to completly abandon the way via excel automation. Probably it would be enough to open the excel file via System.IO.Packaging Namespace. Here you can find a complete sample: Add Custom XML Parts to Documents Without Starting Microsoft Office

In contrast to the other posted answers this solution does not need any references to the Office PIAs. You just need to add a reference to WindowsBase, which is included in .NET. It can even handle other OpenXML document formats like docx, pptx...


In you second snippet, Microsoft.Office.Interop.Excel.Workbook.CustomXMLParts is a type, you can not assign it to variable myCustomXMLParts. You would have to have

Microsoft.Office.Interop.Excel.Workbook myWorkbook = <some appropriate constructor>;...Microsoft.Office.Interop.Excel.Workbook.CustomXMLParts myParts = myWorkbook.CustomXMLParts;