简体   繁体   English

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

[英]How to maintain formatting of cells when copying cells from Excel to Word

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. 目前,我正在使用OleDB与Excel Spreadsheet连接并在DataTable获取数据,对DataTable进行一些修改,然后将数据复制到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. 我正在使用Interop库通过Excel与word和OLEDB进行通信。

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. (宏?)我尝试使用Interop.Excel.Styles但是我不知道如何将其与当前使用的单元格联系起来。

We copy the range of the table in the spreadsheet and the directly paste in the word document which preserves formatting. 我们将表格的范围复制到电子表格中,然后直接粘贴到保留格式的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 is Word.Document and wordApp is word.Application. wordDoc是Word.Document,而wordApp是word.Application。 Hope this helps 希望这可以帮助

Yeah, that's gonna happen. 是的,那将会发生。 OleDB moves data, not formatting information. OleDB移动数据,而不格式化信息。 If you want the formatting, you're going to have to copy/paste from Excel to Word. 如果要设置格式,则必须从Excel复制/粘贴到Word。 If you need to automate the process, VBA is the easiest way to control Excel and Word from the outside. 如果您需要自动化该过程,则VBA是从外部控制Excel和Word的最简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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