Welcome Guest, Not a member yet? Register   Sign In
Download excel file with ajax
#1

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.
Reply
Reply
#3

Thank you fenzy
Reply
#4

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.
Reply
#5

(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...
Reply
#6

(This post was last modified: 09-23-2018, 09:43 AM by Wouter60.)

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"');?>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB