Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Exporting to a Spreadsheet with Field Captions
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00901262
Message ID:
00901326
Views:
14
Hi, Terry,

Check this out, I just wrote it:
The following code will open a table and export the table into a free table as well as a CSV(comma separated value file, excel should open this when double click on the file) file which is also a text file:
LPARAMETERS lpcTable,lpcExport2TableName,m.lpcexportpath
*lpcTable the table name e.g. dbname!tablename
*lpcExport2TableName a free table name the exported
*m.lpcexportpath the directory into which you want to save the exports

LOCAL ARRAY a(1,16),captions(1)
LOCAL lnLen,i,lcexportto,lcvalues,lcAlias,lcfield,lcfields
*use the table name as alias name
IF "!" $ m.lpcTable ;
THEN
	m.lcAlias = GETWORDNUM(m.lpcTable,2,"!")
ELSE  
	m.lcAlias = JUSTSTEM(m.lpcTable)
ENDIF 
* if table not open then open it 
IF NOT USED(m.lcAlias) ;
THEN 
	USE &lpcTable 
ENDIF 
*get the field names
m.lnLen = AFIELDS(a,m.lcAlias)
m.lcexportto = []
m.lcvalues = []  
m.lcfields = [] 
*get the captions
FOR i =1 TO m.lnLen 
	DIMENSION captions(i)
	m.lcfield = m.lcAlias+'.'+a(i,1)
	m.lcfields = m.lcfields + [ALLTRIM(]+m.a(i,1)+[)+","] 
	m.lcvalues = m.lcvalues + 'TRANSFORM('+m.lcfield+')'
	m.lcexportto = m.lcexportto + a(i,1)+' M'
	m.captions(i) = DBGETPROP(m.lcfield,"FIELD","CAPTION")
	IF i#m.lnLen ;
	THEN 
		m.lcexportto = m.lcexportto + "," 
		m.lcvalues = m.lcvalues + ',' 
		m.lcfields = m.lcfields + '+' 
	ENDIF 
NEXT 
m.lpcExport2TableName = JUSTSTEM(m.lpcExport2TableName)
LOCAL lctemp,lctemp2,lcexported
m.lctemp = ADDBS(m.lpcexportpath)+m.lpcExport2TableName

*create the exported table, if it's there it might ask you to 
*replace it depending on set safety on or off 

CREATE TABLE &lctemp FREE ;
	(&lcexportto)
*make the captions first record of the exporeted table
INSERT INTO &lpcExport2TableName FROM ARRAY captions 

*then insert the values into exported table 
SELECT &lcAlias 
SCAN NOOPTIMIZE
	INSERT INTO &lpcExport2TableName ;
		VALUES (&lcvalues)
ENDSCAN  

* there are records to export 
IF RECCOUNT(m.lpcExport2TableName)>0 ;
THEN 
	SELECT &lpcExport2TableName
	m.lctemp = ADDBS(m.lpcexportpath)+m.lpcExport2TableName
	m.lctemp2 = ["]+m.lctemp +[.csv"] 
	m.lcexported = [] 
        * create CSV 
	SCAN NOOPTIMIZE
		m.lcexported = m.lcexported + &lcfields + CHR(13)+CHR(10)
	ENDSCAN  
	IF NOT EMPTY(m.lcexported) ;
	THEN 
                * save it to file 
		STRTOFILE(m.lcexported,m.lctemp2) 
	ENDIF 
ENDIF 
If call the program export2.prg, then you can:
open database mydatabase
do export2 with "mydatebase!table1","exported","c:\temp"
Then in the c:\temp folder you should see exported.dbf,exported.fpt and exported.csv
I am gonna save this for my own use as well.

Hope this helps.

Dawa, a threader

>Hi Threaders
>
>I need to export my data to a spreadsheet, text file, and/or another VFP table. The thing is: I've got some very long, very descriptive field captions in the table and the "short" names are pretty meaningless to a user.
>
>Anyone know is there any way of exporting to a s/s (and indeed a text file or VFP table) but have the resultant column headings as the field captions instead of the fieldnames?
>
>TIA
>
>Terry
Dawa Tsering


"Do not let any unwholesome talk come out of your mouths,
but only what is helpful for building others up according to their needs,
that it may benefit those who listen."

- Ephesians 4:29-30 NIV

Dare to Question -- Care to Answer

Time is like water in a sponge, as long as you are willing you can always squeeze some.

--Lu Xun, Father of Modern Chinese Literature

Previous
Reply
Map
View

Click here to load this message in the networking platform