+ Reply to Thread
Results 1 to 2 of 2

Thread: from listview to excel

  1. #1
    Registered User
    Join Date
    11-23-2011
    Location
    laguna philippines
    MS-Off Ver
    Excel 2007
    Posts
    1

    from listview to excel

    hi all! i have a program which displays the data from my database to my listview and saves it to excel. now my problem is that the column name is not included in the saved excel file. here is my code.
    for save button
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.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
    for the function saveexcelfile

    Public Sub saveExcelFile(ByVal FileName As String)
            Dim xls As New Excel.Application
            Dim sheet As Excel.Worksheet
            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 As Integer = 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
    could someone help me? im using vb.net

  2. #2
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: from listview to excel

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook. Don't upload a picture when you have a workbook. None of us is inclined to recreate your data. Upload the workbook and manually add an 'after' situation so that we can see what you expect. In addition clearly explain how you get the results.

    To attach a file to your post, you need to be using the main 'New Post' or 'New Thread' page and not 'Quick Reply'.
    To use the main 'New Post' page, click the 'Post Reply' button in the relevant thread.

    On this page, below the message box, you will find a button labelled 'Manage Attachments'.
    Clicking this button will open a new window for uploading attachments.

    You can upload an attachment either from your computer or from another URL by using the appropriate box on this page.
    Alternatively you can click the Attachment Icon to open this page.

    To upload a file from your computer, click the 'Browse' button and locate the file.

    To upload a file from another URL, enter the full URL for the file in the second box on this page.
    Once you have completed one of the boxes, click 'Upload'.

    Once the upload is completed the file name will appear below the input boxes in this window.
    You can then close the window to return to the new post screen.
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0