Skip to main content
Announcements
NEW: Seamless Public Data Sharing with Qlik's New Anonymous Access Capability: TELL ME MORE!
cancel
Showing results for 
Search instead for 
Did you mean: 
qhe
Employee
Employee

Export to TXT/CSV with customized header

Hi scripting experts, I have below the table, need to export to csv/txt with a specific header. Any thoughts? 

 

Input:

Customer, Sales

A,100

B,200

C,300

D,400

E,500

 

 

Required output in txt/csv:

Total rows: 5
A,100
B,200
C,300
D,400
E,500

 

 

 

Not able to upload a qvf, so I attached the codes:

Table1:

LOAD * INLINE [
Customer, Sales
A,100
B,200
C,300
D,400
E,500
];

Store Table1 into [lib://Data/Table1.txt](txt, delimiter is '|');

 

Labels (2)
1 Solution

Accepted Solutions
albert_guito
Creator II
Creator II

If you just use a little number of columns this would be a perfect solution:

Table1:
LOAD Customer & '|' & Sales as Reg INLINE [Customer, Sales
A,100
B,200
C,300
D,400
E,500
];

Header: Load 'Total rows: ' & Count(Reg) as numReg Resident Table1; 

LET vLabelColumn1 = peek('numReg',0,'Header');

TableToExport: LOAD PurgeChar(Reg,'"')	as '$(vLabelColumn1)' RESIDENT Table1;

DROP TABLE Table1, Header;

Store TableToExport into [Table1.txt](txt);

The result will be:

Total rows: 5
A|100
B|200
C|300
D|400
E|500
Ag+

View solution in original post

3 Replies
albert_guito
Creator II
Creator II

Hi,

You can try with this code:

Table1:
LOAD * INLINE [Customer, Sales
A,100
B,200
C,300
D,400
E,500
];

Header: Load 'Total rows: ' & Count(Customer) as numReg Resident Table1; 

let vLabelColumn1 = peek('numReg',0,'Header');
let vLabelColumn2 = ' ';

TableToExport:
LOAD 
	Customer as '$(vLabelColumn1)',
	Sales as '$(vLabelColumn2)'
RESIDENT Table1;

DROP TABLE Table1, Header;

Store TableToExport into [Table1.txt](txt, delimiter is '|');

But the solution isn't perfect because it includes the delimiter in the header 😞

Total rows: 5| 
A|100
B|200
C|300
D|400
E|500
Ag+
albert_guito
Creator II
Creator II

If you just use a little number of columns this would be a perfect solution:

Table1:
LOAD Customer & '|' & Sales as Reg INLINE [Customer, Sales
A,100
B,200
C,300
D,400
E,500
];

Header: Load 'Total rows: ' & Count(Reg) as numReg Resident Table1; 

LET vLabelColumn1 = peek('numReg',0,'Header');

TableToExport: LOAD PurgeChar(Reg,'"')	as '$(vLabelColumn1)' RESIDENT Table1;

DROP TABLE Table1, Header;

Store TableToExport into [Table1.txt](txt);

The result will be:

Total rows: 5
A|100
B|200
C|300
D|400
E|500
Ag+
qhe
Employee
Employee
Author

Thanks, Albert! excellent solution!