Export data to Excel using arrays

168 views
Skip to first unread message

jparada

unread,
Nov 10, 2021, 11:23:00 PM11/10/21
to Harbour Users
Hi, I am trying to export the contents of a recordset to an Excel sheet, I am trying with the technique of doing it by array, I found some example with Vfp, can someone please help me "translate" the code snippet from Vfp to Harbour. 

dimension aExcelData[5000,10]
for ix = 1 to 5000
  for jx=1 to 10
    aExcelData[m.ix,m.jx] = m.ix*100+m.jx
  endfor
endfor

WITH oExcel.ActiveWorkBook.ActiveSheet
  .Range(.Cells(1,1), .Cells(5000,10)).Value = GetArrayRef('aExcelData')
endwith

PROCEDURE GetArrayRef(tcArrayName)
RETURN @&tcArrayName

I appreciate any help.

Regards,
Javier

Auge & Ohr

unread,
Nov 11, 2021, 2:12:43 AM11/11/21
to Harbour Users
hi,

   // "write" Array to Excel Sheet
   oSheet:range( "A1:"+cEnde+LTRIM(STR(numRows)) ):value := aExcel

   // "read" from "select" Range into Array
   aExcel := oSheet:range( "A1:"+cEnde+LTRIM(STR(numRows)) ):value

Jimmy

jparada

unread,
Nov 11, 2021, 1:06:49 PM11/11/21
to Harbour Users
Hi, 

There is something "special" to consider in the structure of the array, normally I generate the array in this way:

oRs:MoveFirst()
Do While !oRs:Eof()
  AAdd( aExcelData, { oRs:Fields( 'numero' ):Value, oRs:Fields( 'nombre' ):Value } )
    
  oRs:MoveNext()
Enddo

if I do:

With Object oExcel:ActiveWorkBook:ActiveSheet
  :Range(:Cells(1,1), :Cells(10,2)):Value = aExcelData
Endwith

Excel file is generated but without content (empty)

if I do:

With Object oExcel:ActiveWorkBook:ActiveSheet
  :Range(:Cells(1,1), :Cells(10,2)):Value = hb_ValToExp( aExcelData )
Endwith

The Excel file is generated but not a corresponding content in rows and columns

Any other tip?

Regards,
Javier

poopall

unread,
Nov 17, 2021, 5:44:06 PM11/17/21
to Harbour Users
This is some of my code 

try
                oExcel = CREATEOBJECT( "Excel.Application" )
catch oErr
                Alert( "Excel not found on this machine therefore cannot run" )
                return
end

        oExcel:WorkBooks:Add()
        oSheet := oExcel:ActiveSheet
        oSheet:Name = "YourSheetName"
        nRow := 1
        nColumn := 1
        oSheet:Cells( nRow,  nColumn ):Value =     "what ever value you want here'  // your array value

This should give you a hint as to how it works for me
Reply all
Reply to author
Forward
0 new messages