This section describes how to insert hyperlinks in the cells of the exported Excel file.
Exporting Formulas Copy Link
You can insert hyperlinks in the cells of the exported Excel file by outputting an Excel formula containing a Hyperlink function with a URL value you provide. The code below inserts hyperlinks in the Excel export file for all values in the URL column.
<ag-grid-angular
[columnDefs]="columnDefs"
[defaultExcelExportParams]="defaultExcelExportParams"
[excelStyles]="excelStyles"
/* other grid options ... */ />
this.columnDefs = [
{ field: 'company' },
{
field: 'url',
cellClass: 'hyperlinks' // references excel style
}
];
this.defaultExcelExportParams = {
autoConvertFormulas: true,
processCellCallback: params => {
const field = params.column.getColDef().field;
return field === 'url' ? `=HYPERLINK("${params.value}")` : params.value;
}
};
this.excelStyles = [
{
id: 'hyperlinks',
font: {
underline: 'Single',
color: '#358ccb'
}
}
];Note the following:
- The URL column of the grid below has URL values.
- In the exported Excel file, the URL column has active links for these URL values.
import { Component } from "@angular/core";
import { AgGridAngular } from "ag-grid-angular";
import "./styles.css";
import {
CellStyleModule,
ClientSideRowModelModule,
ColDef,
ColGroupDef,
ExcelExportParams,
ExcelStyle,
GridApi,
GridOptions,
GridReadyEvent,
ModuleRegistry,
enableDevValidations,
} from "ag-grid-community";
import {
ColumnMenuModule,
ContextMenuModule,
ExcelExportModule,
} from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
CellStyleModule,
ClientSideRowModelModule,
ExcelExportModule,
ColumnMenuModule,
ContextMenuModule,
]);
@Component({
selector: "my-app",
standalone: true,
imports: [AgGridAngular],
template: `<div class="container">
<div>
<button
(click)="onBtExport()"
style="margin-bottom: 5px; font-weight: bold"
>
Export to Excel
</button>
</div>
<div class="grid-wrapper">
<ag-grid-angular
style="width: 100%; height: 100%;"
[columnDefs]="columnDefs"
[defaultColDef]="defaultColDef"
[defaultExcelExportParams]="defaultExcelExportParams"
[excelStyles]="excelStyles"
[rowData]="rowData"
(gridReady)="onGridReady($event)"
/>
</div>
</div> `,
})
export class AppComponent {
private gridApi!: GridApi;
columnDefs: ColDef[] = [
{ field: "company" },
{ field: "url", cellClass: "hyperlinks" },
];
defaultColDef: ColDef = {
flex: 1,
minWidth: 100,
};
defaultExcelExportParams: ExcelExportParams = {
autoConvertFormulas: true,
processCellCallback: (params) => {
const field = params.column.getColDef().field;
return field === "url" ? `=HYPERLINK("${params.value}")` : params.value;
},
};
excelStyles: ExcelStyle[] = [
{
id: "hyperlinks",
font: {
underline: "Single",
color: "#358ccb",
},
},
];
rowData: any[] | null = [
{ company: "Google", url: "https://www.google.com" },
{ company: "Adobe", url: "https://www.adobe.com" },
{ company: "The New York Times", url: "https://www.nytimes.com" },
{ company: "Twitter", url: "https://www.twitter.com" },
{ company: "StackOverflow", url: "https://stackoverflow.com/" },
{ company: "Reddit", url: "https://www.reddit.com" },
{ company: "GitHub", url: "https://www.github.com" },
{ company: "Microsoft", url: "https://www.microsoft.com" },
{ company: "Gizmodo", url: "https://www.gizmodo.com" },
{ company: "LinkedIN", url: "https://www.linkedin.com" },
];
onBtExport() {
this.gridApi.exportDataAsExcel();
}
onGridReady(params: GridReadyEvent) {
this.gridApi = params.api;
}
}
.container {
display: flex;
flex-direction: column;
height: 100%;
}
.grid-wrapper {
display: flex;
flex: 1 1 0;
}
.grid-wrapper > div {
width: 100%;
height: 100%;
}
import '@angular/compiler';
import { provideHttpClient } from '@angular/common/http';
import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
if (new URLSearchParams(window.location.search).get('prod') !== 'false') {
enableProdMode();
}
const app = bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
});