无法使用具有动态数据的PHPExcel库下载Excel文件

时间:2019-01-10 06:45:07

标签: php excel codeigniter download phpexcel

当我单击导出按钮时,它将转到我的export_inquiry()函数,并且我已经设置了来自数据库表的数组,并使用该动态数组数据下载了excel文件。但是,现在它引发了我的错误 无法访问该网站 http://www.example.com/project_name/admin/order/export_inquiry处的网页可能暂时关闭,或者已永久移动到新的网址。

  

ERR_INVALID_RESPONSE。

我正在使用PHPExcel中的CodeIgniter库下载excel文件。下面是我的Excel导出代码。

public function export_inquiry()
{
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(E_ALL);


    $this->load->library('phpexcel/PHPExcel');
    $object = new PHPExcel();

      $object->setActiveSheetIndex(0);

      $table_columns = array("DATE", "ID", "CustomerReference", "InvoiceName", "PickUpCity", "PickUpDate");

      $column = 0;

      foreach($table_columns as $field)
      {
       $object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);
       $column++;
      }

      $employee_data  =array(
        array(
            'order_date' => '2018-05-23 22:35:36',
            'id' => 17,
            'customerreference' => '',
            'invoice_name' => 'Troy Design and Manufacturing',
            'pick_up_city' => 'Gothenburg',
            'pick_up_date' => '2018-07-02'
        ),
        array(
            'order_date' => '2018-05-28 02:51:58',
            'id' => 19,
            'customerreference' => '',
            'invoice_name' => 'Hollingsworth Distribution Systems',
            'pick_up_city' => 'Gothenburg',
            'pick_up_date' => '2018-07-02'
        ),
        array(
            'order_date' => '2018-05-28 04:39:23',
            'id' => 20,
            'customerreference' => '',
            'invoice_name' => 'Motherson Automotive (FSP-1)',
            'pick_up_city' => 'Gothenburg',
            'pick_up_date' => '2018-07-02'
        )
    );

      $excel_row = 2;
      foreach($employee_data as $key => $row)
      {
       $object->getActiveSheet()->setCellValueByColumnAndRow(0, $excel_row, $row['order_date']);
       $object->getActiveSheet()->setCellValueByColumnAndRow(1, $excel_row, $row['id']);
       $object->getActiveSheet()->setCellValueByColumnAndRow(2, $excel_row, $row['customerreference']);
       $object->getActiveSheet()->setCellValueByColumnAndRow(3, $excel_row, $row['invoice_name']);
       $object->getActiveSheet()->setCellValueByColumnAndRow(4, $excel_row, $row['pick_up_city']);
       $object->getActiveSheet()->setCellValueByColumnAndRow(5, $excel_row, $row['pick_up_date']);
       $excel_row++;
      }

      $object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="Employee Data.xls"');
      $object_writer->save('php://output');
}

有人可以帮助我解决这个问题吗? 预先感谢。

1 个答案:

答案 0 :(得分:0)

此答案将对您的问题有所帮助。请检查...


public function export_inquiry() {
        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);
        date_default_timezone_set('Asia/Kolkata');

        $this->load->library('phpexcel/PHPExcel');

        $objPHPExcel = new PHPExcel();  

        $employee_data  =array(
            array(
                'order_date' => '2018-05-23 22:35:36',
                'id' => 17,
                'customerreference' => '',
                'invoice_name' => 'Troy Design and Manufacturing',
                'pick_up_city' => 'Gothenburg',
                'pick_up_date' => '2018-07-02'
            ),
            array(
                'order_date' => '2018-05-28 02:51:58',
                'id' => 19,
                'customerreference' => '',
                'invoice_name' => 'Hollingsworth Distribution Systems',
                'pick_up_city' => 'Gothenburg',
                'pick_up_date' => '2018-07-02'
            ),
            array(
                'order_date' => '2018-05-28 04:39:23',
                'id' => 20,
                'customerreference' => '',
                'invoice_name' => 'Motherson Automotive (FSP-1)',
                'pick_up_city' => 'Gothenburg',
                'pick_up_date' => '2018-07-02'
            )
        );      

        // Add some data
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A1', 'ID')
                    ->setCellValue('B1', 'Order date')
                    ->setCellValue('C1', 'Customer reference')
                    ->setCellValue('D1', 'Invoice name')
                    ->setCellValue('E1', 'Pick up city')
                    ->setCellValue('F1', 'Pick up date');

        $i = 2;
        foreach ($employee_data as $key => $value) {
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$i, ($key + 1))
                ->setCellValue('B'.$i, ($value['order_date'] ? date("d-m-Y", strtotime($value['order_date'])) : "-"))
                ->setCellValue('C'.$i, ($value['customerreference'] ? : "-"))
                ->setCellValue('D'.$i, ($value['invoice_name'] ? : "-"))
                ->setCellValue('E'.$i, ($value['pick_up_city'] ? : "-"))
                ->setCellValue('F'.$i, ($value['pick_up_date'] ? date("d-m-Y", strtotime($value['pick_up_date'])) : "-"));                  
            $i++;

        }

        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('Report - '.date("d-m-Y"));
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client’s web browser (Excel2007)
        $file_name = time().".xls";
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename='.$file_name.'');    
        header('Cache-Control: max-age=0');


        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }