I have a VSTO Excel workbook. In the actions pane there is a TreeView and I want to be able to drag开发者_如何学编程-and-drop items from this into Excel cells. The data in the TreeView is stored as in-memory XML. The way in which this XML is converted to cell values depends on where in the workbook it is dropped, so I want to be able to drop the XML onto the workbook and then interpret it in an event handler, rather than doing the conversion before calling DoDragDrop().
I've discovered that it is possible to drop text separated by tabs and carriage returns onto cells from the task pane and Excel converts this into a cell-table layout. As described above, this is not what I want.
I can also convert the XML fragment to text and drop that onto the workbook. Then in the SheetChange handler I can check whether the content is XML and handle it appropriately if it is.
However, if the XML contains a <Table> tag then Excel makes a mess of it, stripping out all the XML tags and just pasting the text nodes together before the SheetChange event is fired. It doesn't seem to check anything else; it doesn't matter where the string <Table> appears, or what other XML is present, or even if the text represents well-formed XML; just that string appearing is enough to trigger some processing in Excel that I can't find a way to circumvent.
So this is really two questions:
- Is there a way to drop non-text objects (ie. System.Xml.XmlElement or similar) onto an Excel workbook and catch the object in an event handler in VSTO code?
- If not, is there a way to catch the pasted text before Excel pre-processes it for me?
Although it's quite a hack, have you tried to Microsoft.Office.Interop.Excel.Application.Undo()
the change made by Excel, while persisting the event arguments of the SheetChange
event, affording you a second opportunity to do the necessary processing to insert your data into the workbook using your own custom approach?
I've read that the Undo
approach is often quite useful when hacking around the default behaviours in Excel API.
精彩评论