2017-08-07 55 views

回答

0

答案是把

exit; 

$objWriter->save('hp://output'); 

例子:

$objWriter->save('hp://output'); 
exit; 
0

检查我的项目,我用Phpexcel

我的视图页面代码(ourproduct.php):

<a href="<?= base_url() ?>ourproduct/download" class="btn btn-success" >Download .xls file</a> 

我的控制器页(ourproduct.php):

public function download() { 
    $subscribers = $this->base_model->get_products(); 
    require_once APPPATH . '/third_party/Phpexcel/Bootstrap.php'; 
    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); 
    $spreadsheet->getProperties()->setCreator('Webeasystep.com ') 
      ->setLastModifiedBy('Ahmed Fakhr') 
      ->setTitle('Phpecxel codeigniter tutorial') 
      ->setSubject('integrate codeigniter with PhpExcel') 
      ->setDescription('this is the file test'); 
    $styleArray = array(
      'font' => array(
        'bold' => true, 
      ), 
      'alignment' => array(
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, 
        'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, 
      ), 
      'borders' => array(
        'top' => array(
          'style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, 
        ), 
      ), 
      'fill' => array(
        'type' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR, 
        'rotation' => 90, 
        'startcolor' => array(
          'argb' => 'FFA0A0A0', 
        ), 
        'endcolor' => array(
          'argb' => 'FFFFFFFF', 
        ), 
      ), 
    ); 
    $spreadsheet->getActiveSheet()->getStyle('A1:P1')->applyFromArray($styleArray); 
    foreach(range('A','Q') as $columnID) { 
     $spreadsheet->getActiveSheet()->getColumnDimension($columnID) 
       ->setAutoSize(true); 
    } 
    $spreadsheet->setActiveSheetIndex(0) 
      ->setCellValue("A1",'id') 
      ->setCellValue("B1",'pro_code') 
      ->setCellValue("C1",'hsn_code') 
      ->setCellValue("D1",'pro_name') 
      ->setCellValue("E1",'pro_brand') 
      ->setCellValue("F1",'pro_price') 
      ->setCellValue("G1",'pro_tax') 
      ->setCellValue("H1",'pro_category') 
      ->setCellValue("I1",'pro_scategory') 
      ->setCellValue("J1",'pro_sscategory') 
      ->setCellValue("K1",'pro_description') 
      ->setCellValue("L1",'pro_filename') 
      ->setCellValue("M1",'pro_date') 
      ->setCellValue("N1",'pro_qauntity') 
      ->setCellValue("O1",'pro_service') 
      ->setCellValue("P1",'pro_certificate'); 
    $x= 2; 
    foreach($subscribers as $sub){ 
     $spreadsheet->setActiveSheetIndex(0) 
       ->setCellValue("A$x",$sub['id']) 
       ->setCellValue("B$x",$sub['pro_code']) 
       ->setCellValue("C$x",$sub['hsn_code']) 
       ->setCellValue("D$x",$sub['pro_name']) 
       ->setCellValue("E$x",$sub['pro_brand']) 
       ->setCellValue("F$x",$sub['pro_price']) 
       ->setCellValue("G$x",$sub['pro_tax']) 
       ->setCellValue("H$x",$sub['pro_category']) 
       ->setCellValue("I$x",$sub['pro_scategory']) 
       ->setCellValue("J$x",$sub['pro_sscategory']) 
       ->setCellValue("K$x",$sub['pro_description']) 
       ->setCellValue("L$x",$sub['pro_filename']) 
       ->setCellValue("M$x",$sub['pro_date']) 
       ->setCellValue("N$x",$sub['pro_qauntity']) 
       ->setCellValue("O$x",$sub['pro_service']) 
       ->setCellValue("P$x",$sub['pro_certificate']); 
     $x++; 
    } 
    $spreadsheet->getActiveSheet()->setTitle('Users Information'); 
    $spreadsheet->setActiveSheetIndex(0); 
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
    header('Content-Disposition: attachment;filename="excel_sheet.xlsx"'); 
    header('Cache-Control: max-age=0'); 
    header('Cache-Control: max-age=1'); 
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
    header('Cache-Control: cache, must-revalidate'); 
    header('Pragma: public'); 
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Excel2007'); 
    $writer->save('php://output'); 
    exit; 
} 

我的模型页(base_model):

public function get_products() { 
    $query = $this->db->get('table_name'); 
    return $query->result_array(); 
} 

根据您的要求更改字段名称。