Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy to CSV with capitalize column name

Status
Not open for further replies.

TheLazyPig

Programmer
Sep 26, 2019
103
PH
Hi! Is it possible when COPY TO CSV the column name were capitalize instead of small letters? I haven't found yet anything related to this.


Thanks.
 
Not sure if you can do that directly
I tried specifying the fields in caps, and that didn't help
I tried exporting to a free table then exporting that and it still came in lowercase

I think you would have to process the top line using automation or write the CSV file out 'manually'
by building a string and use strtofile() to drop it on the disk.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
TheLazyPig,

You can use the CSVProcessor class at for that kind of requirement.

After downloading/cloning the repository (plus the accessory dependency Namer class), DO CSV.prg and you're ready to go:

Code:
CREATE CURSOR csvTest (Column1 Integer AUTOINC, Column2 Varchar(50))

INSERT INTO csvTest (Column2) VALUES ("First row")
INSERT INTO csvTest (Column2) VALUES ("Second row")
INSERT INTO csvTest (Column2) VALUES ("Third row")

LOCAL CSV AS CsvProcessor

m.CSV = CREATEOBJECT("CSVProcessor")

m.CSV.ValueSeparator = ";"		&& adjust as required

WITH m.CSV.FieldMapping AS Collection

	.Add("Column1", "First Column")
	.Add("Column2", "Second Column")

ENDWITH

m.CSV.Export("csvTest.csv", .T.)

The result, in Excel:

Captura_de_ecr%C3%A3_2021-03-08_083136_p4mqet.png
 
Although COPY TO ... TYPE CSV puts the field names in lower case, I've noticed that the FIELD() function returns the names in caps.

So, you could slightly adapt Atlope's code to make it more generic.

Instead of:

Code:
.Add("Column1", "First Column")
.Add("Column2", "Second Column")

you could do:

Code:
.Add("Column1", FIELD(1))
.Add("Column2", FIELD(2))

Mike

_________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hmmm

Code:
USE MyTable
COPY TO D:\$INCOMING\MyCSV.CSV CSV FIELDS NAME,DESC
m.STRING = FILETOSTR("d:\$incoming\MyCSV.csv")
m.FIELDSTR = UPPER(MLINE(m.STRING,1))+CHR(13)+CHR(10)
m.NEWFILE = m.FIELDSTR+CHR(13)+CHR(10)
FOR I = 2 TO MEMLINES(m.STRING)
	m.FIELDSTR = m.FIELDSTR+MLINE(M.STRING,I)+CHR(13)+CHR(10)
NEXT
STRTOFILE(m.FIELDSTR ,"d:\$incoming\MyCSV.csv")

Might be pretty quick

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Mike,

Mike Lewis said:
So, you could slightly adapt Atlope's code to make it more generic.

Actually, not mapping the field at all would produce that result. I was just pointing out that using the class would give a fair amount of control on the name of columns, including mixed casing.

Code:
CREATE CURSOR csvTest (Column1 Integer AUTOINC, Column2 Varchar(50))

INSERT INTO csvTest (Column2) VALUES ("First row")
INSERT INTO csvTest (Column2) VALUES ("Second row")
INSERT INTO csvTest (Column2) VALUES ("Third row")

LOCAL CSV AS CsvProcessor

m.CSV = CREATEOBJECT("CSVProcessor")

m.CSV.Export("csvTest.csv", .T.)

For complete upper casing, this would do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top