Jump to content

Listview para Excel (*.xls)


Recommended Posts


Fiz este simples Sub que permite exporta qualquer dados de um determinado ListView para Excel muito facilmente.. penso que poderá ser ultil para quem precisar.

    Private Sub ExportarXLS(ByVal oListView As ListView)
            Using oSF As New SaveFileDialog
                oSF.Filter = "Ficheiro do Excel (*.xls)|*.xls"
                If oSF.ShowDialog = Windows.Forms.DialogResult.OK Then
                    ' Cria o ficheiro..
                    Using oSR As New StreamWriter(oSF.FileName, False, Encoding.Default)
                        Const cQuo As String = """"
                        Const cTab As String = vbTab
                        ' Adiciona o cabeçalho..
                        For Each oColumn As ColumnHeader In oListView.Columns
                            If Not oColumn.Index = (oListView.Columns.Count - 1) Then
                                oSR.Write(oColumn.Text & cTab)
                            End If
                        ' Escreve todos os rows..
                        For Each oItem As ListViewItem In oListView.Items
                            ' Escreve o primeiro item
                            oSR.Write(cQuo & oItem.Text & cQuo & cTab)
                            ' Escreve os restantes subitems
                            For i As Integer = 1 To (oItem.SubItems.Count - 1)
                                oSR.Write(cQuo & oItem.SubItems(i).Text & cQuo & cTab)
                        MessageBox.Show("Ficheiro exportado com êxito!", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                    End Using
                End If
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

Como utilizar:

Call  ExportarXLS(meu_listview)


Making the impossible possible and pwing the world on db at a time.

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.