Ver Mensaje Individual
  #2 (permalink)  
Antiguo 02/03/2010, 13:51
Avatar de jaullo
jaullo
 
Fecha de Ingreso: abril-2009
Mensajes: 994
Antigüedad: 15 años
Puntos: 30
Respuesta: como exportar datos de asp.net a excel 2007

Puedes usar codebehind para exportar

Un ejemplo seria asi:

Function exporta()
Try
Dim RowsCount As Int32 = Me.dtgdatos.SelectedRows.Count - 1

If RowsCount > -1 Then
'Arreglo para los nombres de campos.
Dim FldNames() As String = {"id", "monto_dolar", "monto_colon", " "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.dtgdatos _
.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

Claro esto es visual basic pero usa un convertidor para llevarlo a C#

Necesitas
Using Excel = Microsoft.Office.Interop.Excel

Saludos,