【问题标题】:Excel file is not downloading using PHPExcel library with dynamic dataExcel 文件未使用带有动态数据的 PHPExcel 库下载
【发布时间】:2019-06-04 23:59:29
【问题描述】:

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

ERR_INVALID_RESPONSE。

我正在使用CodeIgniter 中的PHPExcel 库来下载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');
}

谁能帮我解决这个问题? 提前致谢。

【问题讨论】:

    标签: php excel codeigniter download phpexcel


    【解决方案1】:

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


    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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多