Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Fastest way to export MSFlexgrid data to Excel file.
[ All Languages » VB »  Excel]

Total Hit ( 8906)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
'//This function will export Grid (MSFlexgrid) to Excel file
Public Function ExportGrid(TheFlexgrid As MSFlexGrid, _
 TheRows As Integer, TheCols As Integer, Optional WorkSheetName As String)

  Dim i As Integer
  DimAs Integer
  Dim sString As String

  'EarlyBinding (Requires Reference to Microsoft Excel library)

  '////////////////////////////////////////////
  '//For early Binding use following block
  'Dim objXL As New Excel.Application
  'Dim wbXL As New Excel.Workbook
  'Dim wsXL As New Excel.Worksheet

  'LateBinding

  '////////////////////////////////////////////
  '//For late binding use following block//////
  Dim objXL, wbXL, wsXL

  Set objXL = CreateObject("Excel.Application")
  Set wbXL = CreateObject("Excel.Workbook")
  Set wsXL = CreateObject("Excel.Worksheet")
  '////////////////////////////////////////////


  Dim intRow As Integer  ' counter
  Dim intCol As Integer  ' counter

  If Not IsObject(objXL) Then
    MsgBox "You need Microsoft Excel to use this function", _
        vbExclamation, "Print to Excel"
    Exit Function
  End If

  On Error Resume Next

  ' open Excel
  objXL.Visible = True
  Set wbXL = objXL.Workbooks.Add
  Set wsXL = objXL.ActiveSheet

  ' name the worksheet
  With wsXL
    If Not WorkSheetName = "" Then
      .Name = WorkSheetName
    End If
  End With


  sString = ""
  For i = 0 To TheRows - 1
    For o = 0 To TheCols - 1
      If Len(sString) = 0 Or Right(sString, 1) = vbCr Then
      Else
        sString = sString & vbTab
      End If
      sString = sString & TheFlexgrid.TextMatrix(i, o)
    Next
    sString = sString + vbCr
  Next
  If Right(sString, 1) = vbCr Then sString = Left(sString, Len(sString) - 1)
  Debug.Print sString

  Clipboard.Clear
  Call Clipboard.SetText(sString, 1)
  wsXL.Paste
  Clipboard.Clear

  wsXL.Columns("A:Z").EntireColumn.AutoFit

End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.