带W7的VS2008和Excel [英] VS2008 and Excel w/W7

查看:97
本文介绍了带W7的VS2008和Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用VB .Net开发的应用程序,可以将数据从gridview导出到excel,但是,在Windows XP上运行良好,并且位于该应用程序之上.但是在Windows 7和Server 2008上,曾经是Excel报告已生成,它位于应用程序的后面.它在任务栏中可见,但是它必须位于应用程序的顶部.任何想法就是为什么它落后于应用程序.在Windows 7上?

我正在使用的函数创建对象并请求所需的excel模板如下:------------------------------- ------------------------------------------------

I have an app that was developed in VB .Net and exports data from the gridview to excel, However, on Windows XP works fine and is on top of the App., but on Windows 7 and Server 2008, once the Excel report is generated it goes behind the app. It is visible in the task bar, but it needs to be on top of the App. Any idea''s why it is going behind the App. on Windows 7?

The function that I am using create the objects and request the required excel template is the following: -------------------------------------------------------------------------------

Sub ExcelReports(ByRef bVersion As Boolean)
'Sets up all needed objects to manage the Borelog report in Excel

xl = New Excel.Application
Try
    'check to make sure the correct version is installed
    If xl.Application.Version >= 11 Then bVersion = True
        wkb = xl.Workbooks.Open(Application.StartupPath & ("\Reports\BoreLog.xls"))
        wksTarget1 = wkb.Sheets(1)
        wksTarget2 = wkb.Sheets(2)
        xl.Visible = True
    Else
        'if the wrong version is installed or nothing is installed, write the error to the log
        bVersion = False
        'MUST BE FALSE TO WORK PROPERLY
        Cmn.DebugLog("MS Office is missing or the incorrect version is installed")
    End If

Catch ex As Exception
    MsgBox("Error in ExcelRpt Class, ExcelReports() - ", vbCritical, "Report Error")
    Cmn.DebugLog("Error in ExcelRpt Class, ExcelReports() - " & ex.ToString)
End Try
End Sub

推荐答案

您可能会为自己付出更多的努力.我一直在使用一种简单得多的方法来打开空白工作簿:

You might be making more effort for yourself than you need. I have been using a much simpler way of opening a blank workbook:

var xlApp = new Excel.Application();
var xlWorkBook = xlApp.Workbooks.Add(strTemplateLocaton);



请注意,我没有使用模板,而是在Workbooks.Add方法中添加了模板.如果不需要模板,则删除参数.



Note that I have not been using a template, and I added the template in the Workbooks.Add method. If you do not need the template, than delete the argument.


因为我已经有了excel(xl)的对象,所以只需添加一行即可.

AppActivate(xl.Caption)

出于某种原因,Vista和Windows 7的上述代码存在相同的问题,但这已经解决了,现在可以通过Win 7在XP上运行.

感谢您的帮助和建议.
Since I already had the object for excel (xl) all that was needed was to add one line.

AppActivate(xl.Caption)

For some reason Vista and Windows 7 have the same problem with the above code, but this took care of it and now it works from XP thru Win 7.

Thanks for the help and suggestions.


这篇关于带W7的VS2008和Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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