Imagine essa rotina num botão:
Private Sub Gerar_Relatorio_Click()
Dim xlApp As Object
Dim xlWb As Object
Dim xlWs As Object
'Cria um novo documento do excel
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Worksheets(1)
xlApp.Visible = True
xlApp.UserControl = True
'Cria as colunas e determina a largura inicial
xlWs.Columns(1).ColumnWidth = 40
xlWs.Columns(2).ColumnWidth = 12
xlWs.Columns(3).ColumnWidth = 40
xlWs.Columns(4).ColumnWidth = 20
xlWs.Rows(1).RowHeight = 30
'Altera o tamanho da fonte
xlWs.Range("A1:Z1000").Font.Size = 10
'Configura um cabeçalho na linha 1, centralizando e com quebra de texto automatico
With xlWs.Range("A1:Z1")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'Nomeia o cabeçalho
xlWs.Cells(1, 1).Value = "COLUNA A"
xlWs.Cells(1, 2).Value = "COLUNA B"
xlWs.Cells(1, 3).Value = "COLUNA C"
xlWs.Cells(1, 4).Value = "COLUNA D"
'Puxa os dados do ListView (trocar o nome do listview para o seu)
For Row = 2 To ListView1.ListItems.Count + 1
For Col = 1 To ListView1.ColumnHeaders.Count
If Col = 1 Then
xlWs.Cells(Row, Col).Value = ListView1.ListItems(Row - 1).Text
Else
xlWs.Cells(Row, Col).Value = ListView1.ListItems(Row - 1).SubItems(Col - 1)
End If
Next
Next Row
End Sub
DGAF / DVGD - Divisão de Gestão de Documentos
Rafael A. Guimarães
[email protected]
Postado : 28/01/2014 7:00 am