如何使用Microsoft.Interop.WordembeddedWord文档中的Excel文件?

我有一个问题,我有一个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); 

我从这个网站上的其他线索得到了这个解决scheme。 唯一的问题是,它现在插入Excel表:

图片

我怎样才能插入表,使其直接显示,而不是通过图标?
我需要一个Excel表格没有链接到Word表格的解决scheme,所以如果我以后编辑Excel表格,这个词不应该受到影响。

你可以简单地使用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;