Results 1 to 4 of 4

Thread: ArrayList to Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    115

    ArrayList to Excel

    Hi I am try to save ArrayList to Excel but I only save the last record in the ArrayList to all sheet
    anyone who can help me fix this, thanks


    Code:
           Dim objExcel As New Excel.Application
                        Dim objWorkbook As Excel.Workbook
                        Dim objWorksheet As Excel.Worksheet
                      
                        Try
    
                            objWorkbook = objExcel.Workbooks.Open("PATH TO FILE\" + "FILE.xlsm")
                            objWorksheet = objWorkbook.Worksheets.Item("Sheet1")
                            For Each Subject In Subjectlisti                       
                                objWorksheet.Range("B2", "B120").Value = Subject.type
                                objWorksheet.Range("C2", "C120").Value = Subject.count
                                objWorksheet.Range("D2", "D120").Value = Subject.meters
                            Next
                           
                            objWorkbook.Save()
    
                        Catch Ex As Autodesk.AutoCAD.Runtime.Exception
                            'Handle exception
                        Finally
                            'Do cleanup
    
                            objExcel.Workbooks.Close()
                            objExcel.Application.Quit()
                            objExcel.Visible = False
    
                        End Try
    Regards
    ssalomon

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: ArrayList to Excel

    For Each Subject In Subjectlisti
    objWorksheet.Range("B2", "B120").Value = Subject.type
    objWorksheet.Range("C2", "C120").Value = Subject.count
    objWorksheet.Range("D2", "D120").Value = Subject.meters
    Next
    Since you are writing individual ArrayList elements to individual cells, try modifying the code above to use counters instead of constant range values.
    See sample logic below:
    Code:
    Dim counter As Integer = 1
    For Each Subject In Subjectlisti                  
        objWorksheet.Range("B" + counter.ToString(), "B" + counter.ToString()).Value = Subject.type
        counter += 1   
        'add conditions and codes based on your requirements
    Next
    Note: Use Generic List instead of ArrayList.

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    115

    Re: ArrayList to Excel

    Thanks Kgc for the help will check out with the Generic List

    ssalomon

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: ArrayList to Excel

    Arraylists were added back in the first version of .NET, but once generics were added in 2005, there was no longer any use for the ArrayList. An ArrayList is effectively a List(of Object) and works poorly with most types while not being type safe. The generic List(of T) will work better for all value types and is type safe for all types, so the ArrayList has no advantage over the List(of T), while the List has some advantages over the ArrayList.
    My usual boring signature: Nothing

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