CodeIgniter Forums
Export to excel - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Export to excel (/showthread.php?tid=79418)



Export to excel - Tajar_Dobro - 06-12-2021

Hi all
How can i export to excel the output from search result?
I have done the export but not the total rows only the results of search.
 Huh


RE: Export to excel - InsiteFX - 06-12-2021

This may help you out.

How to Export Data in Excel using Codeigniter 4


RE: Export to excel - Tajar_Dobro - 06-13-2021

Hi, the project  is in CI 3


RE: Export to excel - InsiteFX - 06-13-2021

Here is the CodeIgniter 3 version from the same web site.

How to Generate Excel File in Codeigniter using PHPExcel


RE: Export to excel - demyr - 06-13-2021

Hi. First you need to retrieve all data then, it is a model work. Then you need a foreach.
I use PhpSpreadSheet to export excel, which seems to be the best for now and let me share a copy of my codes with you:

PHP Code:
function download_invoice($user_id){

    $spreadsheet = new Spreadsheet();
    $sheet $spreadsheet->getActiveSheet();
    $date date('Y-m-d');

    /*set column names*/
    $table_columns = array("Company Name""Address""Email""phone""Invoice Date",  "Product Name""etc""etc");
    $column 1;
    foreach ($table_columns as $field) {
        $sheet->setCellValueByColumnAndRow($column1$field);
        $column++;
    }
    /*end set column names*/

    $invoice_data $this->InvoiceModel->download_invoice($user_id); //get your data from model

    $excel_row 2//now from row 2

    foreach ($invoice_data as $row) {
              $sheet->setCellValueByColumnAndRow(1$excel_row$row->company_name);
              $sheet->setCellValueByColumnAndRow(2$excel_row$row->user_address.'-'.$row->user_country);
              $sheet->setCellValueByColumnAndRow(3$excel_row$row->user_email);
              $sheet->setCellValueByColumnAndRow(4$excel_row$row->uye_phone);
              $sheet->setCellValueByColumnAndRow(5$excel_row$row->invoice_date);
              $sheet->setCellValueByColumnAndRow(6$excel_row$row->product_date);
              $sheet->setCellValueByColumnAndRow(7$excel_row$row->etc);

              $excel_row++;
            }

    $invoice_name 'Invoice-'.$user_id.'-'.$date.'.xls';
    $object_writer = new Xlsx($spreadsheet);
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$invoice_name.'"');
    $object_writer->save('php://output');


    



RE: Export to excel - Tajar_Dobro - 06-14-2021

Well, I have the main query in the DB.
now I want only to export the return from the search. also, PHP excel does not work.
This code exports me all the values whereas I need only the result from the search

    //Expor to Excel
    public function export_csv(){ 
        
         $filename = 'expenses_'.date('Y-m-d').'.csv'
         header("Content-Description: File Transfer"); 
         header("Content-Disposition: attachment; filename=$filename"); 
         header("Content-Type: application/csv; ");
 
         $expenses_data = $this->expenses_model->export_expenses();

         $file = fopen('php://output''w');
 
         $header = array("Nr."
                        "Title"
                        "Date",
                        "Time",
                        "User",
                        "Amount",
                        ); 
         fputcsv($file$header);
         foreach ($expenses_data as $key=>$line){ 
             fputcsv($file,$line); 
         }
         fclose($file); 
         exit
    }
     



RE: Export to excel - suvi - 06-16-2021

I just used PHP_XLSXWriter in my CI4 project with good result.

https://github.com/mk-j/PHP_XLSXWriter


RE: Export to excel - paulkd - 06-16-2021

(06-14-2021, 12:35 AM)Tajar_Dobro Wrote: Well, I have the main query in the DB.
now I want only to export the return from the search. also, PHP excel does not work.
This code exports me all the values whereas I need only the result from the search

    //Expor to Excel
    public function export_csv(){ 
        
         $filename = 'expenses_'.date('Y-m-d').'.csv'
         header("Content-Description: File Transfer"); 
         header("Content-Disposition: attachment; filename=$filename"); 
         header("Content-Type: application/csv; ");
 
         $expenses_data = $this->expenses_model->export_expenses();

         $file = fopen('php://output''w');
 
         $header = array("Nr."
                        "Title"
                        "Date",
                        "Time",
                        "User",
                        "Amount",
                        ); 
         fputcsv($file$header);
         foreach ($expenses_data as $key=>$line){ 
             fputcsv($file,$line); 
         }
         fclose($file); 
         exit
    }
     

Can you show us the search code ?


RE: Export to excel - Tajar_Dobro - 06-18-2021

(06-16-2021, 05:12 AM)Tajar_Dobro Wrote:
PHP Code:
public function search_expense($search=NULL){

 
$keyword $this->input->get('search',TRUE);
        
        
// pagination settings
        $total_rows $this->db->select('*')->from('expenses')->like("expense",$keyword)->get()->num_rows();
        $limit 15;
        $config["base_url"] = base_url("expenses/search");
        $config["total_rows"] = $total_rows;
        $config["per_page"] = $limit;

        $config['next_link'] = '→';
        $config['prev_link'] = '←'
        $config['full_tag_open'] = "<ul class='pagination justify-content-center'>";
        $config['full_tag_close'] = "</ul>"
        $config['num_tag_open'] = '<li class="page-item">';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link">';
        $config['cur_tag_close'] = '</a></li>';
        $config['next_tag_open'] = '<li class="page-item">';
        $config['next_tagl_close'] = '</a></li>';
        $config['prev_tag_open'] = '<li class="page-item">';
        $config['prev_tagl_close'] = '</li>';
        $config['first_tag_open'] = "<li class='page-item disabled'>";
        $config['first_tagl_close'] = "</a></li>";
        $config['last_tag_open'] = '<li class="page-item">';
        $config['last_tagl_close'] = '</a></li>';
        $config['attributes'] = array('class' => 'page-link');

        $this->pagination->initialize($config);
        $header["links"] = $this->pagination->create_links();

        $start $this->uri->segment(3);
        $header['query'] = $this->db->select("*")
        ->from("expenses")
        ->like("title",$keyword)
        ->or_like("user",$keyword)
        ->or_like("amount",$keyword)
        ->limit($limit,$start)
        ->order_by('id','DESC')
        ->get()
        ->result_array();

        $this->load->view('expenses/index'$header);

  

paulkd
(06-14-2021, 12:35 AM)Tajar_Dobro Wrote: Well, I have the main query in the DB.
now I want only to export the return from the search. also, PHP excel does not work.
This code exports me all the values whereas I need only the result from the search

    //Expor to Excel
    public function export_csv(){ 
        
         $filename = 'expenses_'.date('Y-m-d').'.csv'
         header("Content-Description: File Transfer"); 
         header("Content-Disposition: attachment; filename=$filename"); 
         header("Content-Type: application/csv; ");
 
         $expenses_data = $this->expenses_model->export_expenses();

         $file = fopen('php://output''w');
 
         $header = array("Nr."
                        "Title"
                        "Date",
                        "Time",
                        "User",
                        "Amount",
                        ); 
         fputcsv($file$header);
         foreach ($expenses_data as $key=>$line){ 
             fputcsv($file,$line); 
         }
         fclose($file); 
         exit
    }
     

Can you show us the search code ?