在VB代码中阻止Excel弹出消息

时间:2013-04-05 18:37:56

标签: vb.net excel

我在将文件导入程序时从excel弹出“立即可用文件”消息框时出现问题。

http://oi50.tinypic.com/23wajt.jpg

Private Function XLSSelect_Click(strFileName As String) As DataTable

    Dim connectionStringTemplate As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0};" + "Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"""
    Dim connectionString As String = String.Format(connectionStringTemplate, strFileName)

    Try
        Dim sqlSelect As String = "SELECT * FROM [" & GetExcelSheetNames(strFileName)(0) & "];"
        ' Load the Excel worksheet into a DataTable
        Dim workbook As DataSet = New DataSet()
        Dim excelAdapter As System.Data.Common.DataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlSelect, connectionString)
        excelAdapter.Fill(workbook)

        For i As Integer = 0 To workbook.Tables(0).Columns.Count - 1
            workbook.Tables(0).Columns(i).ColumnName = workbook.Tables(0).Columns(i).ColumnName.ToString.Trim.Replace(" ", "")
        Next

        Return workbook.Tables(0).Copy
    Catch
        Throw New Exception("Error reading from Excel Spreadsheet. Are you sure this file isn't currently open in Excel, and that it has been saved as a .xls through Excel at least once?")
    End Try
    Return Nothing
End Function

Private Shared Function GetExcelSheetNames(excelFile As String) As [String]()
    Dim objConn As OleDbConnection = Nothing
    Dim dt As System.Data.DataTable = Nothing

    Try
        Dim connString As [String] = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & excelFile & ";Extended Properties=Excel 12.0;"
        ' Create connection object by using the preceding connection string.
        objConn = New OleDbConnection(connString)

        ' Open connection with the database.
        objConn.Open()
        ' Get the data table containg the schema guid.
        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)

        If dt Is Nothing Then
            Return Nothing
        End If

        Dim excelSheets As [String]() = New [String](dt.Rows.Count - 1) {}
        Dim i As Integer = 0

        'Add the sheet name to the string array.
        For Each row As DataRow In dt.Rows
            excelSheets(i) = row("TABLE_NAME").ToString()
            i += 1
        Next

        Return excelSheets
    Catch ex As Exception
        Return Nothing
    Finally
        ' Clean up.
        If objConn IsNot Nothing Then
            objConn.Close()
            objConn.Dispose()
        End If
        If dt IsNot Nothing Then
            dt.Dispose()
        End If
    End Try
End Function

任何人都知道如何禁用此功能?

1 个答案:

答案 0 :(得分:1)

Dim appExcel As Excel.Application

Set appExcel = New Excel.Application
appExcel.Workbooks.Open "e:\development\test.xls", 0

'  Do your thing here...

appExcel.DisplayAlerts = False  '  Surpress save dialog box.
appExcel.Quit  '  Quit without saving. you can change as you want
Set appExcel = Nothing

代码

Try
        Dim connString As [String] = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & excelFile & ";Extended Properties=Excel 12.0;"
        ' Create connection object by using the preceding connection string.
        objConn = New OleDbConnection(connString)

        ' Open connection with the database.
        objConn.Open()

appExcel.DisplayAlerts = False'试试这里!

        ' Get the data table containg the schema guid.
        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)

资料来源:http://www.xtremevbtalk.com/showthread.php?t=16007

相关问题