Link to home
Start Free TrialLog in
Avatar of edfreels
edfreelsFlag for United States of America

asked on

Need help converting text to excel, code inside

Hey Guys,

I have the following code in VB2005.  I am trying to covert a normal text file over to an excel spreadsheet.

 Dim temp As String
        Dim rowNo As Integer
        Dim i As Integer
        Dim filePath As String
        Dim xlRow() As String
        Dim xla As Microsoft.Office.Interop.Excel.Application
        Dim xlw As Microsoft.Office.Interop.Excel.Workbook
        Dim xls As Microsoft.Office.Interop.Excel.Worksheet

        xla = CreateObject("Excel.Application")
        xlw = xla.Workbooks.Add
        xls = xlw.Sheets("Sheet1")
        rowNo = 1
        xla.Visible = False

        filePath = "c:\GiftCards\GiftCardFiles\WILCORPT.txt"

        Open filePath For Input As #1
        While Not EOF(1)
        Line Input #1, temp$
            xlRow = Split(temp, ",", -1, vbTextCompare)
            For i = 0 To UBound(xlRow)
                xls.Cells(rowNo, i + 1) = xlRow(i)
                Debug.Print(xlRow(i))
            Next i%
            rowNo = rowNo + 1
        End While
    Close #1

        xlw.SaveAs("c:\GiftCards\GiftCardFiles\WILCORPT.xls")
        xla.Quit()
        xla = Nothing
        Unload(Me)

The Open and FilePath vb2005 is not likeing and the Unload(Me) it does not like as well.  What can I change that to , to make it compatible with .net

Thanks
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Standard in .NET for File I/O would be the StreamReader class.
Like
        System.IO.StreamReader sr = new System.IO.StreamReader(path);

If you are doing this in a Console App, there is no need to unload.
In a Windows app, closing should be sufficient.
Avatar of edfreels

ASKER

Yes this is a windows app, this is connected to a command button.  So if I use the System.IO.StreamReader above, where do I need to put that in the code and what do I need to replace?
I have something like this, but it still does not seem right

filePath = "c:\GiftCards\GiftCardFiles\WILCORPT.txt"
        System.IO.StreamReader(sr = New System.IO.StreamReader(1))
        While Not EOF(1)
            xlRow = Split(temp, ",", -1, vbTextCompare)
            For i = 0 To UBound(xlRow)
                xls.Cells(rowNo, i + 1) = xlRow(i)
                Debug.Print(xlRow(i))
            Next i%
            rowNo = rowNo + 1
        End While
        Close()
jens?
Ok I have this now, but it says sr is not declared

    Dim temp As String
        Dim rowNo As Integer
        Dim i As Integer
        Dim filePath As String
        Dim xlRow() As String
        Dim xla As Microsoft.Office.Interop.Excel.Application
        Dim xlw As Microsoft.Office.Interop.Excel.Workbook
        Dim xls As Microsoft.Office.Interop.Excel.Worksheet

        xla = CreateObject("Excel.Application")
        xlw = xla.Workbooks.Add
        xls = xlw.Sheets("Sheet1")
        rowNo = 1
        xla.Visible = False

        sr = New System.IO.StreamReader(filePath = "c:\GiftCards\GiftCardFiles\WILCORPT.txt")
        While Not EOF(1)
            xlRow = Split(temp, ",", -1, vbTextCompare)
            For i = 0 To UBound(xlRow)
                xls.Cells(rowNo, i + 1) = xlRow(i)
                Debug.Print(xlRow(i))
            Next i%
            rowNo = rowNo + 1
        End While
        Close()

        xlw.SaveAs("c:\GiftCards\GiftCardFiles\WILCORPT.xls")
        xla.Quit()
        xla = Nothing
        Close()
you need to dim sr as a streamreader

dim sr as system.io.streamreader

somewhere
yea I keep getting bad filename somewhere, not sure where so evidently my code is wrong
Also:
EOF(1) does not apply to the STREAMREADER, since you are not working with #1.

To read a line from the streamreader (right now you are not actually READING anywhere)


So a better outline for the loop would be:
do
    temp = sr.readline()
    if temp is nothing then
          exit do
    end if
' ......your processing.....
loop
You don't want to initialize "path" inside the sr constructor

Try

