Results 1 to 26 of 26

Thread: Writing a ListView to a CSV File

  1. #1

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Question Writing a ListView to a CSV File

    Hey folks,
    I found the following links to fill a listview and then write it to a CSV.

    http://www.freevisualbasic2008source...fillalistview/
    http://www.freevisualbasic2008source...iewtoacsvfile/

    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 anyone solve my problem?

    Thanks for your help.
    Attached Files Attached Files

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    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.

  3. #3

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File

    Here:
    Code:
    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

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Well CSV stands for comma-separated values, ie the columns are delineated by commas and rows are delineated by carriage returns.

    You code doesn't put in any commas, just carriage returns (when you call WriteLine).

    Try replacing your inner loop with this :

    Code:
                For j As Integer = 0 To Me.ListView1.Columns.Count - 1
                    CSVWriter.Write(Me.ListView1.Items(i).SubItems(j).Text) & ", "
                Next
    NB - its not entirely your fault - the two sample bits of code you linked to are both rubbish!

  5. #5

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File

    Well, I did that. But nothing changed. All the data were placed in the A1 cell with commas shown.

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Can you post an example of the actual file

  7. #7

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File



    That's the CSV file the project creates.

  8. #8

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File

    BTW, why do you think the codes are rubbish? Those were only useful codes I found!

  9. #9
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Quote Originally Posted by arithmetica View Post
    BTW, why do you think the codes are rubbish? Those were only useful codes I found!
    Why are they rubbish? Because they will never do anything other than write a single entry on each line - exactly as you have found.

    I asked if you would post the file, not a screenshot of how excel is interpreting the file...

    I suspect the problem is that the entire line may be enclosed in quotes.

  10. #10

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File

    Sorry!
    It's attached.
    Attached Files Attached Files
    Last edited by arithmetica; Feb 13th, 2010 at 05:28 AM.

  11. #11
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Change the loop to

    Code:
                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.

  12. #12
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Can you post the contents of the file rather than attaching archives - like I say I've no interest in downloading files.

    You can get the contents of the file by right-clicking and selecting Open with... and selecting Notepad.

    Even without the quotes the file produced with my first amendment worked fine on my machine.

  13. #13

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File

    Ah, that's how it looks like in Notepad:

    "Text1", "Text2", "Text3",

  14. #14
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    That should be absolutely fine. For me using Excel it opens like this :
    Attached Images Attached Images  

  15. #15
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Writing a ListView to a CSV 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...

    Code:
    WB.Sheets(1).Range("A1").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True

  16. #16
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Quote Originally Posted by koolsid View Post
    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...

    Code:
    WB.Sheets(1).Range("A1").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
    Huh?

    Have you read the question? He isn't manipulating anything in Excel, just writing out a CSV file.

  17. #17

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File

    Well I don't understand what the problem is when the contents look alright in Notepad.

  18. #18
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Writing a ListView to a CSV File

    Quote Originally Posted by keystone_paul View Post
    Huh?

    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

  19. #19

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File

    Quote Originally Posted by keystone_paul View Post
    ?

    Have you read the question? He isn't manipulating anything in Excel, just writing out a CSV file.
    Actually She..

  20. #20
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Apologies for the gender mixup!

    The only thing I can think of is if you are in Iran does Windows have a different list separator than comma?

  21. #21

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Re: Writing a ListView to a CSV File

    Well I don't know. I even tried out another CSV files that I've got but for none Excel produced columns..

  22. #22
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Quote Originally Posted by koolsid View Post
    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"...
    Writing out a CSV file is not necessarily manipulating excel - it just so happens that excel is the tool being used to view the output.

    Using the method you suggest is entirely architecturally more complex and relies on the machine generating the csv file having Excel installed.

    There is no workbook object to reference in this example.

  23. #23
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Quote Originally Posted by arithmetica View Post
    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

  24. #24
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Writing a ListView to a CSV File

    Quote Originally Posted by keystone_paul View Post
    Writing out a CSV file is not necessarily manipulating excel - it just so happens that excel is the tool being used to view the output.
    Agreed and that is the reason why I mentioned

    She is writing to a CSV file which is manipulating Excel in this case.
    Using the method you suggest is entirely architecturally more complex and relies on the machine generating the csv file having Excel installed.

    There is no workbook object to reference in this example.
    The workbook object was an example. There is still data which is being outputted to Range("A1")....

    The only condition, the above code will fail is if in Iran they have something else for ","....

    Edit: I don't have access to vb.net at the moment but later in the day, I will amend the above code and give you an exact example....
    Last edited by Siddharth Rout; Feb 13th, 2010 at 06:15 AM.
    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

  25. #25

    Thread Starter
    Lively Member arithmetica's Avatar
    Join Date
    Jan 2009
    Location
    Ahvaz, Iran
    Posts
    89

    Resolved Re: Writing a ListView to a CSV File

    Quote Originally Posted by keystone_paul View Post
    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!

  26. #26
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Writing a ListView to a CSV File

    Quote Originally Posted by koolsid View Post
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width