RE:Exportar datos a Excel
Publicado por
aaaa (1 intervención) el 13/12/2006 15:22:44
Private Sub cmdExportar_Click()
Dim arch
'para grabarlo en un archivo al exportarlo directamente
CommonDialog1.Filter = "Libro de Microsoft Excel|*.xls"
CommonDialog1.ShowSave
arch = CommonDialog1.FileName
'creamos el objeto de excel
Set ApExcel = CreateObject("Excel.application")
'añadimos un hoja nueva
ApExcel.Workbooks.Add
With ApExcel.ActiveSheet
'damos formato a las celdas
.Range(.Cells(1, 1), .Cells(1, 13)).Borders.LineStyle = xlContinuous
.Range(.Cells(3, 1), .Cells(6, 13)).Borders.LineStyle = xlContinuous
.Range(.Cells(10, 1), .Cells(10, 13)).Borders.LineStyle = xlDouble
.Range(.Cells(3, 3), .Cells(6, 4)).Interior.Color = vbCyan
'tamaño de fila y columna
.Rows("2").RowHeight = 6
.Columns("A").ColumnWidth = 3
.Columns("B").ColumnWidth = 10
.Columns("C").ColumnWidth = 44
.Columns("D").ColumnWidth = 3.5
.Columns("E").ColumnWidth = 3.5
.Columns("F").ColumnWidth = 7.5
.Columns("G").ColumnWidth = 7.5
.Columns("H").ColumnWidth = 3.5
'colores de las celdas
.Cells(10, 7).Interior.Color = vbCyan
.Cells(10, 12).Interior.Color = vbCyan
.Cells(10, 13).Interior.Color = vbGreen
'enviamos datos a excel en las celdas que se indican
ApExcel.Cells(1, 3).Font.Size = 14
ApExcel.Cells(1, 3).Formula = "nombre"
'se define la fuente para esta celda
'ApExcel.Cells(1, 1).Font = 50
ApExcel.Cells(1, 4).Formula = "ORDEN"
ApExcel.Cells(1, 4).Font.Size = 14
ApExcel.Cells(1, 12).Formula = "Nº"
ApExcel.Cells(1, 12).Font.Size = 14
ApExcel.Cells(3, 1).Font.Size = 12
ApExcel.Cells(3, 1).Formula = "PROV:"
ApExcel.Cells(3, 3).Formula = Me.txtxnomcli
' aqui mandamos datos desde un msflexgrid
rowsexc = 11
Linea = 1
Me.msfarticulos.Row = 1
Do While Me.msfarticulos.Row < Me.msfarticulos.Rows - 1
If (Me.msfarticulos.TextMatrix(Me.msfarticulos.Row, 1) <> "") Then
ApExcel.Cells(rowsexc, 1).Formula = Linea
ApExcel.Cells(rowsexc, 2).Formula = Me.msfarticulos.TextMatrix(Me.msfarticulos.Row, 1) 'cODIGO
ApExcel.Cells(rowsexc, 3).Formula = Me.msfarticulos.TextMatrix(Me.msfarticulos.Row, 2) 'nOMBRE
ApExcel.Cells(rowsexc, 6).Formula =
ApExcel.Cells(rowsexc, 10).Formula = Me.msfarticulos.TextMatrix(Me.msfarticulos.Row, 4) 'Costo Unitario
ApExcel.Cells(rowsexc, 11).Formula = Me.msfarticulos.TextMatrix(Me.msfarticulos.Row, 7) 'valor neto
ApExcel.Cells(rowsexc, 12).Formula = 0
.Range(.Cells(rowsexc, 1), .Cells(rowsexc, 13)).Borders.LineStyle = xlContinuous
.Cells(rowsexc, 7).Interior.Color = vbCyan
.Cells(rowsexc, 12).Interior.Color = vbCyan
.Cells(rowsexc, 13).Interior.Color = vbGreen
rowsexc = rowsexc + 1
Linea = Linea + 1
End If
Me.msfarticulos.Row = Me.msfarticulos.Row + 1
Loop
Set objLibro = ApExcel.Worksheets(1)
ApExcel.Worksheets(1).Name = "COMPRAS"
'borramos la hoja
'ApExcel.Worksheets("Hoja2").Delete
'guardamos excel
ApExcel.Worksheets(1).SaveAs arch
'presentamos
ApExcel.Visible = True
'vista preliminar
'ApExcel.ActiveSheet.PrintPreview
'cerramos excel
'ApExcel.Workbooks.Close
'quitamos excel
'ApExcel.Quit
Set ApExcel = Nothing
End With
End Sub