Qlik Community

Qlik Sense App Development

Discussion board where members can learn more about Qlik Sense App Development and Usage.

Highlighted
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
Contributor II

Re: Export to TXT/CSV with customized header

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+
3 Replies
albert_guito
Contributor II

Re: Export to TXT/CSV with customized header

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
Contributor II

Re: Export to TXT/CSV with customized header

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+
Employee
Employee

Re: Export to TXT/CSV with customized header

Thanks, Albert! excellent solution!