dim path as string = "c:\GiftCards\GiftCardFiles\WILCORPT.txt"
dim sr as system.io.streamreader = new system.io.streamreader(path)
do
    temp = sr.readline()
    if temp is nothing then
          exit do
    end if
' ......your processing.....
loop
Hmm, I am a bit lost.  Should that be the whole script?
Ok I have this

        Dim rowNo As Integer
        Dim temp As String
        Dim xla As Microsoft.Office.Interop.Excel.Application
        Dim xlw As Microsoft.Office.Interop.Excel.Workbook
        Dim xls As Microsoft.Office.Interop.Excel.Worksheet

        xla = CreateObject("Excel.Application")
        xlw = xla.Workbooks.Add
        xls = xlw.Sheets("Sheet1")
        rowNo = 1
        xla.Visible = False

        Dim path As String = "c:\GiftCards\GiftCardFiles\WILCORPT.txt"
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(path)
        Do
            temp = sr.readline()
            If temp Is Nothing Then
                Exit Do
            End If
            ' ......your processing.....
        Loop


        xlw.SaveAs("c:\GiftCards\GiftCardFiles\WILCORPT.xls")
        xla.Quit()
        xla = Nothing
        Close()

But the xls file is just blank?
I was showing you how to read your file....

The '....your processing.... comment is where you need to put the stuff that does your work!  You know, the
            xlRow = Split(temp, ",", -1, vbTextCompare)
            For i = 0 To UBound(xlRow)
                xls.Cells(rowNo, i + 1) = xlRow(i)
                Debug.Print(xlRow(i))
            Next i%
            rowNo = rowNo + 1
part...
Alright,  Now I have this

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim rowNo As Integer
        Dim temp As String
        Dim xlRow() As String
        Dim i As Integer
        Dim xla As Microsoft.Office.Interop.Excel.Application
        Dim xlw As Microsoft.Office.Interop.Excel.Workbook
        Dim xls As Microsoft.Office.Interop.Excel.Worksheet

        xla = CreateObject("Excel.Application")
        xlw = xla.Workbooks.Add
        xls = xlw.Sheets("Sheet1")
        rowNo = 1
        xla.Visible = False

        Dim path As String = "c:\GiftCards\GiftCardFiles\WILCORPT.txt"
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(path)
        Do
            temp = sr.readline()
            If temp Is Nothing Then
                Exit Do
            End If
            ' ......your processing.....
            xlRow = Split(temp, ",", -1, vbTextCompare)
            For i = 0 To UBound(xlRow)
                xls.Cells(rowNo, i + 1) = xlRow(i)
                Debug.Print(xlRow(i))
            Next i%
            rowNo = rowNo + 1
        Loop
       



        xlw.SaveAs("c:\GiftCards\GiftCardFiles\WILCORPT.xls")
        xla.Quit()
        xla = Nothing
        Close()

And it throws a com exception?  
Ok, lol,

I have this now


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim rowNo As Integer
        Dim temp As String
        Dim xlRow() As String
        Dim i As Integer
        Dim xla As Microsoft.Office.Interop.Excel.Application
        Dim xlw As Microsoft.Office.Interop.Excel.Workbook
        Dim xls As Microsoft.Office.Interop.Excel.Worksheet

        xla = CreateObject("Excel.Application")
        xlw = xla.Workbooks.Add
        xls = xlw.Sheets("Sheet1")
        rowNo = 1
        xla.Visible = False

        Dim path As String = "c:\GiftCards\GiftCardFiles\WILCORPT.txt"
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(path)
        Do
            temp = sr.readline()
            If temp Is Nothing Then
                Exit Do
            End If
            ' ......your processing.....
            xlRow = Split(temp, ",", -1, vbTextCompare)
            For i = 0 To UBound(xlRow)
                xls.Cells(rowNo, i + 1) = xlRow(i)
                Debug.Print(xlRow(i))
            Next i%
            rowNo = rowNo + 1
            xlw.SaveAs("c:\GiftCards\GiftCardFiles\WILCORPT.xls")
            xla.Quit()
            xla = Nothing
            Close()
        Loop
    End Sub

It works but you shut down one excel app when it opens and then it shows up, but it is not formatted right.  Would you have a clue on how to get the formatting?
ASKER CERTIFIED SOLUTION
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial