将单元格从 Excel 复制到 Word 时如何保持单元格格式

发布于 2024-12-25 20:03:35 字数 317 浏览 4 评论 0原文

目前,我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2025-01-01 20:03:35

我们复制电子表格中表格的范围,然后直接粘贴到保留格式的Word文档中。

wordDoc.Tables.Add(b.Range,newsheet.UsedRange.Rows.Count,newsheet.UsedRange.Columns.Count);
Microsoft.Office.Interop.Word.Table table = b.Range.Tables[1];
newsheet.UsedRange.Copy();
table.Range.Select(); 
wordApp.Selection.Paste();

wordDoc 是Wo​​rd.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.Tables.Add(b.Range,newsheet.UsedRange.Rows.Count,newsheet.UsedRange.Columns.Count);
Microsoft.Office.Interop.Word.Table table = b.Range.Tables[1];
newsheet.UsedRange.Copy();
table.Range.Select(); 
wordApp.Selection.Paste();

wordDoc is Word.Document and wordApp is word.Application. Hope this helps

惯饮孤独 2025-01-01 20:03:35

是的,那会发生。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文