material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Custom Export Dropdown

Open vozian opened this issue 6 years ago • 4 comments

Hello.

I have implemented custom exporting in 2 formats (csv and excel).

Is there a way to add another option ("Export as Excel") to export dropdown or at least customize the "Export as CSV" text?

Or maybe to override the entire thing?

Hope you can help. Thanks!

vozian avatar Jan 16 '20 20:01 vozian

Hello.

I have implemented custom exporting in 2 formats (csv and excel).

Is there a way to add another option ("Export as Excel") to export dropdown or at least customize the "Export as CSV" text?

Or maybe to override the entire thing?

Hope you can help. Thanks!

You can customize the "Export as CSV" text by just passing below code in MaterialTable component :-

localization={{
            toolbar: {
              exportName: 'Export'
        }
      }}

and i have one question, how you added custom exporting in two format,can you share the code please.

therohitjha avatar Jan 17 '20 12:01 therohitjha

i would like to see the code as well.

karanness86 avatar Mar 04 '20 20:03 karanness86

I'm using SheetJS for this + exportButton/exportCsv functionality of material-table.

import XLSX from 'xlsx';
// ...
const exportToExcel = (columns, data) => {
    // determining header labels
    const columnInfo = columns.reduce(
        (acc, column) => {
            const headerLabel = column.title || column.field;
            acc.header.push(headerLabel);
            acc.map[column.field] = headerLabel;
            return acc;
        },
        { map: {}, header: [] }
    );
    // This is now columnInfo:
    // {
    //     "columnInfo": {
    //         "map": {
    //             "a": "Nice name for a",
    //             "b": "Nice name for b"
    //         },
    //         "header": ["Nice name for a", "Nice name for b"]
    //     }
    // } 

    // Translate the keys of each row object to the new header label
    // Turning `{a: 1, b: 2, c: 3}` into `{"Nice name for a": 1, "Nice name for b": 2}`.
    const mappedData = data.map(row =>
        Object.entries(row).reduce((acc, [key, value]) => {
            if (columnInfo.map[key]) {
                acc[columnInfo.map[key]] = value;
            }
            return acc;
        }, {})
    );

    // https://github.com/SheetJS/sheetjs/issues/817
    var ws = XLSX.utils.json_to_sheet(mappedData, {
        header: columnInfo.header, // garanties the column order same as in material-table
    });

    /* add to workbook */
    var wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

    /* generate an XLSX file and triggers the browser to generate the download*/
    XLSX.writeFile(wb, `MyExcelSheet.xlsx`);
};

// ...
const MyComponent = ({ refRable }) => {
    const data = [{a: 1, b: 2, c: 3}, {...}, ..., {...}]

    const columns = [
        {
            field: 'a',
            title: 'Nice name for a',
        },
        {
            field: 'b',
            title: 'Nice name for b',
        },
    ];

    return (
        <MaterialTable
            columns={columns}
            data={data}
            options={{
                exportButton: true,
                exportCsv: exportToExcel,
            }}
            localization={{
                toolbar: {
                    exportTitle: 'Download',
                    exportAriaLabel: 'Download',
                    exportName: 'Download als Excel',
                },
            }}
        />
};

christiaanwesterbeek avatar Mar 20 '20 09:03 christiaanwesterbeek

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Jul 05 '20 19:07 stale[bot]