所以我在Excel上导出数据时遇到了此错误。
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 72 bytes) in /home1/greatsup/public_html/system/application/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell.php on line 889
这是令人惊讶的,因为我已经导出的表仅包含10行。因此,我尝试先打印它以查看是否获取任何数据,但仍然有相同的错误。
ps:我尝试在较大的数据集上使用Excel导出。但是在这个控制器中,我很难弄清楚问题是什么。
这是我的代码
public function excel_export(Request $request) {
$post = $request->all();
$dateFrom = $post['updated_at_from'];
$dateTo = $post['updated_at_to'];
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Manila');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
$objPHPExcel = new PHPExcel();
$objPHPExcel = PHPExcel_IOFactory::load("templates/unit_specifications_format.xlsx");
$objPHPExcel->getProperties()->setCreator(Config::$APPNAME)
->setLastModifiedBy(Config::$APPNAME)
->setTitle(null)
->setSubject(null)
->setDescription(null)
->setKeywords(null)
->setCategory(null);
$fontSettings = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => 'FFFFFF'),
'size' => 12,
'name' => 'calibri'
));
$headerSettings = array('fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => AppResourcesConfig::$PRIMARYCOLOR)
));
$borderSettings = $BStyle = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$query = DB::table('unit_specifications')->get();
print_r($query);
exit;
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
$start = 5;
$row = $start;
$column = 0;
$no = 1;
$allCombination = PHPExcel_Cell::stringFromColumnIndex(0).$start.':'.PHPExcel_Cell::stringFromColumnIndex($column-1).($row-1);
$sheet->getStyle($allCombination)->applyFromArray($borderSettings);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$this->excel_filename.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
我错过了什么想法?
如果不确定设置的php内存限制,则将其有用包含在错误消息中。但是,大小在字节中报告,因此我们为您进行了一些转换:
PHP: Fatal Error: Allowed Memory Size of 8388608 Bytes Exhausted - 8 MB
PHP: Fatal Error: Allowed Memory Size of 16777216 Bytes Exhausted - 16 MB
PHP: Fatal Error: Allowed Memory Size of 33554432 Bytes Exhausted - 32 MB
PHP: Fatal Error: Allowed Memory Size of 67108864 Bytes Exhausted - 64 MB
PHP: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted - 128 MB
PHP: Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted - 256 MB
PHP: Fatal Error: Allowed Memory Size of 536870912 Bytes Exhausted - 512 MB
PHP: Fatal Error: Allowed Memory Size of 1073741824 Bytes Exhausted - 1 GB
解决此错误转到您的php.ini
ini_set('memory_limit', '1024M'); // or you could use 1G
这取决于您使用的大多数使用Word
使用的apachememory_limit
因此,搜索此单词并修改并检查您的数据希望这能解决您的问题