Custom Search

June 2, 2006

.Net : Crear un archivo de Excel, Parte 2

Esta es la segunda parte de la creación de un archivo de Excel. En esta entrega veremos
todo el código necesario para crear la forma en donde visualizaremos los datos de la tabla,
y posteriormente llenar una hoja de un archivo de excel con esa información

El ultimo paso es el guardar el archivo con un nombre.

En esta entrega estamos haciendo un barrido de filas y columnas para llenar el archivo de excel,
en la siguiente parte daremos una explicación de un método distinto de llenado, presentando
así sus pros y contras.



Imports System.Data.SqlClient
Imports Excel


Public Class frmCreateExcelfile
Inherits System.Windows.Forms.Form

Private ds As DataSet
Private objExcel As Excel.Application
Private objWBook As Excel._Workbook
Private objWSheet As Excel._Worksheet
Private objRange As Excel.Range

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
DisposeExcelObjects()
Me.Close()
End Sub

Private Sub DisposeExcelObjects()
Try
objWBook.Close()
objExcel.Workbooks.Close()
Catch ex As Exception

End Try

objExcel.Quit()
If Not objRange Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange)
End If
If Not objExcel Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel)
End If
If Not objWBook Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(objWBook)
End If
If Not objWSheet Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(objWSheet)
End If

objWBook = Nothing
objWSheet = Nothing
objExcel = Nothing
GC.Collect()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cnx As System.Data.SqlClient.SqlConnection
Dim da As System.Data.SqlClient.SqlDataAdapter
Dim cm As System.Data.SqlClient.SqlCommand

Try
cnx = New System.Data.SqlClient.SqlConnection("Data Source=XXX.XXX.XXX.XXX;User Id=XXX;Password=XXX;Initial Catalog=Northwind")

cm = New System.Data.SqlClient.SqlCommand
cm.CommandType = CommandType.Text
cm.CommandText = "SELECT * FROM Employees ORDER BY LastName"
cm.Connection = cnx

da = New System.Data.SqlClient.SqlDataAdapter(cm)

ds = New DataSet
da.Fill(ds)

DataGrid1.DataSource = ds.Tables(0)

Catch ex As Exception
MsgBox(ex.ToString)
Finally
cnx.Close()
cm.Dispose()
cnx.Dispose()
cnx = Nothing
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")
objExcel = New Excel.Application
objExcel.ScreenUpdating = False
objExcel.Visible = False


objWBook = objExcel.Workbooks.Add()

objWSheet = CType(objWBook.Sheets.Item(1), Excel._Worksheet)

Fillsheet(ds)
End Sub

Private Sub Fillsheet(ByVal ds As DataSet)
Dim arrData() As Object
Dim dr As DataRow
Dim i As Int32
Dim j As Int32

ReDim arrData(ds.Tables(0).Columns.Count)

'objWSheet = CType(objWBook.Sheets.Item("Sheet1"), Excel._Worksheet)
objWSheet.Activate()


For i = 0 To ds.Tables(0).Rows.Count - 1
dr = ds.Tables(0).Rows(i)

For j = 0 To 13
'arrData(j) = IIf(IsNumeric(dr(j).ToString), dr(j), dr(j).ToString)
objWSheet.Cells.Item(i + 1, j + 1) = dr(j)
Next
Next


objWSheet.SaveAs(TextBox1.Text)

End Sub
End Class


Esta es una imagen del archivo generado:

1 comment:

Anonymous said...

ahora si te lo voy a piratear we, con los respectivos creditos...