And I modified the codes and created the project which is attached to this post.
The project works OK but my only problem is that when it creates the CSV file, columns are not created in excel. For example if I expect the data to be placed in A1, B1 and C1 cells, the data will all be placed in A1.
Can you show your code - many people (me included) won't download your project but if you post the relevant part of code within [ CODE ] and [ /CODE ] tags we can look at it on here.
Imports System
Imports System.IO
Imports System.Collections
Public Class Form1
Private MyListText(10) As String
Private MyListItem As ListViewItem
Private MyCSVWriter As System.IO.StreamWriter
Private MyFileName As String
Private MyNewLine As String
Private MyText1 As String
Private MyText2 As String
Private MyText3 As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListView1.Clear() '.....Clear the existing data from the listview
ListView1.View = View.Details '.....IMPORTANT - Set the view to Details to allow tabulation (column mode)
'.....Add ListView Column sizes and Titles
ListView1.Columns.Add("Title1", 200, HorizontalAlignment.Left)
ListView1.Columns.Add("Title2", 200, HorizontalAlignment.Left)
ListView1.Columns.Add("Title3", 200, HorizontalAlignment.Left)
MyListText(0) = "Text1"
MyListText(1) = "Text2"
MyListText(2) = "Text3"
MyListItem = New ListViewItem(MyListText)
'.....Display the record in the ListViewBox
ListView1.Items.Add(MyListItem)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
WriteListViewToCSVFile()
End Sub
Sub WriteListViewToCSVFile()
Dim CSVWriter As New StreamWriter("C:\Test.csv")
For i As Integer = 0 To Me.ListView1.Items.Count - 1
For j As Integer = 0 To Me.ListView1.Columns.Count - 1
CSVWriter.Write(Me.ListView1.Items(i).SubItems(j).Text)
Next
CSVWriter.WriteLine()
Next
CSVWriter.Close()
End Sub
End Class
For j As Integer = 0 To Me.ListView1.Columns.Count - 1
CSVWriter.Write(Chr(34) & (Me.ListView1.Items(i).SubItems(j).Text) & Chr(34) & ", ")
Next
I hadn't realised that you'd basically taken the exact code from those samples. Like I say they are very poor examples of how to write CSV files as they don't actually produce any columns - it might as well be a straight text file.
After you write to the Excel file try this in the End where WB is your workbook Object, Sheets(1) is the sheet you are working with... Please change them to relevant names...
After you write to the Excel file try this in the End where WB is your workbook Object, Sheets(1) is the sheet you are working with... Please change them to relevant names...
Have you read the question? He isn't manipulating anything in Excel, just writing out a CSV file.
I went on the basis of this...
The project works OK but my only problem is that when it creates the CSV file, columns are not created in excel. For example if I expect the data to be placed in A1, B1 and C1 cells, the data will all be placed in A1.
Can anyone solve my problem?
She is writing to a CSV file which is manipulating Excel in this case. What My code does is using the facility which is called "Text To Columns"...
Last edited by Siddharth Rout; Feb 13th, 2010 at 06:01 AM.
Reason: Changed 'He' to 'She' ;)
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Well I don't know. I even tried out another CSV files that I've got but for none Excel produced columns..
OK so its not an issue with your code as such if all CSV files behave the same way, I'd imagine that it is to do with regional settings either in Windows or in Excel.
Try this instead :
Code:
For j As Integer = 0 To Me.ListView1.Columns.Count - 1
CSVWriter.Write((Me.ListView1.Items(i).SubItems(j).Text) & System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator)
Next
OK so its not an issue with your code as such if all CSV files behave the same way, I'd imagine that it is to do with regional settings either in Windows or in Excel.
Try this instead :
Code:
For j As Integer = 0 To Me.ListView1.Columns.Count - 1
CSVWriter.Write((Me.ListView1.Items(i).SubItems(j).Text) & System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator)
Next
It worked for me!
Sounds like the problem was the listseperator in my windows.
Thanks a lot!
The only condition, the above code will fail is if in Iran they have something else for ","....
There's absolutely no need to do it using references to excel - it introduces unnecessary complexity and dependencies.
I've amended the code above so it will work with whatever the comma separator
Code:
For j As Integer = 0 To Me.ListView1.Columns.Count - 1
CSVWriter.Write((Me.ListView1.Items(i).SubItems(j).Text) & System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator)
Next