2017-03-03 66 views
2

我想問你的幫忙。PHP Excel和Codeigniter

我在做一個使用CodeIgniter的項目,除了我製作的圖表外,我還需要生成Excel文件。我正在使用PHPExcel來生成Excel文件。

我在項目的third_party文件夾中添加了Classes文件夾。

我創建了一個名爲Excel.php項目的庫文件夾與文件的代碼:

<?php 
if (!defined('BASEPATH')) exit('No direct script access allowed'); 

require_once APPPATH."/third_party/Classes/PHPExcel.php"; 
class Excel extends PHPExcel { 
    public function __construct() { 
     parent::__construct(); 
    } 
} 

我認爲文件中,我使用AJAX來發送數據並進行處理的控制器,該控制器我在那裏放置了PHPExcel的代碼。下面是我在視圖代碼:

$("#excel").click(function(){ 
      var fromDate = $("#fromDate").val(); 
      var toDate = $("#toDate").val(); 

      var dataString = "fromDate="+fromDate+"&toDate="+toDate; 

      $.ajax({ 
      //url of the function 
       url: '<?php echo base_url(); ?>index.php/charts/excel', 
          //set type post 
       type: 'POST', 
          //get the data from the input field 
       data: dataString, 
       success:function(data) 
       { 
        alert(data); 
       } 
      }); 
    }); 

這是我在控制器代碼:

public function excel() 
{ 
    $this->load->library('Excel'); 
    //activate worksheet number 1 
    $this->excel->setActiveSheetIndex(0); 
    //name the worksheet 
    $this->excel->getActiveSheet()->setTitle('Users list'); 

    $sDate = $this->input->post('fromDate');//date_create_from_format('Y-m-d', $_POST['fromDate']); 
    $eDate = $this->input->post('toDate');//date_create_from_format('Y-m-d', $_POST['toDate']); 
    $data = $this->Charts_model->hello($sDate,$eDate); 

    // read data to active sheet 
    //print_r($data); 

    $row = 1; 
    foreach($data as $r){ 
     $this->excel->getActiveSheet()->fromArray(array($r->emp_id, $r->emp_stat, $r->isActive, $r->dateStarted), null, 'A'.$row); 
     $row++; 
    } 


    $filename='just_some_random_name.xls'; //save our workbook as this file name 

    header('Content-Type: application/vnd.ms-excel'); //mime type 

    header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name 

    header('Cache-Control: max-age=0'); //no cache 

    //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type) 
    //if you want to save it as .XLSX Excel 2007 format 

    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); 

    //force user to download the Excel file without writing it to server's HD 
    $objWriter->save('php://output'); 
} 

控制器似乎是工作,但是,它不下載任何Excel文件。

因此,我試着提醒控制器返回的數據,並且this是提醒時的外觀。

我希望你能幫我解決這個問題。由於

UPDATE

的問題就解決了。事實證明,它已經在下載文件。當我檢查目錄時,文件存在。

我所做的是我添加了一個錨點標記,如果導出成功,它會觸發該文件實際下載並在頁面下方看到。

$("#someid").trigger("click").attr("target", "_blank"); 

錨標記看起來是這樣的:

<a href="<?php echo base_url(); ?>/directoryofthefile/somename.xls" style="display: none;"> 
<input type="button" id="someid" value="Test" class="btn btn-success"> 
</a> 
+0

未來,使用'console.log()'而不是'alert()'進行調試。這是一個更好的習慣,是互動的。 – Goose

回答

1

php.net提取:

如果你希望用戶被提示保存您發送的數據,如生成的PDF文件,您可以使用»Content-Disposition標題提供建議的文件名並強制瀏覽器顯示保存對話框。

<?php 
// We'll be outputting a PDF 
header('Content-Type: application/pdf'); 

// It will be called downloaded.pdf 
header('Content-Disposition: attachment; filename="downloaded.pdf"'); 

// The PDF source is in original.pdf 
readfile('original.pdf'); 
?> 

推斷,爲您的代碼,我認爲你只是缺少ReadFile的()調用 。嘗試在創建文件後添加它。 好運