控制Excel中在WebBrowser控件 [英] Control Excel Within WebBrowser Control

查看:901
本文介绍了控制Excel中在WebBrowser控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载一个.xls文件到在我的Windows中的WebBrowser控件窗体应用程序中,我能跟上数据的编程方式更新它的方式。

因此​​,举例来说,当用户presses窗体上的按钮,在Excel工作表的更新数据。

我已成功非常容易地将数据加载到控制:

  WebBrowser1.Navigate时(PathToXLSFile)
 

和,据我所在线阅读,我应该能够使用现在的线沿线的东西拿到表的控制:

 昏暗WB作为对象
WebBrowser1.Navigate时(PathToXLSFile)
WB = WebBrowser1.Document
 

现在,我试图把那的code最后一行到两个 WebBrowser1_DocumentCompleted 的的 WebBrowser1_Navigated 模块,但对于这两个,我得到的 WebBrowser1.Document =什么

我在网上看了一下,发现了解决方案,如的这个和<一href="http://www.vbforums.com/showthread.php?455851-RESOLVED-Excel-in-Web-Browser-Control-Select-sheet-problem"相对=nofollow>这个,但他们都不是为我工作。

再次就是我希望做的是(如果你有更好的想法或其他控件)来加载Excel工作表到WebBrowser控件,并能够编辑/通过.NET进行更改。


至于我完全方案(为什么我需要这个):

基本上,我的程序生成数据的大量最终用户希望看到一种特殊的方式(通常是不断变化的)格式,所以我所做的就是创建一个格式化的excel表与公式引用回表的不同的Excel工作表。 这样一来,我所有的程序需要做的就是吐出所有的计算值表,然后将数据是那里的用户看到(并且是非常容易定制不会弄乱我的程序)。

目前的挑战是,现在他们希望看到一个窗口的形式对这个数据的重复的许多的,所以,基本上是我需要做的是他们希望看到新的数据每一次,计算它的飞行和再展示给他们。

我想通了,DataGridView中是非常困难的在这里使用,因为复杂的格式,所以,我希望做的是让他们的输出工作表(通过一个WebBrowser控件,因为这是所有我能找到),并必须不断更新的输入片用新数据,使得输出片保持更新的能力。

解决方案

只是为了完整地回答这个问题,所以任何人谁面临的这一挑战在未来将不会有敲自己的头撞墙像我一样,将有一些引导他们,这里就是我最后做:

  1. 在后台打开我的Excel工作簿中一次使用Office.Interop - 这让我来操纵数据,并使用Excel的计算引擎,以极快的速度更新数据
  2. 添加到我的形式的图片框在面板内部,设置面板的自动滚屏= TRUE

code,用于显示输出到Excel:

 昏暗MyRange作为Microsoft.Office.Interop.Excel.Range

... code ...

MyRange.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen,Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap)

如果Clipboard.GetDataObject状态并没有任何然后
   昏暗的数据作为IDataObject的= Clipboard.GetDataObject

   如果Data.GetData present(DataFormats.Bitmap)然后
       昏暗的IMG作为图像= Data.GetData(DataFormats.Bitmap,真)

       PictureBox1.Height = img.Height
       PictureBox1.Width = img.Width
       PictureBox1.Image = IMG
   结束如果
结束如果
 

我会definietly喜欢的状态,虽然,这个答案是100%的感谢蒂姆·威廉姆斯他惊人的方向和意见 - !谢谢

I am trying to load a .xls file into a webbrowser control in my Windows Forms app in such a way that I can keep updating it with data programmatically.

So, for example, as the user presses a button on the form, the data in the Excel sheet updates.

I have managed to load the data into the control very easily:

WebBrowser1.Navigate(PathToXLSFile)

And, according to what I have read online, I should be able to now get control of that sheet using something along the lines of:

Dim wb As Object
WebBrowser1.Navigate(PathToXLSFile)
wb = WebBrowser1.Document

Now, I have tried putting that final line of code into BOTH the WebBrowser1_DocumentCompleted and the WebBrowser1_Navigated modules, but for both, I get that WebBrowser1.Document = Nothing.

I have looked online and found the solutions such as this, this and this, but none of them are working for me.

Again, what I'm looking to do is to load the Excel sheet into the webbrowser control (or another control if you have better ideas) and to be able to edit/change it via .NET.


As for my full scenario (why I need this):

Basically, my program generates a enormous amount of data that end-users want to see formatted in a particular way (often changing), so what I have done is create a formatted excel sheet with formulas referencing back to a table on a different excel sheet. That way, all my program needs to do is spit out all the calculated values to the table and then the data is there for the users to see (and is very easily customizable without messing up my program).

The challenge is that now they want to see lots of iterations of this data on a windows form, so, basically what I need to do is each time they want to see the new data, calculate it on the fly and re-show it to them.

I figured out the DataGridView was very difficult to use here because of the complex formatting, so what I'm looking to do is show them the "Output" sheet (via a webBrowser control because that's all that I could find) and have the ability to keep updating the "Input" sheet with new data such that the output sheet keeps updating.

解决方案

Just to fully answer this question so anyone who faces this challenge in the future won't have to knock their head against a brick wall like I did and will have something to guide them, here's what I ended up doing:

  1. Opened my Excel workbook once in the background using Office.Interop - This allowed me to manipulate the data and use Excel's calculation engine to update the data extremely quickly.
  2. Added to my form a PictureBox inside a Panel and set the Panel's autoscroll = True

Code used to display Excel output:

Dim MyRange As Microsoft.Office.Interop.Excel.Range

... code ...

MyRange.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen,Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap)

If Clipboard.GetDataObject IsNot Nothing Then
   Dim Data As IDataObject = Clipboard.GetDataObject

   If Data.GetDataPresent(DataFormats.Bitmap) Then
       Dim img As Image = Data.GetData(DataFormats.Bitmap, True)

       PictureBox1.Height = img.Height
       PictureBox1.Width = img.Width
       PictureBox1.Image = img
   End If
End If

I would definietly like to state, though, that this answer is 100% thanks to Tim Williams for his AMAZING direction and comments - Thanks!

这篇关于控制Excel中在WebBrowser控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