phpexcel 库在本地主机上工作正常,但在服务器中生成空白的 excel 文件

标签 php codeigniter phpexcel

这是我的代码,它在本地主机上运行良好,可以生成一个包含数据库数据的 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');

最佳答案

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

关于phpexcel 库在本地主机上工作正常,但在服务器中生成空白的 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40019548/

相关文章:

php - 显示“”字符而不是 “ ”

PHP imap_search 收件箱/已发送项目

php - CodeIgniter 中的 jQuery,在 View 内还是在外部 js 文件中?

php - 如何在 Eloquent Laravel 中对三个表进行咨询

php - 屏幕上的div定位

php - 使用 PHPExcel 合并单元格值 - PHP

php - 在PHPExcel中将单元格格式化为百分比

PHPExcel 将 XLS 转换为具有特殊字符的 CSV

php - 在流明中找不到特征 'App\HasApiTokens'

codeigniter - CodeIgniter 中的自定义异常