Search results

Excel Export

The Excel export allows Pivot Grid data to export as Excel document. To enable Excel export in the pivot grid, set the allowExcelExport as true. You need to use the excelExport method for Excel exporting.

razor
Export.cs
@Html.EJS().Button("excel").Content("Export To Excel").IsPrimary(true).Render()

@Html.EJS().PivotView("PivotGrid").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false)
.FormatSettings(formatsettings =>
{
formatsettings.Name("Amount").Format("C0").MaximumSignificantDigits(10).MinimumSignificantDigits(1).UseGrouping(true).Add();
}).Rows(rows =>
{
rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
columns.Name("Year").Caption("Production Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).AllowExcelExport(true).Render()

<script>
    var pivotGridObj;
    document.getElementById('excel').onclick = function () {
        pivotGridObj = document.getElementById('PivotGrid').ej2_instances[0];
        pivotGridObj.excelExport();
    }
</script>
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.data = data;
    return View();
}

Multiple pivot grid exporting

The Excel export provides an option to export multiple pivot grid data in the same Excel file.

Same WorkSheet

The Excel export provides support to export multiple pivot grids in same sheet. To export in same sheet, define multipleExport.type as AppendToSheet in excelExportProperties. It has an option to provide blank rows between pivot grids and these blank row(s) count can be defined using themultipleExport.blankRows property.

By default, multipleExport.blankRows value is 5 between pivot grid’s within the same sheet.

razor
SameSheetExport.cs
@Html.EJS().Button("excel").Content("Export To Excel").IsPrimary(true).Render()

@Html.EJS().PivotView("PivotGrid").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false)
.FormatSettings(formatsettings =>
{
    formatsettings.Name("Amount").Format("C0").MaximumSignificantDigits(10).MinimumSignificantDigits(1).UseGrouping(true).Add();
}).Rows(rows =>
{
    rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
    columns.Name("Year").Caption("Production Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).AllowExcelExport(true).Render()
<br />
@Html.EJS().PivotView("PivotGrid2").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false)
.FormatSettings(formatsettings =>
{
    formatsettings.Name("Amount").Format("C0").MaximumSignificantDigits(10).MinimumSignificantDigits(1).UseGrouping(true).Add();
}).Columns(columns =>
{
    columns.Name("Country").Add(); columns.Name("Products").Add();
}).Rows(rows =>
{
    rows.Name("Year").Caption("Production Year").Add(); rows.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).AllowExcelExport(true).Render()

<script>
    var pivotGridObj; var pivotGridObj2;
    document.getElementById('excel').onclick = function () {
        pivotGridObj = document.getElementById('PivotGrid').ej2_instances[0];
        pivotGridObj2 = document.getElementById('PivotGrid2').ej2_instances[0];

        var excelExportProperties = {
            multipleExport: { type: 'AppendToSheet', blankRows: 2 }
        };
        var firstGridExport = pivotGridObj.grid.excelExport(excelExportProperties, true);
        firstGridExport.then(function (fData) {
            pivotGridObj2.excelExport(excelExportProperties, false, fData);
        });
    }
</script>
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.data = data;
    return View();
}

New WorkSheet

Excel export provides support to export multiple pivot grids into new sheets. To export in new sheets, define multipleExport.type as NewSheet in excelExportProperties.

razor
NewSheetExport.cs
@Html.EJS().Button("excel").Content("Export To Excel").IsPrimary(true).Render()

@Html.EJS().PivotView("PivotGrid").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false)
.FormatSettings(formatsettings =>
{
    formatsettings.Name("Amount").Format("C0").MaximumSignificantDigits(10).MinimumSignificantDigits(1).UseGrouping(true).Add();
}).Rows(rows =>
{
    rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
    columns.Name("Year").Caption("Production Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).AllowExcelExport(true).Render()
<br />
@Html.EJS().PivotView("PivotGrid2").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false)
.FormatSettings(formatsettings =>
{
    formatsettings.Name("Amount").Format("C0").MaximumSignificantDigits(10).MinimumSignificantDigits(1).UseGrouping(true).Add();
}).Columns(columns =>
{
    columns.Name("Country").Add(); columns.Name("Products").Add();
}).Rows(rows =>
{
    rows.Name("Year").Caption("Production Year").Add(); rows.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).AllowExcelExport(true).Render()

<script>
    var pivotGridObj; var pivotGridObj2;
    document.getElementById('excel').onclick = function () {
        pivotGridObj = document.getElementById('PivotGrid').ej2_instances[0];
        pivotGridObj2 = document.getElementById('PivotGrid2').ej2_instances[0];

        var excelExportProperties = {
            multipleExport: { type: 'NewSheet' }
        };
        var firstGridExport = pivotGridObj.grid.excelExport(excelExportProperties, true);
        firstGridExport.then(function (fData) {
            pivotGridObj2.excelExport(excelExportProperties, false, fData);
        });
    }
</script>
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.data = data;
    return View();
}

Customization during excel export

The Excel export provides an option to customize mapping of the pivot grid to excel document.

Theme

The Excel export provides an option to include theme for exported Excel document.

To apply theme in exported Excel, define the theme in excelExportProperties.

By default, material theme is applied to exported Excel document.

razor
ThemeExport.cs
@Html.EJS().Button("excel").Content("Export To Excel").IsPrimary(true).Render()

@Html.EJS().PivotView("PivotGrid").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false)
.FormatSettings(formatsettings =>
{
    formatsettings.Name("Amount").Format("C0").MaximumSignificantDigits(10).MinimumSignificantDigits(1).UseGrouping(true).Add();
}).Rows(rows =>
{
    rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
    columns.Name("Year").Caption("Production Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).AllowExcelExport(true).Render()

<script>
    var pivotGridObj; var pivotGridObj2;
    document.getElementById('excel').onclick = function () {
        pivotGridObj = document.getElementById('PivotGrid').ej2_instances[0];
        var excelExportProperties = {
            theme: {
                header: { fontName: 'Segoe UI', fontColor: '#666666' },
                record: { fontName: 'Segoe UI', fontColor: '#666666' },
                caption: { fontName: 'Segoe UI', fontColor: '#666666' }
            }
        };
        pivotGridObj.excelExport(excelExportProperties);
    }
</script>
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.data = data;
    return View();
}

The Excel export provides an option to include header and footer content for the exported Excel document.

razor
@Html.EJS().Button("excel").Content("Export To Excel").IsPrimary(true).Render()

@Html.EJS().PivotView("PivotGrid").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false)
.FormatSettings(formatsettings =>
{
    formatsettings.Name("Amount").Format("C0").MaximumSignificantDigits(10).MinimumSignificantDigits(1).UseGrouping(true).Add();
}).Rows(rows =>
{
    rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
    columns.Name("Year").Caption("Production Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).AllowExcelExport(true).Render()

<script>
    var pivotGridObj; var pivotGridObj2;
    document.getElementById('excel').onclick = function () {
        pivotGridObj = document.getElementById('PivotGrid').ej2_instances[0];
        var excelExportProperties = {
            header: {
                headerRows: 2,
                rows: [
                    { cells: [{ colSpan: 4, value: "Pivot Grid", style: { fontColor: '#C67878', fontSize: 20, hAlign: 'Center', bold: true, underline: true } }] }
                ]
            },
            footer: {
                footerRows: 4,
                rows: [
                    { cells: [{ colSpan: 4, value: "Thank you for your business!", style: { hAlign: 'Center', bold: true } }] },
                    { cells: [{ colSpan: 4, value: "!Visit Again!", style: { hAlign: 'Center', bold: true } }] }
                ]
            }
        };
        pivotGridObj.excelExport(excelExportProperties);
    }
</script>

CSV Export

Also, the Excel export allows Pivot Grid data to be exported in CSV file format. To export pivot grid in CSV file format, you need to use the csvExport method.

razor
CSVExport.cs
@Html.EJS().Button("excel").Content("Export To Excel").IsPrimary(true).Render()

@Html.EJS().PivotView("PivotGrid").Height("300").DataSource(dataSource => dataSource.Data((IEnumerable<object>)ViewBag.Data).ExpandAll(false)
.FormatSettings(formatsettings =>
{
    formatsettings.Name("Amount").Format("C0").MaximumSignificantDigits(10).MinimumSignificantDigits(1).UseGrouping(true).Add();
}).Rows(rows =>
{
    rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
    columns.Name("Year").Caption("Production Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).AllowExcelExport(true).Render()

<script>
    var pivotGridObj; var pivotGridObj2;
    document.getElementById('excel').onclick = function () {
        pivotGridObj = document.getElementById('PivotGrid').ej2_instances[0];
        pivotGridObj.csvExport();
    }
</script>
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.data = data;
    return View();
}

See Also