edfreels
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.E xcel.Appli cation
Dim xlw As Microsoft.Office.Interop.E xcel.Workb ook
Dim xls As Microsoft.Office.Interop.E xcel.Works heet
xla = CreateObject("Excel.Applic ation")
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
filePath = "c:\GiftCards\GiftCardFile s\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\G iftCardFil es\WILCORP T.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
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.E
Dim xlw As Microsoft.Office.Interop.E
Dim xls As Microsoft.Office.Interop.E
xla = CreateObject("Excel.Applic
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
filePath = "c:\GiftCards\GiftCardFile
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\G
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
Standard in .NET for File I/O would be the StreamReader class.
Like
System.IO.StreamReader sr = new System.IO.StreamReader(pat h);
If you are doing this in a Console App, there is no need to unload.
In a Windows app, closing should be sufficient.
System.IO.StreamReader sr = new System.IO.StreamReader(pat
If you are doing this in a Console App, there is no need to unload.
In a Windows app, closing should be sufficient.
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?
ASKER
I have something like this, but it still does not seem right
filePath = "c:\GiftCards\GiftCardFile s\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()
filePath = "c:\GiftCards\GiftCardFile
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()
ASKER
jens?
ASKER
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.E xcel.Appli cation
Dim xlw As Microsoft.Office.Interop.E xcel.Workb ook
Dim xls As Microsoft.Office.Interop.E xcel.Works heet
xla = CreateObject("Excel.Applic ation")
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
sr = New System.IO.StreamReader(fil ePath = "c:\GiftCards\GiftCardFile s\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\G iftCardFil es\WILCORP T.xls")
xla.Quit()
xla = Nothing
Close()
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.E
Dim xlw As Microsoft.Office.Interop.E
Dim xls As Microsoft.Office.Interop.E
xla = CreateObject("Excel.Applic
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
sr = New System.IO.StreamReader(fil
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\G
xla.Quit()
xla = Nothing
Close()
you need to dim sr as a streamreader
dim sr as system.io.streamreader
somewhere
dim sr as system.io.streamreader
somewhere
ASKER
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
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\GiftCardFile s\WILCORPT .txt"
dim sr as system.io.streamreader = new system.io.streamreader(pat h)
do
temp = sr.readline()
if temp is nothing then
exit do
end if
' ......your processing.....
loop
Try
dim path as string = "c:\GiftCards\GiftCardFile
dim sr as system.io.streamreader = new system.io.streamreader(pat
do
temp = sr.readline()
if temp is nothing then
exit do
end if
' ......your processing.....
loop
ASKER
Hmm, I am a bit lost. Should that be the whole script?
ASKER
Ok I have this
Dim rowNo As Integer
Dim temp As String
Dim xla As Microsoft.Office.Interop.E xcel.Appli cation
Dim xlw As Microsoft.Office.Interop.E xcel.Workb ook
Dim xls As Microsoft.Office.Interop.E xcel.Works heet
xla = CreateObject("Excel.Applic ation")
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
Dim path As String = "c:\GiftCards\GiftCardFile s\WILCORPT .txt"
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(pat h)
Do
temp = sr.readline()
If temp Is Nothing Then
Exit Do
End If
' ......your processing.....
Loop
xlw.SaveAs("c:\GiftCards\G iftCardFil es\WILCORP T.xls")
xla.Quit()
xla = Nothing
Close()
But the xls file is just blank?
Dim rowNo As Integer
Dim temp As String
Dim xla As Microsoft.Office.Interop.E
Dim xlw As Microsoft.Office.Interop.E
Dim xls As Microsoft.Office.Interop.E
xla = CreateObject("Excel.Applic
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
Dim path As String = "c:\GiftCards\GiftCardFile
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(pat
Do
temp = sr.readline()
If temp Is Nothing Then
Exit Do
End If
' ......your processing.....
Loop
xlw.SaveAs("c:\GiftCards\G
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...
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...
ASKER
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.E xcel.Appli cation
Dim xlw As Microsoft.Office.Interop.E xcel.Workb ook
Dim xls As Microsoft.Office.Interop.E xcel.Works heet
xla = CreateObject("Excel.Applic ation")
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
Dim path As String = "c:\GiftCards\GiftCardFile s\WILCORPT .txt"
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(pat h)
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\G iftCardFil es\WILCORP T.xls")
xla.Quit()
xla = Nothing
Close()
And it throws a com exception?
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.E
Dim xlw As Microsoft.Office.Interop.E
Dim xls As Microsoft.Office.Interop.E
xla = CreateObject("Excel.Applic
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
Dim path As String = "c:\GiftCards\GiftCardFile
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(pat
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\G
xla.Quit()
xla = Nothing
Close()
And it throws a com exception?
ASKER
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.E xcel.Appli cation
Dim xlw As Microsoft.Office.Interop.E xcel.Workb ook
Dim xls As Microsoft.Office.Interop.E xcel.Works heet
xla = CreateObject("Excel.Applic ation")
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
Dim path As String = "c:\GiftCards\GiftCardFile s\WILCORPT .txt"
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(pat h)
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\G iftCardFil es\WILCORP T.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?
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.E
Dim xlw As Microsoft.Office.Interop.E
Dim xls As Microsoft.Office.Interop.E
xla = CreateObject("Excel.Applic
xlw = xla.Workbooks.Add
xls = xlw.Sheets("Sheet1")
rowNo = 1
xla.Visible = False
Dim path As String = "c:\GiftCards\GiftCardFile
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(pat
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\G
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.