Hi!
I just have this program that validates if the selected file is an excel file, and I want this to have a separate function for validation so I can use that function to other program, can you please give me an example on how to do this function..

Thanks

Why cant you force the user to select only excel file? using fileopen Dialogue?
are you opening the file through program not using fileopen Dialogue?

Why cant you force the user to select only excel file? using fileopen Dialogue?
are you opening the file through program not using fileopen Dialogue?

Yep, I am using fileUpload

If ur opening through program then you can try something like this

Private Function isFileExcel(ByVal filename As String) As Boolean
        Try
            Dim fileExtenstion As String = String.Empty
            fileExtenstion = filename.Substring(filename.IndexOf(".", 1))
            Select Case fileExtenstion
                Case ".xlt"
                    Return True
                Case ".xls"
                    Return True
                Case Else
                    Return False

            End Select
        Catch ex As Exception
            Msgbox(ex.Tostring())
            Return false
        End Try
You need to include all the formats of excel in case statement.

Yep, I am using fileUpload

Is it web application? Can you please post your code here what you have so far?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With New OpenFileDialog
            .Filter = "Only myCool Excel Files|*.xls;*.xlst"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                MsgBox("tada")
            End If
        End With
    End Sub

Pgmer, why Not use IO.Path.GetExtension("full File.Path or just File.Name here") ?

CodeOrder, Yes we can use that. Thanks :)

Is it web application? Can you please post your code here what you have so far?

here is my code:

Dim fileName As String
        Dim fileExtension As String
        Dim fileServer As String

            fileName = FileUpload1.PostedFile.FileName
            fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName)
            fileServer = FileUpload1.FileName
If FileUpload1.HasFile = True Then

' some code here

If fileExtension.Equals(".xls") = True Then
'some code here

else
'some code here
end if

else if FileUpload1.hasFile = false then

'some code here


end if

here it is.. I want this to be a clean code, rather than this one, and I want to know how can this be converted to n-layer.. thanks

If fileExtension.Equals(".xls") = True Then'some code here else'some code hereend if

what is the problem with this code? you want to make genreal function to check or validate .xls?

Pgmer, something else other than glad I could help.
.You can use more than one "something" in a Select Case(line.2).

Select Case fileExtenstion
                Case ".xlt",".xls"
                    Return True                
                Case Else
                    Return False
            End Select

jbutardo, I already flagged your thread as an ASP.NET thread, though I will try to help once you provide more defined details as to exactly "how" to make it more of a "clean code" other than having one too many declarations.

If fileExtension.Equals(".xls") = True Then'some code here else'some code hereend if

what is the problem with this code? you want to make genreal function to check or validate .xls?

yes. i want this to become a general function, can you help me on how to do it? thanks

See if this helps.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fileName As String = FileUpload1.PostedFile.FileName
        Dim fileServer As String = FileUpload1.FileName

        If FileUpload1.HasFile Then
            ' some code here
            '//////////////////////////////////////////////////
            If isCoolExcelFile(fileName) Then
                '//////////////////////////////////////////////////
                'some code here
            Else
                'some code here
            End If
        Else '// If FileUpload1.Has "NO" File
            'some code here
        End If
    End Sub

    Private Function isCoolExcelFile(ByVal selCoolFile As String) As Boolean
        Select Case IO.Path.GetExtension(selCoolFile)
            Case ".xls"
                Return True
            Case Else
                Return False
        End Select
    End Function

See if this helps.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fileName As String = FileUpload1.PostedFile.FileName
        Dim fileServer As String = FileUpload1.FileName

        If FileUpload1.HasFile Then
            ' some code here
            '//////////////////////////////////////////////////
            If isCoolExcelFile(fileName) Then
                '//////////////////////////////////////////////////
                'some code here
            Else
                'some code here
            End If
        Else '// If FileUpload1.Has "NO" File
            'some code here
        End If
    End Sub

    Private Function isCoolExcelFile(ByVal selCoolFile As String) As Boolean
        Select Case IO.Path.GetExtension(selCoolFile)
            Case ".xls"
                Return True
            Case Else
                Return False
        End Select
    End Function

Okay, thanks for the help..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.