2017-08-07 1 views
0

J'exporte la base de données pour exceller avec la bibliothèque phpexcel avec succès. Mais quand j'ouvre le fichier en Excel, il est dit contenu illisible. Mais je peux ouvrir et voir mes données.phpexcel dans les données d'exportation de Codeigniter dans la base de données et le modèle de barre latérale

Ensuite, j'ouvre le fichier en utilisant le bloc-notes, je vois que le code html de ma barre latérale est exporté en Excel.

Comment puis-je le supprimer?

Répondre

0

La réponse est de mettre

exit; 

après

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

Exemple:

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

Vérifiez mon projet où je PHPExcel.

Selon moi, le code de la page (ourproduct.php):

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

Ma page de commande (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; 
} 

Ma page modèle (base_model):

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

Modifiez les noms des champs selon vos besoins.