将单元格从 Excel 复制到 Word 时如何保持单元格格式
目前,我正在使用OleDB
连接Excel电子表格并获取DataTable
中的数据,对数据进行一些修改,然后将数据复制到Word。
这样做时,我会丢失单元格的格式,例如文本的任何部分是否为彩色或背景颜色是否为灰色或是否为粗体。
我正在使用 Interop 库与 Word 和 OLEDB
与 Excel 进行通信。
如果这个解决方案不足以满足我需要实现的目标,您能建议替代解决方案吗? (宏?) 我尝试使用 Interop.Excel.Styles 但我不知道如何将它与当前使用的单元格关联起来。
Currently I'm using OleDB
to connect with Excel Spreadsheet and getting data in a DataTable
, doing some modifications on data and then copying the data to Word.
In doing so I'm losing the formatting of the cell, like if any part of text was colored or if the background color was grayed or if it's bold.
I'm using Interop library to communicate with word and OLEDB
with Excel.
If this solution is not good enough for what I need to achieve , can you suggest alternative solutions? (Macros?)
I tried using Interop.Excel.Styles
but I can't figure out how to relate it with the cell being currently used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们复制电子表格中表格的范围,然后直接粘贴到保留格式的Word文档中。
wordDoc 是Word.Document,wordApp 是word.Application。希望这有帮助
We copy the range of the table in the spreadsheet and the directly paste in the word document which preserves formatting.
wordDoc is Word.Document and wordApp is word.Application. Hope this helps
是的,那会发生。 OleDB 移动数据,而不是格式化信息。如果您想要格式设置,则必须从 Excel 复制/粘贴到 Word。如果您需要自动化该过程,VBA 是从外部控制 Excel 和 Word 的最简单方法。
Yeah, that's gonna happen. OleDB moves data, not formatting information. If you want the formatting, you're going to have to copy/paste from Excel to Word. If you need to automate the process, VBA is the easiest way to control Excel and Word from the outside.