We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Custom name for each excel tab

Thread ID:

Created:

Updated:

Platform:

Replies:

151249 Feb 5,2020 06:25 AM UTC Mar 3,2020 01:10 PM UTC Angular - EJ 2 3
loading
Tags: Grid
Arjuna Dratharashtra
Asked On February 5, 2020 06:25 AM UTC

I would like to have meaningful names for each sheet when I export multiple grids in the same excel file. Currently they name as Sheet1, Sheet2 etc. I want them to have custom names. Please help.

Prasanna Kumar Viswanathan [Syncfusion]
Replied On February 6, 2020 11:45 AM UTC

Hi Arjuna , 
 
Greetings from Syncfusion support 
 
Query : Custom name for each excel tab 
 
To achieve this query we suggest to use the Workbook concept in Excel export. Here, we can change the excel tab name using the Name property in Workbook concept. Please follow the below code snippet and sample for more reference. 
 
App.component.ts 

import { Component, OnInit, ViewChild } from '@angular/core'; 
import { Workbook } from '@syncfusion/ej2-excel-export'; 

@Component({ 
    selector: 'app-root', 
    template: `<p><b>First Grid:</b></p> 
    <ejs-grid #grid1 id='firstGrid' [dataSource]='fData' [toolbar]='toolbarOptions' [allowExcelExport]='true' 
    (toolbarClick)='toolbarClick($event)'> 
                <e-columns> 
                            
                </e-columns> 
                </ejs-grid> 
                <p><b>Second Grid:</b></p> 
                <ejs-grid #grid2 id='secondGrid' [dataSource]='sData' [allowExcelExport]='true'> 
                <e-columns> 
                 … 
                </e-columns> 
                </ejs-grid> 
                ` 
}) 
export class AppComponent implements OnInit { 

    public fData: object[]; 
    public sData: object[]; 
    public names: any ; 

    ngOnInit(): void { 
        this.fData = data.slice(0, 5); 
        this.names = ["OrderDetail", "EmployeeDetail"];    // here you can customize the sheet names 
         
    } 

    toolbarClick = (args: ClickEventArgs) => { 
      var firstGrid = (document.getElementById("firstGrid") as any).ej2_instances[0]; 
        if (args.item.id === 'firstGrid_excelexport') { 
      var appendExcelExportProperties = { 
      multipleExport: { type: 'NewSheet' } 
    }; 
    var Grids = []; 
    for (var i = 0; i < document.querySelectorAll(".e-grid").length; i++) { 
      var grid = (document.getElementById(document.querySelectorAll(".e-grid")[i].id) as any).ej2_instances[0]; 
      Grids.push(grid); 
    } 
    if (Grids.length > 1) { 
      (appendExcelExportProperties.header.rows[0].cells[0] as any).value = this.names[0]; 
      var firstGridExport = firstGrid.excelExport(appendExcelExportProperties, true).then(function (fData) { 
        fData.worksheets[0].name = this.names[0]; 
        this.exportData = fData; 
        for (var j = 1; j < Grids.length - 1; j++) { 
          (appendExcelExportProperties.header.rows[0].cells[0] as any).value = this.names[1]; 
          Grids[j].excelExport(appendExcelExportProperties, true, this.exportData).then(function (wb) { 
            (appendExcelExportProperties.header.rows[0].cells[0] as any).value = this.names[wb.worksheets.length]; 
            this.exportData = wb; 
            if (this.exportData.worksheets.length === (Grids.length - 1)) { 
              for (var k = 0; k < this.exportData.worksheets.length; k++) { 
                if (!this.exportData.worksheets[k].name) { 
                  this.exportData.worksheets[k].name = this.names[k];          //Sheet name is changed to the given string values 
               } 
        } 
        var lastGridExport = Grids[Grids.length - 1].excelExport(appendExcelExportProperties, true, this.exportData).then(function (wb) { 
          (appendExcelExportProperties.header.rows[0].cells[0] as any).value = this.names[this.names.length - 1]; 
          wb.worksheets[wb.worksheets.length - 1].name = this.names[Grids.length - 1]; 
          const book = new Workbook(wb, 'xlsx');            //  Stores the changed name as workbook 
          book.save('Export.xlsx');                                       //  Save the stored file in Excel format 
        }.bind(this)); 
      }.bind(this)); 
    } 
    
 
 
 
Please get back to us if you need further assistance 
 
Regards 
Prasanna Kumar N.S.V 


Arjuna Dratharashtra
Replied On March 2, 2020 05:39 PM UTC

Thank you very much for the solution. Actually used a promise chain from grid.excelExport and pushed them to Workbook object.

Gokul Ramalingam [Syncfusion]
Replied On March 3, 2020 01:10 PM UTC

Hi Arjuna, 
  
We are happy to hear that your issue has been resolved. 
  
Please get back to us if you need further assistance. 
  
Regards, 
Gokul R 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon