简体   繁体   中英

How to create multi line headers in CSV in Angular?

I am creating an 'Export to CSV' button which will export my Tabular data to.csv file

截屏

This is the html for CSV button

<a csvLink class="btn downloadBtn" [headers]="csvHeaders" [data]="csvData">Export to CSV</a>

This is mt typescript for writing csv:

          //CSV File Formating
          //Headers
          //Master Agent
          let csvHead = [];
          for(let service of res.data.retUnderAgt[0].services){
            for(let t of service.txnTypes){
              csvHead.push({label: service.serviceName+" "+t.txnTypeName, key: service.serviceCode+t.txnTypeName})
            }                
          }
          this.csvHeaders = [
            {label:'OrgName',key: 'orgName'},
            ...csvHead,
            {label: 'NGR', key: 'ngr'}
          ]

          //Data
          for(let i of res.data.retUnderAgt){
            let csvDataInner = {} ;
            if(i.orgName){
              csvDataInner['orgName'] = (i.orgName);
            }else if(i.date){}
            else{      
              csvDataInner['orgName'] = "Total";
            }
            for(let j of i.services){
              for(let k of j.txnTypes){
                csvDataInner[j.serviceCode+k.txnTypeName] = (k.amount);
              }
            }
            csvDataInner['ngr']=i.ngr;
            this.csvData.push(csvDataInner)
          } 

And this is what I have so far in csv:

csv 截图

I want this to be in csv:

主代理

Alhough logically, 'Master Agents' is a header, you could generate the extra line by adding it to this.csvData before you start the second for -loop:

this.csvData.push({ orgName : 'Master Agents'});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM