简体   繁体   English

使用PublishObjects.Add从Excel到Word的MS Office Automation传输表

[英]MS Office Automation transfer table from excel to word using PublishObjects.Add

For a project I'm working on, we need to copy data from Excel sheets into new tables within a Word document and have a strategy that works... in most cases. 对于我正在从事的项目,我们需要将数据从Excel工作表复制到Word文档中的新表中,并制定一项适用于大多数情况的策略。

First, we do 首先,我们要做

string file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() +
    ".mht");
object sheetObj = sheetName;
object trueObj = true;
Excel.PublishObject obj = workbook.PublishObjects.Add(Excel.XlSourceType.xlSourceSheet, file,
sheetObj);
obj.Publish(trueObj);

then Document tempDocument = wordApp.Documents.Open(file); 然后文档tempDocument = wordApp.Documents.Open(file); and read templDocument into the targe Word doc. 并将templDocument读入目标Word文档。 ... ...

In a couple of cases, we're seeing problems. 在某些情况下,我们遇到了问题。 (The problems are illustrated in the example files at http://thinkscience.us/office/examples.zip ) 1) the big text files show text truncation between Excel and the exported .mht 2) the 'nutritional' files show the addition of several lines of white space between the Excel data and the .mht. (问题显示在http://thinkscience.us/office/examples.zip的示例文件中)1)大文本文件显示Excel与导出的.mht之间的文本截断2)'营养'文件显示附加内容Excel数据和.mht之间的几行空白。

I've tried several variations on the parameters to PublishObjects.Add (using a range rather than an entire sheet). 我已经尝试了PublishObjects.Add参数的几种变体(使用范围而不是整个工作表)。 The add method includes an optional XlHtmlType parameter that only works with value XlHtmlType.xlHtmlStatic. add方法包括一个可选的XlHtmlType参数,该参数仅与值XlHtmlType.xlHtmlStatic一起使用。

Has anyone used PublishObjects.Add or another strategy to transfer sheets from Excel to Word, preserving as much formatting as possible and not interfering with the system clipboard? 有没有人使用PublishObjects.Add或其他策略将工作表从Excel转移到Word,并保留尽可能多的格式并且不干扰系统剪贴板?

I found an article which might meet your needs, but it's not using Office automation, it's using a library called Spire.Office. 我找到了一篇可能满足您需求的文章,但它没有使用Office自动化,而是使用了一个名为Spire.Office的库。 Check: How to Maintain Formatting of Cells when Copying Cells from Excel to Word . 检查: 将单元格从Excel复制到Word时如何保持单元格格式 在此处输入图片说明

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

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