2017-07-16 4 views
1

私は以下の問題を抱えています: PHPイグナイタをPHPフレームワークとして使用しています。私のビューの1つでは、PHPExcelを使用してxlsxファイルを生成し、 mysqlデータベースからのデータ。ファイルはサーバー内に正しく作成されていますが、強制ダウンロードしようとすると何もダウンロードされません。コードイグナイタでコントローラからexcelファイルをダウンロードしています

public function generar_excel($idCotizacion){ 

isLogged($this->session->userdata('logged_in')); 
isAdmin($this->session->userdata('logged_in')); 

$cotizacion = $this->cotizacion_model->get_cotizacion_by_id($idCotizacion); 

$propuestas = $this->propuesta_model->getPropuestasPorCotizacion($idCotizacion); 

error_reporting(E_ALL); 
date_default_timezone_set('Europe/London'); 

require_once 'application/libraries/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php'; 
require_once 'application/libraries/PHPExcel-1.8/Classes/PHPExcel.php'; 

$excel2 = PHPExcel_IOFactory::createReader('Excel2007'); 
$excel2 = $excel2->load('prueba.xlsx'); // Empty Sheet 
$excel2->setActiveSheetIndex(0); 





$excel2->getActiveSheet()->setCellValue('C8', $cotizacion["capitas"]) 
->setCellValue('C2', $cotizacion["empresa_nombre"]) 
->setCellValue('C9', $cotizacion["masa_salarial"]) 
->setCellValue('B11', $cotizacion["aseguradora_actual"]) 
->setCellValue('B13', $cotizacion["variable"]/100) 
->setCellValue('B14', '0.6')  
->setCellValue('B12', '0'); 





$letra = 'C'; 

foreach($propuestas->result_array() as $row) { 


    $excel2->getActiveSheet()->setCellValue($letra.'11', $row["nombre"]) 
    ->setCellValue($letra.'13', $row["variable"]/100) 
    ->setCellValue($letra.'14', '0.6')  
    ->setCellValue($letra.'12', '0') 
    ->setCellValue($letra.'16', '=C$8*'.$letra.'12+C$9*'.$letra.'13+C$8*'.$letra.'14') 
    ->setCellValue($letra.'17','=(B$16-'.$letra.'16)*13') 
    ->setCellValue($letra.'18','=1-('.$letra.'16/B16)'); 
    ++$letra; 
} 


$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007'); 
$nombreArchivo = 'CuadroComparativo-'.$cotizacion["empresa_nombre"].'.xlsx'; 

$objWriter->save('uploads/'.$nombreArchivo); 
$fileContents = file_get_contents('http://rpm.singleton.com.ar/uploads/'.$nombreArchivo); 
//print_r($fileContents); 
$this->load->helper('download'); 
force_download($nombreArchivo, $fileContents); 

}

ブラウザの検査からのレスポンスとプレビューが判読できます:ここではPHPの関数です。 ありがとうございました!

答えて

1

force_download()関数で送信するデータのMIMEタイプを設定します。ブラウザは、それを最も推測しようとしているか、送信したものだけを出力している可能性があります(あなたが参照している「読めない」データかもしれません)。

はあなたにforce_downloadラインを変更してみてください:

force_download($nombreArchivo, $fileContents, TRUE); 

これは、ファイルをダウンロードし、ブラウザを強制すべきで、あなたのファイル拡張子(のxlsx)に基づいてMIMEタイプを設定します。

+0

force_downloadにTRUEを追加すると、ajaxリクエストはエラーを返します。 –

0

は、このコードを試してみてください。

header('Content-Type: application/vnd.ms-excel'); 
header("Content-Disposition: attachment; filename={$nombreArchivo}.xls"); 
$this -> load -> view ('view_file',$fileContents); 
1

私は、Ajaxの成功の機能に次のように追加して、それを解決:

success: function(json) { 

        var content = JSON.parse(json); 
        //alert(content); 
        if (content.error) { 
         $('#error').html(content.response); 
         $('#error').show(); 
         $('#success').hide(); 

        } else { 
         // alert(content); 
         location.href="http://rpm.singleton.com.ar/uploads/"+content.response; 
         //location.href = "<?php echo site_url('administrador'); ?>/" + content.response; 
        } 
       }, 

と私は、ファイル名をエコーPHPで。

関連する問題