Home   Artículos   Recursos   Foros   
Artíclos recientes publicados en Latindevelopers:
Visual C++: NSDoubleEdit: Un control para el manejo de números decimales en Visual C++.
Visual C++: Implementando una Calculadora en Visual C++
Visual C++: CCommandLine: Una clase para el uso de la linea de comando
Visual C++: Una clase para el manejo del Registro

Exportar datagrid a un archivo excel

Visual Formularios, MDI, Módulos, Class, DataReports, Rutime y mas..

Moderador: jAngel

Exportar datagrid a un archivo excel

Notapor weekend el Vie Nov 24, 2006 4:42 pm

hola como estan bueno el problma es que no puedo esportar datos de un data grid hacia un arhivo de exel...puedohacer que se abra el exel pero lo que me guarda es el formulario y no el data grid como yo quiero.......



private void verExcel_Click(object sender, System.EventArgs e) {
// No enviamos la respuesta hasta que hemos
// terminado de procesar todo
Response.Buffer = true;

// Ponemos el tipo de la respuesta al valor adecuado
Response.ContentType = "application/vnd.ms-excel";

// Desactivamos el view state, ya que no lo necesitamos
this.EnableViewState = false;

// Desactivamos propiedades conflictivas
dataGrid.AllowPaging = false;
dataGrid.AllowSorting = false;

// Enlazamos los datos de nuevo para reflejar
// los cambios
dataGrid.DataBind();


// A continuación escribiremos el HTML que genera
// el dataGrid en la respuesta para enviárselo al
// cliente
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);

dataGrid.RenderControl(hw);

Response.Write(tw.ToString());

// Enviamos los datos al cliente
Response.End();
}
estoy usando esta instruccion.. pero no pasa nada con lo que yo quiero....
weekend
Novato
Novato
 
Mensajes: 1
Registrado: Vie Nov 24, 2006 4:34 pm

HOLA, CREO Q ESTO TE PUEDE AYUDAR

Notapor DARIUS-22 el Lun Ago 27, 2007 2:48 am

Option Explicit

Private Sub Command1_Click()
'+++++++++++++++++++++++++++++++++++++++++++
' Reference : MicroSoft Excel 9.0 Object Library
'+++++++++++++++++++++++++++++++++++++++++++
Dim xl As Excel.Application
Dim wb As Object
Dim ws As Object
Dim excelwasnotrunnung As Boolean

' Make New Object
Set xl = CreateObject("Excel.Application")

' Display Screen
xl.Application.Visible = True
' Add Excel
xl.Workbooks.Add
xl.Worksheets("hoja1").Name = "HelloSheet" ' Change Name of Sheet

Dim i, j, k, p As Integer

k = Data1.Recordset.RecordCount - 1 ' Count Total Row in DB

p = Data1.Recordset.Fields.Count - 1 ' cOUNT Total Column in DB

With DBGrid1


For i = 0 To k ' Starting from [0] because the start point of DBGRID
' is [0] <---- STARTING ROW

For j = 0 To p ' Starting from [0] because the start point of DBGRID
' is [0] <--- STARTING COLUMN


.Row = i
.Col = j



'------------------------------------------------------------
' IF Some of Data on your DB may have NULL. DO NOT worry !
' The relevant point of cells in the Excel will not be
' shown [without error]
'
' If you want to test !
' 1). Open DB1.mdb File
' 2). Delete any of data in the Field
' 3). Save
' 4). Run Program
'------------------------------------------------------------

xl.Worksheets("HelloSheet").Cells(i + 1, j + 1).Value = .Text


'----------------------- (i + 1, j + 1) --------------------
' This is very very important matter !
' If you not start + 1 . Then, You will see critical error
' during transfer data form DBGRID to Excel
' because The starting point of the Excel's cells is [Cells(1, 1)]
' NOT (0, 0) !
'
' In addition, If you want to shift the starting point of output
' on the Excel, Then you can change like ..
'
' .Cells(i + 5, j + 5).Value = .Text ... Whatever you want !
'------------------------------------------------------------------

Next


Next

End With

Set wb = Nothing
Set xl = Nothing


End Sub


Private Sub Form_Load()

Command1.Caption = "Start Transfer Data"

Data1.DatabaseName = App.Path & "\db1.mdb"
Data1.Refresh

Call Data_Description
End Sub
Private Sub Data_Description()


If Data1.Recordset.EOF And Data1.Recordset.BOF Then

MsgBox "NO Data available !", vbCritical + vbOKOnly, "Error"

Exit Sub
End If


Data1.RecordSource = "SELECT * FROM Table1 WHERE [aa] ORDER BY [aa]"
Data1.Recordset.MoveLast
Data1.Recordset.MoveFirst
Data1.Refresh
Data_Grid


End Sub
Private Sub Data_Grid()

'--------- Adjust Column width of DB Grid -------------

With DBGrid1

.HeadLines = 1
.Columns(0).Width = 500
.Columns(1).Width = 500
.Columns(2).Width = 600
.Columns(3).Width = 700
.Columns(4).Width = 600
.Columns(5).Width = 700
End With


End Sub

Private Sub Form_Unload(Cancel As Integer)
Set Form1 = Nothing
Data1.Recordset.Close
End
End Sub


ESPERO TE SIRVA
DARIUS-22
Novato
Novato
 
Mensajes: 2
Registrado: Dom Ago 26, 2007 5:26 am


Volver a Visual Basic / Visual Basic .NET

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 0 invitados