
31/05/2011, 09:51
|
 | | | 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 originalPublic Sub ExportToExcel(ByVal Dt As DataTable, ByVal Path As String) On Error Resume Next Dim excelApplication As Excel.Application = New Excel.Application Dim excelWorkbook As Excel.Workbook = CType(excelApplication.Workbooks.Add(System.Reflection.Missing.Value), Excel.Workbook) Dim excelSheet As Excel.Worksheet = CType(excelWorkbook.Sheets(1), Excel.Worksheet) For i As Integer = 0 To Dt.Columns.Count - 1 CType(excelSheet.Cells(1, i + 1), Excel.Range).Value2 = Dt.Columns(i).ColumnName Next i excelSheet.Range(excelSheet.Cells(1, 1), excelSheet.Cells(1, Dt.Columns.Count)).Font.Bold = True excelSheet.Range(excelSheet.Cells(1, 1), excelSheet.Cells(1, Dt.Columns.Count)).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter For i As Integer = 0 To Dt.Rows.Count - 1 excelSheet.Cells.Range(excelSheet.Cells(i + 2, 1), excelSheet.Cells(i + 2, Dt.Columns.Count)).Value2 = Dt.Rows(i).ItemArray Next i Dim tmp As String tmp = excelSheet.Cells.Range(excelSheet.Cells(2, 1), excelSheet.Cells(2, 1)).Value2.ToString() If Path <> vbNullString Then excelWorkbook.Close(True, Path, Nothing) System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook) System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication) excelWorkbook = Nothing excelApplication = Nothing MessageBox.Show("El Archivo fue exportado satisfactoriamente " + Path, "Exportación de Archivos", MessageBoxButtons.OK, MessageBoxIcon.Information) Process.Start(Path) Else MessageBox.Show("Debe seleccionar la ubicación donde guardar el archivo", "Exportación de Archivos", MessageBoxButtons.OK, MessageBoxIcon.Information) Exit Sub End If End Sub
Y en un Boton o donde quieras haces esto habiendo agregado un control SavaFileDialog a tu diseño:
Código vb:
Ver originalDim ruta As String Me.SavaFileDialog1.Filter = "Todos los Archivos (*.*)|*.*|Archivos Excel 95-97 (*.xls)|*.xls|Archivos Excel 2000-2003 (*.xls)|*.xls|Archivos Excel 2007 (*.xlsx)|*.xlsx" Me.SavaFileDialog1 .ShowDialog() ruta = Me.SavaFileDialog1.FileName Call ExportToExcel(Me.TUGRID.DataSource, ruta)
Saludos.
__________________ "SELECT * FROM Mujeres WHERE situacion NOT IN ('CASADAS','CON HIJOS','ATORMENTADAS','CUASI-ENNOVIADAS') AND personalidad <> 'INTENSA'" |