Welcome Guest, Not a member yet? Register   Sign In
Export to excel
#1

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
Reply
#2

This may help you out.

How to Export Data in Excel using Codeigniter 4
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Hi, the project  is in CI 3
Reply
#4

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

How to Generate Excel File in Codeigniter using PHPExcel
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

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');


    
Reply
#6

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
    }
     
Reply
#7

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

https://github.com/mk-j/PHP_XLSXWriter
Reply
#8

(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 ?
Reply
#9

(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 ?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB