0

I'm working on an app to parse Excel files. I need to remove invalid characters in column 2 and then in column 3 I need to trim and upper that column.

Any ideas?

Dim sql As String
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        ' xlApp = New Excel.ApplicationClass
        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Open("c:\test13.xlsx")
        xlWorkSheet = xlWorkBook.Worksheets("sheet1")
        sql = "SELECT * FROM [Sheet1$]"

    [B]    'Remove Invalid Characters in Column 2[/B]
        xlWorkSheet.Cells(2:2)  {",", "%", "#", "a", "b", "c"} and Trim(vbUpperCase)

        [B][U]'Remove Invalid Characters in Column 3[/U][/B]
        xlWorkSheet.Cells(3:3) trim(upper)


        xlWorkBook.Close()
        'xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by sandeepparekh9
0

change your code to this :

Dim sql As String
        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet


        xlApp = New Microsoft.Office.Interop.Excel.Application
        xlWorkBook = xlApp.Workbooks.Open(txtExcelFile.Text)
        xlWorkSheet = xlWorkBook.Worksheets("sheet1")
        sql = "SELECT * FROM [Sheet1$]"


        For i As Integer = 1 To xlWorkSheet.Rows.Count - 1

            If xlWorkSheet.Cells(i, 2).value <> Nothing Then


                xlWorkSheet.Cells(i, 2) = xlWorkSheet.Cells(i, 2).value.ToString().Replace("^", "")
                xlWorkSheet.Cells(i, 2) = xlWorkSheet.Cells(i, 2).value.ToString().Replace("`", "")
                xlWorkSheet.Cells(i, 2) = xlWorkSheet.Cells(i, 2).value.ToString().Replace("#", "")
                xlWorkSheet.Cells(i, 2) = xlWorkSheet.Cells(i, 2).value.ToString().Replace("@", "")
                xlWorkSheet.Cells(i, 2) = xlWorkSheet.Cells(i, 2).value.ToString().Replace("&", "")
                xlWorkSheet.Cells(i, 2) = xlWorkSheet.Cells(i, 2).value.ToString().Replace("*", "")
                xlWorkSheet.Cells(i, 2) = xlWorkSheet.Cells(i, 2).value.ToString().Replace("!", "")

                xlWorkSheet.Cells(i, 3) = xlWorkSheet.Cells(i, 3).value.ToString().ToUpper()
            Else
                xlWorkBook.Close()
                xlApp.Quit()
                releaseObject(xlApp)
                releaseObject(xlWorkBook)
                releaseObject(xlWorkSheet)
                Return
            End If
        Next
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.