Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Turning the tables
Message
From
24/01/2017 04:29:45
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
23/01/2017 20:28:16
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2016
Network:
Windows Server 2016
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01646970
Message ID:
01646978
Views:
69
>Hi All,
>
>Is there a way to programmatically re-order the columns in a table? I want to export the table to an Excel sheet in a user-defined order.
>Thanks.
>
>Luke

You can use an SQL. Here is a sample using Customer sample data:
Local loStream As ADODB.Stream, lcDataSource, lcSQL, lcColumnHeaders
lcDataSource = _samples+'data'
TEXT to m.lcSQL textmerge noshow
Select Country, Region, City, Cust_Id, Company, Contact
From Customer
ENDTEXT
*** We want to use custom headers on columns, rather than fieldnames
*** Our list should match the order of columns in our select
*** We could simply omit passing headers, then field names would be used as column names
lcColumnHeaders = "Country, Region, City, CustomerID, Company Name, Contact Name"
loStream = GetDataAsAdoStream("Provider=VFPOLEDB;Data Source="+m.lcDataSource, m.lcSQL)

*** Main Excel automation part now
oExcel = Createobject("Excel.Application")
With oExcel
	.DisplayAlerts = .F.
	.Workbooks.Add
	.Visible = .T.
	With .ActiveWorkBook.ActiveSheet
		.Name = 'Customer List'
		* Send the data - safe copy to replacement
		VFP2ExcelVariation(m.loStream, .Range("A1"), m.lcColumnHeaders)
		.Columns.AutoFit()
	Endwith
Endwith


Function VFP2ExcelVariation(toStream, toRange, tcHeaders)
	Local loRS As ADODB.Recordset,ix
	loRS = Createobject('Adodb.Recordset')
	m.loRS.Open( m.toStream )
	* Use first row for headers
	Local Array aHeader[1]
	m.toRange.Offset(1,0).CopyFromRecordSet( m.loRS )  && Copy data starting from headerrow + 1
	For ix=1 To Iif( !Empty(m.tcHeaders), ;
			ALINES(aHeader, m.tcHeaders,1,','), ;
			m.loRS.Fields.Count )
		m.toRange.Offset(0,m.ix-1).Value = ;
			Iif( !Empty(m.tcHeaders), ;
			Alltrim(aHeader[m.ix]), ;
			Proper(m.loRS.Fields(m.ix-1).Name) )
		m.toRange.Offset(0,m.ix-1).Font.Bold = .T.
	Endfor
	m.loRS.Close()
Endfunc

Procedure GetDataAsAdoStream(tcConnection, tcSQL)
	Local loStream As 'AdoDb.Stream', ;
		loConn As 'AdoDb.Connection', ;
		loRS As 'AdoDb.Recordset'
	loStream = Createobject('AdoDb.Stream')
	loConn = Createobject("Adodb.connection")
	loConn.ConnectionString = m.tcConnection
	m.loConn.Open()
	loRS = loConn.Execute(m.tcSQL)
	m.loRS.Save( loStream )
	m.loRS.Close
	m.loConn.Close
	Return m.loStream
Endproc
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform