|
-
Aug 20th, 2016, 09:29 AM
#1
Thread Starter
Lively Member
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
-
Aug 20th, 2016, 11:21 AM
#2
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
-
Aug 20th, 2016, 12:08 PM
#3
Thread Starter
Lively Member
Re: ArrayList to Excel
Thanks Kgc for the help will check out with the Generic List
ssalomon
-
Aug 20th, 2016, 03:17 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|