Ver Mensaje Individual
  #3 (permalink)  
Antiguo 31/05/2011, 09:51
Avatar de Carlojas
Carlojas
 
Fecha de Ingreso: junio-2007
Ubicación: Shikasta
Mensajes: 1.272
Antigüedad: 17 años, 9 meses
Puntos: 49
Respuesta: guardar datagriedview como txt o en excel

Que tal manueltibaduiza, puedes hacerlo de esta forma utilizando esta función que exporta el contenido de tu DataGridView a un excel:
Código vb:
Ver original
  1. Public Sub ExportToExcel(ByVal Dt As DataTable, ByVal Path As String)
  2.         On Error Resume Next
  3.         Dim excelApplication As Excel.Application = New Excel.Application
  4.         Dim excelWorkbook As Excel.Workbook = CType(excelApplication.Workbooks.Add(System.Reflection.Missing.Value), Excel.Workbook)
  5.         Dim excelSheet As Excel.Worksheet = CType(excelWorkbook.Sheets(1), Excel.Worksheet)
  6.  
  7.         For i As Integer = 0 To Dt.Columns.Count - 1
  8.             CType(excelSheet.Cells(1, i + 1), Excel.Range).Value2 = Dt.Columns(i).ColumnName
  9.         Next i
  10.  
  11.         excelSheet.Range(excelSheet.Cells(1, 1), excelSheet.Cells(1, Dt.Columns.Count)).Font.Bold = True
  12.         excelSheet.Range(excelSheet.Cells(1, 1), excelSheet.Cells(1, Dt.Columns.Count)).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
  13.         For i As Integer = 0 To Dt.Rows.Count - 1
  14.             excelSheet.Cells.Range(excelSheet.Cells(i + 2, 1), excelSheet.Cells(i + 2, Dt.Columns.Count)).Value2 = Dt.Rows(i).ItemArray
  15.         Next i
  16.  
  17.         Dim tmp As String
  18.         tmp = excelSheet.Cells.Range(excelSheet.Cells(2, 1), excelSheet.Cells(2, 1)).Value2.ToString()
  19.         If Path <> vbNullString Then
  20.             excelWorkbook.Close(True, Path, Nothing)
  21.  
  22.             System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
  23.             System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication)
  24.             excelWorkbook = Nothing
  25.             excelApplication = Nothing
  26.  
  27.             MessageBox.Show("El Archivo fue exportado satisfactoriamente " + Path, "Exportación de Archivos", MessageBoxButtons.OK, MessageBoxIcon.Information)
  28.             Process.Start(Path)
  29.         Else
  30.             MessageBox.Show("Debe seleccionar la ubicación donde guardar el archivo", "Exportación de Archivos", MessageBoxButtons.OK, MessageBoxIcon.Information)
  31.             Exit Sub
  32.         End If
  33.     End Sub

Y en un Boton o donde quieras haces esto habiendo agregado un control SavaFileDialog a tu diseño:
Código vb:
Ver original
  1. Dim ruta As String
  2. Me.SavaFileDialog1.Filter = "Todos los Archivos (*.*)|*.*|Archivos Excel 95-97 (*.xls)|*.xls|Archivos Excel 2000-2003 (*.xls)|*.xls|Archivos Excel 2007 (*.xlsx)|*.xlsx"
  3. Me.SavaFileDialog1 .ShowDialog()
  4. ruta = Me.SavaFileDialog1.FileName
  5. Call ExportToExcel(Me.TUGRID.DataSource, ruta)


Saludos.
__________________
"SELECT * FROM Mujeres WHERE situacion NOT IN ('CASADAS','CON HIJOS','ATORMENTADAS','CUASI-ENNOVIADAS') AND personalidad <> 'INTENSA'"