Excel export - Freeze headers and expand to fit?

We're getting request from our users that they'd like to have the headers of the excel export be frozen (so when they scroll they can still see the headers). Additionally, they would like all the cell widths to expand to fit the longest text. Is there any way to accomplish these in the excel ouput? I was not able to find an answer in the documentation. Thanks.

4 Replies

MS Manivel Sellamuthu Syncfusion Team June 3, 2020 01:11 PM UTC

Hi Ryan, 

Greetings from Syncfusion support. 

Query: We're getting request from our users that they'd like to have the headers of the excel export be frozen (so when they scroll they can still see the headers) 

You can freeze a portion of the sheet to keep it visible while you scroll through the rest of the sheet. Please refer the below code example and sample for more information. 

[App.component.ts] 
 
   public toolbarClick(args: ClickEventArgs): void { 
        switch (args.item.text) {           
            case 'Excel Export': 
       const GridExportPromise<any> = this.grid.excelExport({}, true); 
       GridExport.then((workbook: any) => { 
// here we can access the workbook and set the number of rows should be freeze 
       workbook.worksheets[0].freeze = { row: 1 }; 
       const book: Workbook = new Workbook(workbook, 'xlsx'); 
           book.save('Export.xlsx'); 
        }); 
        } 
    } 

 

Query: they would like all the cell widths to expand to fit the longest text. Is there any way to accomplish these in the excel output? 
 
Excel export don’t have support for fit the cell widths to based on the cell content. However the width of the excel columns are set based on the Grid columns width.  
So if we have applied autofit in the Grid, It will be accomplished in the exported file too. Please refer the below code example and sample  and screenshot 0for more information. 

[App.component.html] 
<div class="control-section"> 
    <ejs-grid #grid [dataSource]='data' (dataBound)="databound($event)" allowPaging='true'  
height=400 [toolbar]='toolbar' (toolbarClick)='toolbarClick($event)' [allowExcelExport]='true'> 
 . . . 
    </ejs-grid> 
</div> 
 
[App.component.ts] 
    public databound () { 
      var sum = 0; 
      this.grid.autoFitColumns(); 
      let columns = this.grid.getColumns(); 
      for (var i = 0; i< columns.length; i++) { 
        sum = sum + parseInt(columns[i].width as any); 
      } 
      this.grid.width = sum; 
    } 

 


 
 
 
Please let us know, if you need further assistance. 

Regards, 
Manivel 



RY Ryan July 6, 2020 09:11 PM UTC

Hi Manivel, really appreciate the examples and direction for this. The frozen headers technique worked almost 100% but unfortunately our footer kept getting removed. Luckily our footer was just an aggregate row so we just used the custom aggregate of the aggregation feature of the grid to get our "footer" data back. So no harm there. It's been awhile, but I do believe I was able to get the footer to appear if I called this.grid.excelExport({}, false); however then the browser bugged the user about downloading multiple files and I didn't want the extra notification for our users. Regardless, wouldn't have been able to figure this out on my own so I appreciate that. It'd would be very cool if you guys could offer a better interface to control the freezing of headers in the excel library in a future release.

The autoFitColumns() method did not work for our implementation (for us the grid is one data source and the excel export is another data source), but good suggestion regardless. We ended up rolling our own quick-and-dirty resizing logic to fit the data by manipulating the ExcelExportProperties.Columns[i].width property of each column.



MS Manivel Sellamuthu Syncfusion Team July 7, 2020 04:12 AM UTC

Hi Ryan, 

Thanks for your update. 

We are glad that your issue has been resolved. We will validate and consider your suggestion to our improvements. 

Please let us know, if you need further assistance. 

Regards, 
Manivel 



ED edwards May 6, 2021 01:26 PM UTC

There are many tricks to export data to Excel without any hassle but I want to share some easy and effective ones with you.

Method #1: Using Export Wizard

Method #2: Exporting Data To Excel From A Button

Method 3# Exporting Report Data To Excel

Method 4# Export SQL Data To Excel

To get detail about these methods of exporting data to Excel read this post:  Export Data To Excel



Loader.
Up arrow icon