Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download excel in wordpress ajax #154

Open
persianlionn opened this issue Sep 25, 2024 · 2 comments
Open

download excel in wordpress ajax #154

persianlionn opened this issue Sep 25, 2024 · 2 comments

Comments

@persianlionn
Copy link

Hi
I use this plugin in wordpress theme. Everything works fine. But I don't know how to download the file in Ajax.

    $(document).on('click', '#export_to_excel', function (e) {
        e.preventDefault();
        $.ajax({
            url: ajax_url,
            type: 'post',
            cache: false,
            data: {action:'export_to_excel'},
            success: function (response) {
                if (response.status === 'error') {
                } else {
                }
            }
        })
    }) 
add_action('wp_ajax_export_to_excel', function () {
    global $wpdb;
    $table = $wpdb->prefix . 'custom';
    $lists= $wpdb->get_results("SELECT * FROM {$table }", ARRAY_A);
    $data = [];
    foreach ($lists as $list) {
        $data[] = [$lists['name'], $lists['url']];
    }
   SimpleXLSXGen::fromArray($data)->downloadAs('file.xlsx');
});
@shuchkin
Copy link
Owner

Try save XLSX to public folder and return url as result. Then document.location = result.xlsx_url

@shuchkin
Copy link
Owner

or see https://github.com/shuchkin/simplexlsxgen?tab=readme-ov-file#js-array-to-excel-ajax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants