Excel 中的 VSTO 拖放
我有一个 VSTO Excel 工作簿。在操作窗格中,有一个 TreeView,我希望能够将其中的项目拖放到 Excel 单元格中。 TreeView 中的数据以内存中 XML 的形式存储。此 XML 转换为单元格值的方式取决于它在工作簿中的放置位置,因此我希望能够将 XML 放置到工作簿中,然后在事件处理程序中解释它,而不是在调用之前进行转换DoDragDrop()。
我发现可以将由制表符和回车符分隔的文本拖放到任务窗格中的单元格上,Excel 会将其转换为单元格表格布局。如上所述,这不是我想要的。
我还可以将 XML 片段转换为文本并将其拖放到工作簿中。然后,在 SheetChange 处理程序中,我可以检查内容是否为 XML,如果是,则进行适当的处理。
但是,如果 XML 包含 然后 Excel 将其搞乱,删除所有 XML 标签,然后在触发 SheetChange 事件之前将文本节点粘贴在一起。它似乎没有检查其他任何东西;字符串
所以这实际上是两个问题:
- 是否有一种方法可以将非文本对象(即 System.Xml.XmlElement 或类似对象)拖放到 Excel 工作簿上并在 VSTO 代码的事件处理程序中捕获该对象?
- 如果没有,有没有办法在 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:
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然这是一个很大的黑客攻击,但您是否尝试过
Microsoft.Office.Interop.Excel.Application.Undo()
Excel 所做的更改,同时保留SheetChange
的事件参数> 事件,为您提供第二次机会进行必要的处理,以使用您自己的自定义方法将数据插入工作簿?我了解到,在破解 Excel API 中的默认行为时,
Undo
方法通常非常有用。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 theSheetChange
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.