简体   繁体   中英

VSTO drag-and-drop in excel

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:

  1. 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?
  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM