2017-03-09 87 views
0

我有一个问题,我有一个Excel表格,我想使用Microsoft.Interop.Word将它插入到Word文档中。如何使用Microsoft.Interop.Word在Excel文档中嵌入Excel文件?

我已经尝试过这一个:

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的表格是这样的:

Image

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

+0

据我所知,如果你想插入Excel表格中没有提及它,你需要像对象一样添加它;)。 –

回答

2

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

Range.PastExcelTable如何知道我要插入的Excel表格? – Snaylor

+0

您可以预先指定要插入的“表格”。 – Xiaoy312

+0

'var table =(Excel.Range)worksheet.Range [“__ RANGE __”];' 我应该在这里插入“范围”? 对不起,我提出了一些愚蠢的问题,但我仍然在努力... – Snaylor