【问题标题】:phpexcel library works fine on localhost but generates blank excel file in serverphpexcel 库在本地主机上运行良好,但在服务器中生成空白 excel 文件
【发布时间】:2016-10-13 11:19:33
【问题描述】:

这是我的代码,可以在 localhost 中正常工作,以从数据库中生成一个包含数据的 excel 文件,但在托管服务器中它会生成一个空白的 excel 文件:

// Starting the PHPExcel library
            $this->load->library('PHPExcel');
            //$this->load->library('PHPExcel/IOFactory');

            $objPHPExcel = new PHPExcel();
            $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
            $objPHPExcel->setActiveSheetIndex(0);

            // Field names in the first row
            $fields = $query->list_fields();
            $col = 0;
            foreach ($fields as $field)
            {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
                $col++;
            }

            //format the column sizes 
            $sheet = $objPHPExcel->getActiveSheet();
            $cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells( true );
            /** @var PHPExcel_Cell $cell */
            foreach( $cellIterator as $cell ) {
                    $sheet->getColumnDimension( $cell->getColumn() )->setAutoSize( true );
            }

            //var_dump($query->result());
            //die;

            // Fetching the table data
            $row = 2;
            foreach($query->result() as $data)
            {
                $col = 0;
                foreach($fields as $field)
                {
                    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
                    $col++;
                }

                $row++;
            }

            $objPHPExcel->setActiveSheetIndex(0);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');


            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="01simple.xlsx"');
            header('Cache-Control: max-age=0');

            ob_clean();
            $objWriter->save('php://output');

【问题讨论】:

  • 这表明您在本地服务器上安装了正确的依赖项(模块),并且可能在您的托管服务器上丢失。这将是首先要调查的事情。
  • 也有这个问题。找不到解决方案:-(

标签: php codeigniter phpexcel


【解决方案1】:

我认为问题与phpexcel无关。我之前遇到过类似的问题,后来发现 CI 的 list_fields() 函数在某些 linux 服务器中不起作用。您可以通过静态放置字段名称而不是使用此函数来检查这方面。

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2017-06-16
    • 2011-05-24
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    相关资源
    最近更新 更多