Kohana 3.2 + PHPExcel在写入超过31行时创建空文档。

时间:2013-10-11 23:14:01

标签: export-to-excel phpexcel kohana-3.2

我正在尝试创建一个包含3列列表的简单excel文档。 (名字,姓氏,电子邮件地址)

当我输出超过31行时,会创建 空白的Excel文件 。有谁知道如何修理它?

我的代码如下:

    $guests = ORM::factory('guest')
      ->order_by('last_name', 'ASC')
      ->find_all()
  ->as_array(); 

$columns = array('first_name', 'last_name', 'email');


$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()
        ->setCreator('Me')
        ->setLastModifiedBy('Me)
        ->setTitle('Guest List')
        ->setSubject('Guest List')
        ->setDescription('Title of Report, run on ' . date('m/d/Y H:i:s'));
$objPHPExcel->createSheet(0);
$objPHPExcel->setActiveSheetIndex(0);

    foreach($guests as $row => $guest) {
        $source = array();
        $column = 0;
        $next_row = $objPHPExcel->getActiveSheet()->getHighestRow();

        foreach($guest->as_array() as $column_name => $data)  {
            if(in_array($column_name, $columns)) {

              $objPHPExcel->getActiveSheet()
                ->setCellValueByColumnAndRow($column, $next_row+1, $data);  

              $column++;
              // unset($data);
            }
        }

    }

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');   
    $this->response->body($objWriter->save('php://output'));
    $this->response->send_file(TRUE, 'GuestList.xls');

1 个答案:

答案 0 :(得分:0)

Kohana使用send_file()方法处理渲染文件很困难,因此使用PHPExcel中的内置方法将文件写入磁盘。然后使用Kohana下载保存的文件。

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($filename, 'EXCEL2007');
$this->response->send_file( DOCROOT.$filename );