在PHPExcel中设置内存限制和时间限制

时间:2016-12-31 14:02:36

标签: php arrays phpexcel

我有很多数组,我使用PHPExcel将其打印到excel中。有时需要写很多列。它超过5000列。

问题有时甚至列从5000列开始,它可以成功打印但有时它显示为损坏的文件。

我读过这个:Why PHPExcel does not allow to write more than 5000 rows

但仍然令人困惑。

这是我的代码:

 <?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once 'PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Valerian Timothy")
                             ->setLastModifiedBy("Valerian Timothy")
                             ->setTitle("Excel Document")
                             ->setSubject("Data Mining")
                             ->setDescription("Count how many words exists.")
                             ->setKeywords("Data Mining")
                             ->setCategory("Data Mining");


// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Username')
            ->setCellValue('B1', 'Jumlah')
            ->setCellValue('C1', 'Hashtag')
            ->setCellValue('D1', 'Jumlah')
            ->setCellValue('E1', 'Etc')
            ->setCellValue('F1', 'Jumlah')
            ->setCellValue('G1', 'Date')
            ->setCellValue('H1', 'Jumlah');

// Miscellaneous glyphs, UTF-8
if($_SESSION['username'] != "")
{
    $w = 2;
    foreach($_SESSION['username'] as $user => $jumlah)
    {
        $cellUser = 'A' . $w;
        $cellJumlah = 'B' . $w;
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue($cellUser, $user)
        ->setCellValue($cellJumlah, $jumlah);
        $w++;
    }
}

if($_SESSION['hashtag'] != "")
{
    $x = 2;
    foreach($_SESSION['hashtag'] as $hashtag => $jumlah)
    {
        $cellUser = 'C' . $x;
        $cellJumlah = 'D' . $x;
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue($cellUser, $hashtag)
        ->setCellValue($cellJumlah, $jumlah);
        $x++;
    }
}

if($_SESSION['etc'] != "")
{
    $y = 2;
    foreach($_SESSION['etc'] as $etc => $jumlah)
    {
        $cellUser = 'E' . $y;
        $cellJumlah = 'F' . $y;
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue($cellUser, $etc)
        ->setCellValue($cellJumlah, $jumlah);
        $y++;
    }
}

if(!empty($_SESSION['tanggal']))
{
    $z = 2;
    foreach($_SESSION['tanggal'] as $date => $jumlah)
    {
        $cellUser = 'G' . $z;
        $cellJumlah = 'H' . $z;
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue($cellUser, $date)
        ->setCellValue($cellJumlah, $jumlah);
        $z++;
    }
}

foreach(range('A','H') as $columnID) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
        ->setAutoSize(true);
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$today = strtotime(date('Y-m-d H:i:s'));

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Data-Mining'.$today.'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
session_destroy();
?>

你能帮我解决一下吗?

0 个答案:

没有答案