0

当我将文件导入我的程序时,我遇到了从 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

任何人都知道我可以如何禁用这个?

4

1 回答 1

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

于 2013-04-05T18:46:09.873 回答