CodeIgniter Forums
Download excel file with ajax - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Download excel file with ajax (/showthread.php?tid=71776)



Download excel file with ajax - demo - 09-22-2018

Hi to all, i'm new there and i have a big problem, i hope find answer here.
So i want to generate xsl file from database and download without reload page.

So this is my controller:

PHP Code:
   public function export()
 
   {
 
       $this->load->library("excel");
 
       $rowscount $this->Public->Count();
 
       $object = new PHPExcel();

 
       $object->setActiveSheetIndex(0);

 
       $table_columns = array("Name");

 
       $column 0;

 
       foreach($table_columns as $field)
 
       {
 
           $object->getActiveSheet()->setCellValueByColumnAndRow($column1$field);
 
           $column++;
 
       }

 
       $users $this->Public->export();

 
       $excel_row 2;

 
       foreach($users as $row)
 
       {
 
           $object->getActiveSheet()->setCellValueByColumnAndRow(0$excel_row$row->username);
 
           $excel_row++;
 
       }

 
       $object_writer PHPExcel_IOFactory::createWriter($object'Excel5');
 
       header("Content-Type: application/vnd.ms-excel");
 
       header("Content-Disposition: attachment;filename=Export.xls");
 
       $object_writer->save('php://output');
 
   
This is form in view:
PHP Code:
<form role="form" action="<?php echo site_url('admin/export')?>" method="post">
<
button class="btn btn-warning">Export</button>
 
   </form

This is work correct, but before download start the current page reloaded. I want with ajax make download form work without reload page.

Any hint how to do that?

Regards.


RE: Download excel file with ajax - fenzy - 09-23-2018

Refer to http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/


RE: Download excel file with ajax - demo - 09-23-2018

Thank you fenzy


RE: Download excel file with ajax - Wouter60 - 09-23-2018

Don't use a form at all. Just a hyperlink on your page that says "Download" or "Export". The hyperlink must refer to the controller/function that generates the Excel file and forces the download.
In that case, the current page will stay active.
And you don't need AJAX.


RE: Download excel file with ajax - demo - 09-23-2018

(09-23-2018, 05:19 AM)Wouter60 Wrote: Don't use a form at all. Just a hyperlink on your page that says "Download" or "Export". The hyperlink must refer to the controller/function that generates the Excel file and forces the download.
In that case, the current page will stay active.
And you don't need AJAX.

Thanks for your solution i do this:

Code:
<a href="<?php echo site_url('admin/export')?>" class="btn btn-warning btn-sm"><i class="fa fa-file-excel-o"></i> <?php echo $this->lang->line('export'); ?></a>

and work, but page page reload also...


RE: Download excel file with ajax - Wouter60 - 09-23-2018

For me, this works:
Code:
header('Content-Disposition: attachment;filename="Export.xls"');

And you can simplify the url with this:
PHP Code:
<?= anchor('admin/export','<i class="fa fa-file-excel-o"></i>' $this->lang->line('export') ,'class="btn btn-warning btn-sm"');?>