
25/05/2009, 10:27
|
 | | | Fecha de Ingreso: abril-2009
Mensajes: 994
Antigüedad: 15 años, 10 meses Puntos: 30 | |
Respuesta: Exportar a Excel Checate esto
Function exporta()
Try
Dim RowsCount As Int32 = Me.dtgfactxc.SelectedRows.Count - 1
If RowsCount > -1 Then
'Arreglo para los nombres de campos.
Dim FldNames() As String = {"id", "auxiliar", "operador", "periodo", "factura", "nota", "monto_dolar", "monto_colon", "registrada", "num_liquidacion", "nota_interna", "orden_pago", "mes_liquidado", "monto_liquidado", "monto_pendiente", "fecha"}
'Arreglo para los datos seleccionados.
Dim DataArr(RowsCount, 15) As Object
Dim ColsCounter As Int32 = 0
'Populate the data array - The list is sorted in ascending order.
For RowsCounter As Int32 = 0 To RowsCount
For Each cell As DataGridViewCell In Me.dtgfactxc _
.SelectedRows(RowsCount - RowsCounter) _
.Cells
DataArr(RowsCounter, ColsCounter) = cell.Value
ColsCounter = ColsCounter + 1
Next
ColsCounter = 0
Next
'Populate the data array - The list is sorted in descending order.
'Dim Rows As Int32 = 0
'For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
'For Each cell As DataGridViewCell In Me.DataGridView1.SelectedRows(Rows).Cells
'DataArr(Rows, ColsCounter) = cell.Value
'ColsCounter = ColsCounter + 1
'Next
'ColsCounter = 0
'Rows = Rows + 1
'Next
'Variables for Excel.
Dim xlApp As New Excel.Application
Dim xlWBook As Excel.Workbook = xlApp.Workbooks.Add( _
Excel.XlWBATemplate.xlWBATWorksheet)
Dim xlWSheet As Excel.Worksheet = CType(xlWBook.Worksheets(1), Excel.Worksheet)
Dim xlCalc As Excel.XlCalculation
'Save the present setting for Excel's calculation mode and turn it off.
With xlApp
xlCalc = .Calculation
.Calculation = Excel.XlCalculation.xlCalculationManual
End With
'Write the field names and the data to the targeting worksheet.
With xlWSheet
.Range(.Cells(1, 1), .Cells(1, 16)).Value = FldNames
.Range(.Cells(2, 1), .Cells(RowsCount + 2, 16)).Value = DataArr
.UsedRange.Columns.AutoFit()
End With
With xlApp
.Visible = True
.UserControl = True
'Restore the calculation mode.
.Calculation = xlCalc
End With
'Sacar los elementos de memoria.
xlWSheet = Nothing
xlWBook = Nothing
xlApp = Nothing
GC.Collect()
End If
Catch ex As Exception
MessageBox.Show("Error al Tratar de exportar los datos", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Function
Funciona perfectamente... |