Grid 2 CSV

HMG Samples and Enhancements

Moderator: Rathinagiri

Post Reply
User avatar
Rathinagiri
Posts: 5321
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 198 times
Been thanked: 210 times
Contact:

Grid 2 CSV

Post by Rathinagiri »

Hi,

I had created a function to export grid data to csv format. I think it would be useful to you too.

Kindly check up for any bugs. :)

grid2csv.prg

usage: grid2csv("formname","gridname",lheader)
if lheader is .f. (default) only the data are exported. Otherwise first line will contain header data.

Code: Select all

# include "minigui.ch"

function grid2csv(windowname,gridname,lheader)

local filename := ""
local adata := {}
local lines := 0
local i := 0
local aeditcontrols := {}
local count1 := 0
local count2 := 0
local linedata := {}
local xres,aec
local cdata := ""
local aclinedata := {}
local fhandle := 0
local linebreak := chr(13)
local aline := {}
local cline := ''
local ncolumns := 0
default lheader := .f.

filename :=  PutFile ( {{"Comma Separated Value Files (*.csv)","*.csv"}} , "Export to text file (CSV)" ,  , .f. ) 
if len(alltrim(filename)) == 0
   return nil
endif

if at(".csv",lower(filename)) > 0
   if .not. right(lower(filename),4) == ".csv"
      filename := filename + ".csv"
   endif
else
   filename := filename + ".csv"
endif

if file(filename)
   if .not. msgyesno("Are you sure to overwrite?","Export to text file (CSV)")
      return nil
   endif
endif

lines := getproperty(windowname,gridname,"itemcount")
IF lines == 0
   msginfo("No rows to save!")
   RETURN nil
ENDIF


fhandle := fcreate(filename)
if fhandle < 0
   msgstop("File "+filename+" could not be created!")
   return nil
endif



i := GetControlIndex ( gridname , windowname )

aEditcontrols := _HMG_SYSDATA [ 40 ] [ i ] [ 2 ]

if lheader
   asize(aclinedata,0)
   ncolumns := len(getproperty(windowname,gridname,"item",1))
   for count1 := 1 to ncolumns
      cdata := getproperty(windowname,gridname,"header",count1)
      aadd(aclinedata,cdata)
   next count1
   aadd(adata,aclone(aclinedata))
endif

for count1 := 1 to lines
   linedata := getproperty(windowname,gridname,"item",count1)
   asize(aclinedata,0)
   for count2 := 1 to len(linedata)
      do case
         case ValType(linedata[count2]) == "N"
            xres := _HMG_PARSEGRIDCONTROLS ( AEDITCONTROLS , count2 )
            AEC := XRES [1]
            AITEMS := XRES [5]
            IF AEC == 'COMBOBOX'
               cdata := aitems[linedata[count2]]
            else
               cdata := LTrim( Str( linedata[count2] ) )
            ENDIF
         case ValType(linedata[count2]) == "D"
            cdata := dtoc( linedata[count2])
         case ValType(linedata[count2]) == "L"
            xres := _HMG_PARSEGRIDCONTROLS ( AEDITCONTROLS , count2 )
            AEC := XRES [1]
            AITEMS := XRES [8]
            IF AEC == 'CHECKBOX'
               cdata := iif(linedata[count2],aitems[1],aitems[2])
            else
               cdata := iif(linedata[count2],"TRUE","FALSE")
            endif
         otherwise
            cdata := linedata[count2]
      endcase
      aadd(aclinedata,cdata)
   next count2
   aadd(adata,aclone(aclinedata))
next count1
for count1 := 1 to len(adata)
   cline := ''
   aline := adata[count1]
   for count2 := 1 to len(aline)
      cline := cline + '"' + _parsequote(aline[count2]) + '"'
      if .not. count2 == len(aline)
         cline := cline + ','
      endif
   next count2
   cline := cline + linebreak
   fwrite(fhandle,cline)
next count1
if fclose(fhandle)
   msginfo("Exported Successfully!")
else
   msgstop("Error in saving!")
endif
return nil
         

function _parsequote(cdata)
local i := 0
local cout := ""
for i := 1 to len(cdata)
   if substr(cdata,i,1) == '"'
      cout := cout + substr(cdata,i,1) + '"'
   else
      cout := cout + substr(cdata,i,1)
   endif
next i
return cout   
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
esgici
Posts: 4537
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 409 times
Been thanked: 118 times
Contact:

Post by esgici »

Thanks to sharing :)

Regards

--

Esgici
Viva INTERNATIONAL HMG :D

User avatar
Vanguarda
Posts: 543
Joined: Wed Feb 11, 2009 10:56 am
Location: Americana - SP
Been thanked: 1 time
Contact:

Post by Vanguarda »

Hi friends,

Thanks for sharing this nice function with us.


With best regards,
--
Paulo Sérgio Durço (Vanguarda)


http://hmglights.wordpress.com/

User avatar
luisvasquezcl
Posts: 1183
Joined: Thu Jul 31, 2008 3:23 am
Location: Chile
Has thanked: 16 times
Been thanked: 18 times
Contact:

Post by luisvasquezcl »

hi,
thank you very much for your input.
regards,
luis vasquez

User avatar
sudip
Posts: 1446
Joined: Sat Mar 07, 2009 11:52 am
Location: Kolkata, WB, India
Has thanked: 5 times
Been thanked: 2 times

Post by sudip »

Hi Rathi,
Thanks for sharing! :)
With best regards.
Sudip
With best regards,
Sudip

User avatar
mol
Posts: 3408
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 268 times
Been thanked: 172 times
Contact:

Post by mol »

Nice work, Rathi!
Marek "MOL"

Post Reply