vb.net 在 Windows 窗体中嵌入 Excel 2010 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13126053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:56:40  来源:igfitidea点击:

Embed Excel 2010 Document in Windows Form

c#.netvb.netexcelinterop

提问by John Bustos

I've been trying to fix this problem the whole day and hope someone here has already figured out how to do this - I'm trying to display an Excel 2010 Worksheet in my Windows Form and did not expect it to be this difficult.

我一整天都在尝试解决这个问题,希望这里有人已经想出了如何做到这一点 - 我试图在我的 Windows 窗体中显示一个 Excel 2010 工作表,但没想到它会这么困难。

So... From what I've seen online, the best way to do this is via a WebBrowser control - My code for this is SUPER easy:

所以......从我在网上看到的,最好的方法是通过 WebBrowser 控件 - 我的代码非常简单:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim strfilename = Application.StartupPath & "\SheetXX.xlsx"
    WebBrowser1.Navigate(strfilename, False)

End Sub

But it pops up a File Download dialog. At first, my problem was that even when I pushed the "Open" button, it opened Excel in a separate window. This I managed to fix using this registry fix, but it still pops up the dialog before opening it in my webbrowser which my end-users will complain about.

但它会弹出一个文件下载对话框。起初,我的问题是,即使我按下“打开”按钮,它也会在单独的窗口中打开 Excel。我设法使用这个注册表修复来修复这个问题,但它仍然会在我的网络浏览器中打开它之前弹出对话框,我的最终用户会抱怨。

Other solutions online mentioned the Microsoft dsoframer, but that didn't work for me at all either...

其他在线解决方案提到了 Microsoft dsoframer,但这对我来说根本不起作用......

Anyone know how to make this work?? Plain and simply, I just want to see the Excel sheet in my windows form.

有谁知道如何使这项工作?简单明了,我只想在我的 Windows 窗体中查看 Excel 工作表。

Thanks!!

谢谢!!

采纳答案by John Bustos

For anyone else that got stuck with a similar issue, the best solution I found to do what I wanted was this one. Hope it helps others too!!

对于遇到类似问题的任何其他人,我找到的最好的解决方案就是这个。希望它也能帮助其他人!!

回答by BrOSs

I figured out this workaroundsolution:

我想出了这个解决方法:

First, you have to save your Excel file as MHT format. Then, just add up a iFrame:

首先,您必须将 Excel 文件保存为 MHT 格式。然后,只需添加一个 iFrame:

<p style="text-align:center">

    <iframe id="ExcelID" src="Reports/ExcelFile.mht?wmode=transparent" 
     width="100%" height="800" wmode="transparent" style="z-index: -1"></iframe>
</p>

wmode="transparent" style="z-index: -1is because sometimes the menuitem"hides" behind the iFrame's content. I don't know if you have one but you can just delete it.

wmode="transparent" style="z-index: -1是因为有时菜单项“隐藏”在 iFrame 的内容后面。我不知道你有没有,但你可以删除它。

Simple way:

简单的方法:

 <iframe src="Reports/ExcelFile.mht" width="100%" height="800"></iframe>

Regards

问候