simplexlsxgen
simplexlsxgen copied to clipboard
download excel in wordpress ajax
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');
});
Try save XLSX to public folder and return url as result. Then document.location = result.xlsx_url
or see https://github.com/shuchkin/simplexlsxgen?tab=readme-ov-file#js-array-to-excel-ajax