Excel Export Customization
The Grid provides extensive Excel export customization capabilities that allow you to modify the appearance, structure, and content of the exported files to meet specific business requirements.
This article demonstrates how to customize various aspects of the Excel export, including formatting, columns, data management, and advanced scenarios.
Common Customization Patterns
The Grid's Excel export functionality is powered by two main components:
- The excelExport Event—Triggered before the export begins, allowing you to access and modify the workbook object.
- The Workbook API—Provides a rich set of configuration options to customize sheets, rows, columns, and cells.
Accessing the Workbook
The excelExport event provides access to the workbook configuration object that will be used to generate the Excel file.
$("#grid").kendoGrid({
toolbar: ["excel"],
excelExport: function(e) {
var workbook = e.workbook;
var sheet = workbook.sheets[0];
// Access rows and cells
var rows = sheet.rows;
var headerRow = rows[0];
}
});
Modifying Cell Properties
You can customize the appearance of cells by setting various properties such as background color, text color, font styles, and number formats. For a complete list of the available cell properties, refer to the Excel Appearance documentation.
$("#grid").kendoGrid({
toolbar: ["excel"],
excelExport: function(e) {
var sheet = e.workbook.sheets[0];
// Format a specific cell
sheet.rows[1].cells[0].background = "#ffff00";
sheet.rows[1].cells[0].color = "#000000";
sheet.rows[1].cells[0].bold = true;
sheet.rows[1].cells[0].format = "0.00";
}
});
Preventing Default Export
You can prevent the default export behavior to implement custom logic before saving the file.
$("#grid").kendoGrid({
toolbar: ["excel"],
excelExport: function(e) {
// Prevent default export to apply custom logic
e.preventDefault();
// Perform custom operations
// ... your code
// Manually save
kendo.saveAs({
dataURI: new kendo.ooxml.Workbook(e.workbook).toDataURL(),
fileName: "CustomExport.xlsx"
});
}
});
Customization Scenarios
The following sections demonstrate common customization scenarios for Grid Excel export. Each scenario addresses specific requirements and provides working examples.
Basic Formatting
Customize the visual appearance of exported data including cell formats, colors, fonts, and styling.
Cell and Date Formatting
- Format Cell Values—Apply custom number formats, currency, percentages, and other cell value formats in the exported Excel file.
- Format Date Cells—Configure proper date formatting for date columns to ensure dates display correctly in Excel.
Visual Styling
- Alternating Row Colors—Set background colors for alternating rows to improve readability of exported data.
- Align Footer Cells—Control the horizontal alignment of footer cells in the Excel export.
- Apply Custom Fonts—Change the font family, size, and style of cells in the exported document.
- Strikethrough Text Styling—Add strikethrough formatting to specific cells or rows.
- Set Pattern Style Colors—Apply background pattern styles and colors to cells.
Layout
- Set Row Height to Auto—Configure automatic row height adjustment based on content.
- Handle New Lines—Properly display multi-line text content in Excel cells.
Column Customization
Control which columns are exported and how they appear in the Excel file.
Column Selection and Visibility
- Use Column Templates—Export column template values instead of raw data to maintain custom formatting.
- Export Selected Columns Only—Allow users to choose which columns to include in the export.
- Include Hidden Columns—Export columns that are hidden in the Grid UI.
Column Manipulation
- Add Columns During Export—Insert new columns at specific positions in the exported file.
- Insert New Columns—Add additional columns with custom data during the export process.
- Format Sheet Columns—Configure column widths, alignment, and other formatting options.
- Auto-fit Columns—Automatically adjust column widths to fit content.
Data Filtering and Selection
Manage which data gets exported, including filtered datasets and partial data exports.
Filtering
- Export Filtered Data (All Pages)—Export only the filtered data across all pages, respecting current filter criteria.
- Export Filtered Data from Dynamic Grids—Handle Excel export for grids with dynamically generated columns and filtered data.
Data Range Selection
- Export Entire Dataset—Export all data from the data source, regardless of pagination or current view.
- Limit Exported Rows—Restrict the number of rows included in the export.
- Insert Additional Rows—Add custom rows (headers, footers, summaries) to the exported file.
Hierarchical and Grouped Data
Export complex data structures including master-detail relationships and grouped data.
Master-Detail Exports
- Export Master and Detail Grids—Combine master and detail Grid data into a single Excel workbook.
- Export Hierarchy to Separate Sheets—Place master and detail data on different Excel sheets for better organization.
Grouped Data
- Export with Group Paging—Handle Excel export for grids using group paging functionality.
- Export Each Group to Separate Sheets—Create individual Excel sheets for each data group.
Advanced Content
Export special content types including images, hyperlinks, formulas, and HTML content.
Rich Content Types
- Export Images—Include image data in the exported Excel file.
- Export Clickable Hyperlinks—Make URL columns clickable in the exported Excel document.
- Add Excel Formulas—Insert Excel formulas into cells for calculations within the exported file.
Content Processing
- Export Text from HTML Content—Strip HTML tags and export only text content when cells contain HTML.
- Custom Headers—Add custom header rows with merged cells, titles, or branding.
Export Configuration
Customize the export process, file naming, and user interactions.
File Management
- Customize Export Filename—Dynamically set the Excel filename including dates, timestamps, or user information.
- Export with User Confirmation—Require user confirmation before proceeding with the export.
Advanced Configuration
- Modify Exported Excel Document—Apply comprehensive modifications to the workbook structure and content.
- Export Page Content with Grid—Include additional page elements and content alongside the Grid data.
- Server-Side Export—Implement Excel export processing on the server side.
- Handle Proxy Issues—Troubleshoot and resolve proxy server issues during export.