This is a migrated thread and some comments may be shown as answers.

Export to excel with dynamic columns

6 Answers 961 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Lior
Top achievements
Rank 1
Lior asked on 14 Aug 2018, 03:11 PM

Hi, 

I'm trying to export my grid to excel. I'm dynamically building the columns, but they don't get exported unless I hardcode them. 

let columns = [],
        columnNames = [];
        if (!this.state.gridColumns) return;
        this.state.gridColumns.forEach(
            (column, index) => {
                columns.push(
                    <ExcelExportColumn key={index} field={column.name} title={column.name}/>
                );
                columnNames.push(column.name);
            }
        );
 
        return (
            <ExcelExport
                columns={columnNames}
                data={this.state.gridData}
                fileName="test.xlsx"
                ref={(exporter) => {
                    this._exporter = exporter;
                }}
 
            >
                <button
                    title="Export Excel"
                    className="k-button k-primary"
                    onClick={this.export}
                >
                    Export to Excel
                </button>
                
            </ExcelExport>

6 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 15 Aug 2018, 06:32 AM
Hello, Lior,

Thank you for the code snippet.

When the columns have to be passed when exporting, we can suggest passing them to the save function.

I made an example demonstrating how to export only some of the columns based on a columns array:

https://stackblitz.com/edit/react-jgpiuv?file=app/main.js

I hope this is helpful.

Regards,
Stefan
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Lior
Top achievements
Rank 1
answered on 15 Aug 2018, 07:16 AM

Thank you very much! 

I also understand that I need to have have the data prior to rendering the grid.

0
Jon
Top achievements
Rank 1
answered on 22 Jan 2019, 08:44 PM

Hello,

I was hoping you could shed some light on what you meant by "need to have the data prior to rendering the Grid". I am having an issue where only my columns are showing up in my Excel export but there is no data in the rows.

 

For context: my Grid is rendered/updated somewhat dynamically.

1. On componentDidMount, API GET request brings back my dataset

2. The Grid is empty at first

3. The user interacts with a Slider and/or DatePicker which applies a custom filter to the dataset

4. A subset of data is then rendered onto the Grid.

5. The user can dynamically change the subset of data returned by moving the slider or changing the date in the DatePicker.

Is my particular Grid implementation not going to work with ExcelExport ?

0
Jon
Top achievements
Rank 1
answered on 22 Jan 2019, 09:09 PM
My issue might have been caused by a second ExcelExport that was for a different Grid and a conflicting name. Seems to work now.
0
Stefan
Telerik team
answered on 23 Jan 2019, 07:10 AM
Hello, Esteban,

I`m glad to hear that the issue is resolved.

I will also provide more details on the question. The excel export does not require the Grid to have any data before rendering. If the data is not available at the moment of export, for example, if the user clicks export when the Grid is empty, the exported file will be empty as well.

Regards,
Stefan
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Jon
Top achievements
Rank 1
answered on 23 Jan 2019, 02:20 PM
Thanks for the followup, Stefan. That makes sense to me.
Tags
General Discussions
Asked by
Lior
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Lior
Top achievements
Rank 1
Jon
Top achievements
Rank 1
Share this question
or