Comment on Tutorial - How to export from DataGridView to excel using VB.net By Issac
Comment Added by : Anonymous
Comment Added at : 2015-04-21 03:10:41
Comment on Tutorial : How to export from DataGridView to excel using VB.net By Issac
' REFERENCIA EN EL PROYECTO (Microsoft Excel 12.0 Object Library)
' BUSCARLA EN PESTAÑA COM (C:Program Files (x86)Microsoft OfficeOffice12EXCEL>EXE)
Imports Excel = Microsoft.Office.Interop.Excel Imports Microsoft.Vbe.Interop
Public Class Form1
Sub DATAGRIDVIEW_TO_EXCEL(ByVal DGV As DataGridView)
Try
Dim DTB = New DataTable, RWS As Integer, CLS As Integer
For CLS = 0 To DGV.ColumnCount - 1
DTB.Columns.Add(DGV.Columns(CLS).Name.ToString)
Next
Dim DRW As DataRow
For RWS = 0 To DGV.Rows.Count - 1
DRW = DTB.NewRow
For CLS = 0 To DGV.ColumnCount - 1
If DGV.Columns(CLS).Visible = True Then
Try
DRW(DTB.Columns(CLS).ColumnName.ToString) = DGV.Rows(RWS).Cells(CLS).Value.ToString
Catch ex As Exception
End Try
End If
Next
DTB.Rows.Add(DRW)
Next
DTB.AcceptChanges()
Dim DST As New DataSet
DST.Tables.Add(DTB)
DTB.WriteXml("C:MMS FILESRESOURCESXML.xml") ' SE CREA UN XML CON LA INFO DEL DATAGRIDVIEW
MACRO("C:MMS FILESRESOURCESXML.xml") ' Y ESTE PROCEDIMIENTO LO ABRE EN EXCEL
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub MACRO(ByVal FLE As String)
Try
Dim xlApp As Object = New Microsoft.Office.Interop.Excel.Application
Try ' OPCIONALMENTE LE DAS TAMAÑO A TU APLICACION DE EXCEL
xlApp.Left = 250
xlApp.Top = 100
xlApp.Width = 900
xlApp.Height = 550
Catch ex As Exception
' PROTEGIENDO EL PROCESO SI LA ULTIMA APLICACION FUE EN FULL SCREEN
End Try
Dim xlWb As Excel.Workbook = xlApp.Workbooks.Add ' AGREGAS TU LIBRO DE EXCEL
Dim MT As Integer ' Y OPCIONALMENTE LE DAS ESTILO Y FORMATO A SUS 3 HOJAS
For MT = 3 To 1 Step -1
xlWb.Sheets(MT).Select()
xlWb.Sheets(MT).cells.select()
xlWb.Sheets(MT).cells.Font.Name = "Arial"
xlWb.Sheets(MT).cells.Font.Size = 8
xlWb.Sheets(MT).cells.Font.bold = True
xlWb.Sheets(MT).Range("A1").Select()
Next
' LE CREAS UN MODULO LO NOMBRAS Y LE AGREGAS UNA MACRO DE ARRANQUE AL ABRIR
Dim xlMod As Microsoft.Vbe.Interop.VBComponent = xlWb.VBProject.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule)
xlMod.Name = "Module1"
Dim macroCode As String = _
"Public Sub Main()" & vbCrLf & _
" ActiveWorkbook.XmlImport URL:=" & Chr(34) & FLE & Chr(34) & ", ImportMap:=Nothing, Overwrite:=True, Destination:=Range(" & Chr(34) & "$A$1" & Chr(34) & ")" & vbCrLf & _
" ActiveSheet.ListObjects(" & Chr(34) & "Table1" & Chr(34) & ").TableStyle = " & Chr(34) & "TableStyleMedium12" & vbCrLf & _
" Sheet1.Range(" & Chr(34) & "A2" & Chr(34) & ").Select" & vbCrLf & _
" ActiveWindow.FreezePanes = True" & vbCrLf & _
"End Sub"
xlMod.CodeModule.AddFromString(macroCode)
xlApp.Visible = True
xlApp.Application.Run("Main")
' OPCIONALMENTE (RECOMENDADO DIRIA YO) BORRAS TU MODULO SALVO QUE QUIERAS VERLO DURANTE LAS PRUEBAS
Dim MDL As Object = xlApp.Application.VBE.ActiveVBProject.VBComponents
MDL.Remove(VBComponent:=MDL.Item("Module1"))
' Y CIERRAS ESTE PROCEDIMIENTO LIBERANDO LAS VARIABLES
ReleaseObject(xlApp)
ReleaseObject(xlMod)
ReleaseObject(xlWb)
ReleaseObject(MDL)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub ReleaseObject(ByVal OBJ As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(OBJ)
Catch ex As Exception
End Try
OBJ = Nothing
GC.Collect()
End Sub
End Class
View Tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. Very nice and easy to understand, keep it up !!
View Tutorial By: claudiu at 2010-02-07 06:12:30
2. "For example, float can be useful when repres
View Tutorial By: rangzen at 2014-03-28 09:31:47
3. Hai.. i tried with d above code but am getting an
View Tutorial By: muthupandi at 2010-08-17 09:49:23
4. How i can comment in server?Some another message
View Tutorial By: Youjizz at 2013-04-09 00:30:47
5. HI EVERY ONE I AM NEW IN J2ME SO I WANT TO LEARN S
View Tutorial By: KALAI at 2011-04-04 03:10:32
6. i just got to know about this and i am really imp
View Tutorial By: spo_mega at 2009-06-11 07:16:38
7. Please give me a example of regionMatches().
View Tutorial By: A. K. M. Saleh Sultan at 2009-10-05 14:52:25
8. one closing bracket ')' is required at while loop
View Tutorial By: varsha Tambe at 2013-01-07 09:34:37
9. copy of h.sheld
View Tutorial By: vivek at 2011-08-31 13:12:36
10. Hi Rajesh, you have found the right site. look at
View Tutorial By: mohan at 2008-07-27 08:55:17