如何嵌入使用Microsoft.Interop.Word一个字的文档中的Excel文件?

问题描述 投票:2回答:1

我有,我有一个Excel表,我想将它插入到Word文档,使用Microsoft.Interop.Word问题。

我已经尝试过这一个:

var path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent?.FullName + @"\Documents\Businessprocess\Excel-Templates\Tabelle_zum_BP.xlsx";
var oIconLabel = "Tabelle zum BP";
var oMissing = System.Reflection.Missing.Value;
var oIconFileName = oMissing;
var oFileDesignInfo = path;
var oClassType = "Word.Document.8";

_wordDocument.Bookmarks["EPExcelFile"].Range.InlineShapes.AddOLEObject(
        oClassType, oFileDesignInfo, false, true, oIconFileName, oMissing, oIconLabel, oMissing);

我从其他线程本网站上的这一解决方案。唯一的问题是,它现在插入Excel的表格是这样的:

screenshot

我怎样才能插入表,以便它会直接显示,而不是在某个图标? 我需要在Excel工作表中不挂字表,所以,如果我编辑的Excel表事后一个在这个词应该不会受到影响的解决方案。

c# excel ms-word office-interop
1个回答
4
投票

你可以简单地使用Range.PasteExcelTable方法来实现你所需要的:

var path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent?.FullName + @"\Documents\Businessprocess\Excel-Templates\Tabelle_zum_BP.xlsx";
var excel = (Excel.Application)new Excel.ApplicationClass() { Visible = true };
var template = excel.Workbooks.Add(path);
var worksheet = (Excel.Worksheet)template.Worksheets["__SHEET_NAME__"];
var table = (Excel.Range)worksheet.Range["__RANGE__"];

table.Copy();
_wordDocument.Bookmarks["EPExcelFile"].Range.PasteExcelTable(false, true, false);

如果你想知道:

using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
© www.soinside.com 2019 - 2024. All rights reserved.