save listview data to same excel file but different sheet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sigridish
    New Member
    • Nov 2011
    • 4

    #1

    save listview data to same excel file but different sheet

    i already know how to save in excel using openfiledialog. but what i want to happen is that when i save another file, i want it to be saved in my existing file but in a different sheet. can you guys help me?

    this is my code for saving

    Code:
    Public Sub saveExcelFile(ByVal FileName As String)
                Dim xls As New Excel.Application
                Dim sheet As Excel.Worksheet
                Dim i As Integer
                xls.Workbooks.Add()
                sheet = xls.ActiveWorkbook.ActiveSheet
                Dim row As Integer = 1
                Dim col As Integer = 1
                For Each item As ListViewItem In ListView1.Items
                        For i = 0 To item.SubItems.Count - 1
                                sheet.Cells(row, col) = item.SubItems(i).Text
                                col = col + 1
                        Next
                        row += 1
                        col = 1
                Next
                xls.ActiveWorkbook.SaveAs(FileName)
                xls.Workbooks.Close()
                xls.Quit()
        End Sub
    
    
    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
                Dim saveFileDialog1 As New SaveFileDialog
                saveFileDialog1.Filter = "Excel File|*.xlsx"
                saveFileDialog1.Title = "Save an Excel File"
                saveFileDialog1.ShowDialog()
                If saveFileDialog1.FileName <> "" Then
                        saveExcelFile(saveFileDialog1.FileName)
                End If
                MessageBox.Show("Record Saved!")
        End Sub
  • pod
    Contributor
    • Sep 2007
    • 298

    #2
    Code:
    if fileExist then
      workbook.open file
      worksheet.add sheet
      put content in sheet
      save and close workbook
    else
      workbook.add new book
      put content in activesheet
      save and close workbook
    end if

    Comment

    Working...