将数据插入Web浏览器工具中托管的Excel文档中? -VB.net [英] Inserting data into an Excel doc which is being hosted in the web browser tool? - VB.net

查看:161
本文介绍了将数据插入Web浏览器工具中托管的Excel文档中? -VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个USB称重传感器,它将数据(数字)发送到我的主窗体上的文本框.然后,将这些数据导出到Excel 2007的实例,然后将其添加到图表.
这一切都很好.

但是,我想在我的主窗体中托管Excel 2007文档.我设法使用以下方法通过网络浏览器工具打开了Excel文档:

Hello everyone,

I have a usb load cell which sends data (numeric) to a textbox on my mainform. This data is then exported to an instance of Excel 2007 and is then added to a chart.
This all works fine.

However I want to host the Excel 2007 doc in my mainform. I have managed to get the Excel doc to open using the web browser tool using:

WebBrowser1.Navigate("C\filename")


(如果您使用的是Excel 2007,则需要使用MS Fix 50298更改设置,以便文档将在Internet Explorer 7和8中打开)

唯一的事情是我无法将数据导出到在Web浏览器中打开的Excel文档中.

是否有用于访问Web浏览器然后访问Excel文档的特殊参考?还是可以完成?

PS:如果有人希望将文本框中的数据从Mainform导出到excel 2007实例时需要帮助,请随时询问!

谢谢
詹姆斯.


(If you are using Excel 2007 you need to change settings using MS Fix 50298 so doc will open in Internet Explorer 7 & 8)

Only thing is I can''t export my data to the Excel doc I have opened in the web browser.

Is there a special reference to use to access the web browser and then the Excel doc or can it be done?

PS: If anyone would like help with exporting data from textbox in mainform to an instance of excel 2007 please feel free to ask!

Thank you,
James.

推荐答案

大家好,经过一两天的反复试验,我们终于找到了答案!

在使用VB 2010和Excel 2007之前,我忘了提及.

我现在使用Microsoft Web浏览器(axwebbrowser)代替上面使用的Web浏览器.

请查看下面的MS支持链接,以获取详细的说明和示例,对我有帮助:)

http://support.microsoft.com/kb/304643 [ 然后将出现选择工具箱项"窗口.
选择"COM组件"并添加"Microsoft Web浏览器",然后单击确定.现在,它将显示在您的工具箱项目列表中.

将其添加到您的表单.
在我的代码中,我希望ms Web浏览器在主窗体启动时加载excel文档并显示图表.
当我按下按钮时,它将激活我的USB称重传感器,询问我是否要运行"Test1"(选项是/否"),然后继续将数据输入excel,然后输入到图表中.

这是我使用的代码:

标注您的Excel对象的尺寸.
我在我的代码的顶部公开标注了这些尺寸,这样在整个代码中都是常见的.
Hi all, after a day or two a trial and error and searching we finally got it!

Forgot to mention before I am using VB 2010, and of course Excel 2007.

Instead of the web browser used above I now use the Microsoft Web Browser(axwebbrowser).

Check out the MS Support link below for detailed explanation and example, sure helped me:)

http://support.microsoft.com/kb/304643[^]

You can add this by right clicking on the toolbox window and selecting "chose items...".
The "chose toolbox items" windows will then appear.
Select "COM Components" and add "Microsoft Web Browser", OK. It will now appear in your toolbox items list.

Add it to your form.
In my code I want the ms web browser to load the excel doc when the mainform starts and display the chart.
When I press a button it will activate my usb load cell, ask if i want to run "Test1"(Option yes/no) and proceed to input the data into excel and then onto the chart.

This is the code i used:

Dimension your excel objects.
I publicly dimensioned these at the top of my code, this way its common throughout the code.
Imports System.Reflection
Imports Excel = Microsoft.Office.Interop.Excel

Public Class FormMain
Dim oDocumnet As Object
Dim Excel As Excel.Application
Dim oWorkbook As Excel.Workbook
Dim Sheet1 As Excel.Worksheet


下面进入您的主要形式


Below goes into your mainform

Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oDocument = Nothing
        AxWebBrowser1.Navigate("C:\path")

End Sub


导航上述文件后,将运行以下代码


When the above file is navigated the following code is run

Private Sub AxWebBrowser1_NavigateComplete2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles AxWebBrowser1.NavigateComplete2
        'oDoc is used to define the doc which will go into the axwebbrowser
        oDocument = e.pDisp.Document
        'Define oWorkBook as document to be opened in axwebbrowser
        oWorkbook = oDocument.Application.ActiveWorkbook
        oWorkbook.Sheets("Chart1").Select()
 Dim Browser_graph_ready As Boolean
        'Used to define Browser_graph_ready.
        'When webbrowser has finished loading code will continue running(if = true)
        Browser_graph_ready = True

    End Sub


Excel现在可以使用了!
使用按钮启动测试,并显示一个简单的是/否消息框


Excel is now ready to use!
Using a button I start the test, and a simple yes/no messagebox is displayed

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click


'Start Test 1?............
        If MessageBox.Show("Start Test 1? ", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification, False) = _
            Windows.Forms.DialogResult.No Then

            GoTo StopGraph
        End If


For i = 0 To 50
            oWorkbook.Worksheets("Sheet1").Range("A3").Offset(i, 0).Value = txtBoxname.Text
        Next


StopGraph:
   'Insert the action you want here
        
    End Sub
End Class



停止图是一个标签".因此,如果ans为否",则代码将跳至StopGraph在代码中的位置.它必须在同一个子中!

您可以将值更改为任意值,但是在我的情况下,读数进入txtBox,所以这就是我使用它的原因.
请注意:我的程序中有很多代码,摘录了这些摘录,希望对某些人有所帮助.我尚未测试此代码,因此如果您执行复制粘贴工作,我不能保证它会起作用,但它应该为您指明正确的方向.如果您有任何疑问,请询问,我将尽力提供帮助!谢谢,
詹姆斯.



Stop Graph is a ''label''. So if the ans is "No" then the code will skip down to where StopGraph is in your code. It has to be in the same sub though!!

You can change the value to whatever you like, but in my case the readings come into a txtBox so this is why i use it.

Please note: There is a lot more code in my program and these snippets were taken out to hopefully help some of you guys. I have not tested this code so if you do a copy paste job I can''t guarantee it will work but it should point you in the right direction. If you have any questions please ask and i''ll try my best to help! Thanks,
James.


这篇关于将数据插入Web浏览器工具中托管的Excel文档中? -VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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