Hi guys,i just wanted to know how to export a record when ever a user clicks from a listview and export it to excel by adding a worksheet whenever the record is more than 3,basically excel offers 3wrk sheets.
can u help?Thank You!!!
Nebil 5 Junior Poster
i got this code but it exports the whole record in the listview.little help please...
Public Sub excel()
Dim lview As ListViewItem
Dim lview2 As ListViewItem.ListViewSubItem
Dim row, col As Integer
row = 2
col = 1
Try
'Start a new workbook in Excel
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
For Each lview In ListView1.Items
oSheet.Cells(row, col) = lview.Text
For Each lview2 In lview.SubItems
oSheet.Cells(row, col) = lview2.Text
col = col + 1
Next
col = 1
row = row + 1
Next
'Save the Workbook and Quit Excel
oBook.SaveAs("C:\Users\Abdueie\Desktop\BMS\Bridge Asset Management\Adigrat.xlsx")
oExcel.Quit()
MsgBox("Record has been successfully exported", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Nebil 5 Junior Poster
Hey Guys,
I just managed to export a single item to excel application.My idea now is that when i try to export another record to excel,i want to place it on another sheet and third time,on third sheet...
But now when i try to export another one i have to replace the previous folder when excel tells me that a file already exists.
A little hint folks.here's my new code
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.ActiveSheet
Dim col As Integer = 1
For j As Integer = 0 To ListView1.Columns.Count - 1
xlWorkSheet.Cells(col, 1) = ListView1.Columns(j).Text.ToString
col = col + 1
Next
If Not ListView1.SelectedItems.Count = 0 Then
With ListView1.SelectedItems.Item(0)
xlWorkSheet.Cells(1, 2) = .Text.ToString
xlWorkSheet.Cells(2, 2) = .SubItems(1).Text
xlWorkSheet.Cells(3, 2) = .SubItems(2).Text
xlWorkSheet.Cells(4, 2) = .SubItems(3).Text
xlWorkSheet.Cells(5, 2) = .SubItems(4).Text
xlWorkSheet.Cells(6, 2) = .SubItems(5).Text
xlWorkSheet.Cells(7, 2) = .SubItems(6).Text
xlWorkSheet.Cells(8, 2) = .SubItems(7).Text
xlWorkSheet.Cells(9, 2) = .SubItems(8).Text
xlWorkSheet.Cells(10, 2) = .SubItems(9).Text
xlWorkSheet.Cells(11, 2) = .SubItems(10).Text
xlWorkSheet.Cells(12, 2) = .SubItems(11).Text
End With
Else
MsgBox("Please select a record to be exported", vbInformation, "Bridge Asset Management")
Exit Sub
End If
xlApp.Cells.Select()
xlApp.Cells.EntireColumn.AutoFit()
xlApp.Cells.Range("A1").Select()
xlWorkBook.SaveAs("C:\Users\Abdueie\Desktop\BMS\Bridge Asset Management\Adigrat.xlsx")
xlWorkBook.Close()
xlApp.Quit()
MsgBox("Record has been successfully exported", vbInformation, "Bridge Asset Management")
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.