how to write data of datagrid in file excel

Interested adding compiled externals from LiveCode and third parties to your LiveCode projects? This is the place to talk about them.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

how to write data of datagrid in file excel

Post by QuangNgo » Wed May 04, 2011 12:39 pm

Hi guys,

I'm working on livecode project , I'm now having a problem of writing data of datagrid in file excel
Hope you guys give me some advice to fix that problem
I'm so appreciated !

Quang

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: how to write data of datagrid in file excel

Post by bangkok » Wed May 04, 2011 12:48 pm

First method :

put the dgProp[ "column labels" ] of group "myDataGrid" into theColumnNames

replace cr with tab in theColumnNames

put the dgText of group "myDataGrid" into theText

ask file "Save file :"
put "file:"&it&".txt" into theFileName
put theColumnNames&CR&CR&theText into url theFileName

Method number 2 :

put empty into theText
put the dgData of group "myDataGrid" into theData
put the dgIndexes of group "myDataGrid" into theIndexes

repeat for each item theIndex in theIndexes
put theData[theIndex]["myCol1"] &tab&theData[theIndex]["myCol2"]& cr after theText
end repeat

ask file "Save file :"
put "file:"&it&".txt" into theFileName
put theText into url theFileName

QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

Re: how to write data of datagrid in file excel

Post by QuangNgo » Thu May 05, 2011 1:19 am

Thanks a lot bangkok :D
That's very useful !

Thanks again :D
Quang

QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

Re: how to write data of datagrid in file excel

Post by QuangNgo » Thu May 05, 2011 2:51 am

Hi bangkok

There is a small problem .I do follow your solution and It work well except one thing is file format.
In your code, I changed like this
ask file "Save file :"
put "file:"&it&".xlsx" into theFileName -----turn txt into xlsx (the error here file format or extension is not valid) but It's fine if the file extension is ".xls"
put theText into url theFileName

Is there any differences between them ? Could you explain it to me why?
Thanks again
Quang

SparkOut
Posts: 2857
Joined: Sun Sep 23, 2007 4:58 pm

Re: how to write data of datagrid in file excel

Post by SparkOut » Thu May 05, 2011 11:17 am

MS Excel has a much more sophisticated file format than just setting the text. Simply setting the filename extension doesn't change the file format. To write a data file in Excel would involve reverse engineering the proprietary file format and working out how to convert the text data into the binary storage format, with all the delimiters and all the other file information required.
That is waaaay out of this scope, and not necessary at all. Excel will quite happily open a tab delimited text file and show on screen. You can then save the file in native Excel format from within the Excel interface.
If you are concerned that a double-click on your file with a .txt extension opens that file in Notepad or other text editor then you could perhaps try changing the filename extension to .tab which is also recognised by Excel. Otherwise (if you really have to) you could adapt bangkok's code to include a double quote before and after each data element, and change the delimeter to comma instead of tab. Then use .csv as the filename extension and you will probably find that Excel is the more usual default application that opens such files (.tab or .csv). Tab delimited is a much more robust solution than Comma Separated Values though.

QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

Re: how to write data of datagrid in file excel

Post by QuangNgo » Thu May 05, 2011 2:55 pm

Hi SparkOut !

Thank you so much your advice :D That's very useful explaination

Thanks again both of you

Quang

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: how to write data of datagrid in file excel

Post by BarrySumpter » Mon Aug 15, 2011 10:43 pm

bangkok wrote:First method :

Code: Select all


   put the dgProp[ "column labels" ] of group "myDataGrid" into theColumnNames

   replace cr with tab in theColumnNames

   put the   dgText of group "myDataGrid" into theText

   ask file "Save file :"
   put "file:" & it & ".txt" into theFileName
   put theColumnNames & CR & CR & theText into url theFileName

Method number 2 :

Code: Select all


   put empty into theText
   put the dgData of group "myDataGrid" into theData
   put the dgIndexes of group "myDataGrid" into theIndexes
   
   repeat for each item theIndex in theIndexes
      put theData[theIndex]["myCol1"] & tab & theData[theIndex]["myCol2"] & cr after theText
   end repeat

   ask file "Save file :"
   put "file:" & it &".txt" into theFileName
   put theText into url theFileName

All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

Re: how to write data of datagrid in file excel

Post by QuangNgo » Thu Aug 18, 2011 9:52 am

Hi Barry Sumpter

I had the solution to do it before but I'm so happy with your help

Thanks a lot ^^

Regards,
Quang

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: how to write data of datagrid in file excel

Post by BarrySumpter » Thu Aug 18, 2011 9:55 am

Should have added the note that I reposted that same code to make it easier for me to read.

Nice simple straightforward script.
Very nice.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Post Reply

Return to “Using Externals”