PHPExcel 导出较大文件失败

发布于 2024-09-26 15:14:03 字数 1128 浏览 5 评论 0原文

我正在使用 PHPExcel 库基于 mysql 数据库生成 Excel 数据。 MySql 查询结果为 1,34,000 行。 Excel 在一张工作表上支持 65,536 行。因此,逻辑就像

foreach($result as $value)
{
    $val = array_values($value);

    if($rowscounter < 65000)
    {
        $objPHPExcel->addRow($val,$rowscounter);
    }
    else
    {
        $active_sheet++;
        $objPHPExcel->createSheet();
        $objPHPExcel->setActiveSheetIndex($active_sheet);
        $rowscounter = 1;
    }
    $rowscounter++;
}
// deliver header   
header("Content-Type: $mtype; charset=" . $objPHPExcel->sEncoding);
header("Content-Type:application/octet-stream");
header("Content-Disposition: inline; filename=\"" . $filename . ".$ext\"");

// Save it as an excel 2003 file
$objWriter = IOFactory::createWriter($objPHPExcel,$objPHPExcel->sFileFormat);
//echo "Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB";exit;
$objWriter->save('php://output');

在一张工作表达到 65000 条记录后创建新工作表一样。

但这不起作用;不给出任何输出或错误。最初我认为这是因为内存限制。但是当回显时,它显示峰值内存为 1400.5 MB,并且我已使用 ini_set('memory_limit', '3500M'); 将内存限制设置为 3500MB

;您能否提出建议或任何替代方案?

I am using PHPExcel library to generate excel data based on mysql datbase. MySql query results in the 1,34,000 rows. And Excel Supports 65,536 Rows on one worksheet. So made logic like

foreach($result as $value)
{
    $val = array_values($value);

    if($rowscounter < 65000)
    {
        $objPHPExcel->addRow($val,$rowscounter);
    }
    else
    {
        $active_sheet++;
        $objPHPExcel->createSheet();
        $objPHPExcel->setActiveSheetIndex($active_sheet);
        $rowscounter = 1;
    }
    $rowscounter++;
}
// deliver header   
header("Content-Type: $mtype; charset=" . $objPHPExcel->sEncoding);
header("Content-Type:application/octet-stream");
header("Content-Disposition: inline; filename=\"" . $filename . ".$ext\"");

// Save it as an excel 2003 file
$objWriter = IOFactory::createWriter($objPHPExcel,$objPHPExcel->sFileFormat);
//echo "Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB";exit;
$objWriter->save('php://output');

to create new worksheet after 65000 records are reached for one woeksheet.

But it doesn't work; not giving any output or error. Initially I thought its because of memory limit. But when echoed it shows peak memory to 1400.5 MB and I have set my memory limit up to 3500MB using ini_set('memory_limit', '3500M');

Could you please suggest something or any alternative?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2024-10-03 15:14:03

您可能超出了 65,000 的限制,您将 $rowscounter 初始值设置为多少? 1 或 0(零)?我之所以要求数组结果从索引 0(零)开始,您要添加行,然后在添加后递增计数器。因此,如果您从 0(零)开始计数器,那么您的行数可能会多于您计算的行数。另外,您在 else 语句中缺少一行,您循环遍历一个值,但不将其添加到工作表中,

尝试这

$rowscounter = 1;

foreach($result as $value)
{
    $val = array_values($value);

    if($rowscounter < 65000)
    {
        $objPHPExcel->addRow($val,$rowscounter);
    }
    else
    {
        $active_sheet++;
        $objPHPExcel->createSheet();
        $objPHPExcel->setActiveSheetIndex($active_sheet);
        $rowscounter = 1;

        // add missing row
        $objPHPExcel->addRow($val,$rowscounter);
    }
    $rowscounter++;
}
// deliver header   
header("Content-Type: $mtype; charset=" . $objPHPExcel->sEncoding);
header("Content-Type:application/octet-stream");
header("Content-Disposition: inline; filename=\"" . $filename . ".$ext\"");

// Save it as an excel 2003 file
$objWriter = IOFactory::createWriter($objPHPExcel,$objPHPExcel->sFileFormat);
//echo "Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB";exit;
$objWriter->save('php://output');

是我试图解释的一个简单示例,如果 $i 设置为 0(零)否则不满足条件。因此,您的结果中将会多出一行。如果 $i 设置为 1,则满足 else 条件

$count = array(1,2,3,4,5,6,7,8,9,10);
$i=1; // set this to 0 (zero) and test, set to 1 and test

foreach($count as $cnt) {
    if($i < 10) {
        echo "If condition - Count value: ".$cnt." i value:".$i."<br />";
    } else {
        echo "Else condition - Count value: ".$cnt." i value:".$i."<br />";
    }
    $i++;
}

You might be exceeding the 65,000 limit, what do you set the $rowscounter initial value at? 1 or 0 (zero)? The reason I ask it that the array results starts at index 0 (zero), you're adding the row and then incrementing the counter after the add. So if you start the counter at 0 (zero) than you might have more rows that what you have counted. Also you are missing a row in the else statement, you loop through a value but don't add it to the sheet

try this

$rowscounter = 1;

foreach($result as $value)
{
    $val = array_values($value);

    if($rowscounter < 65000)
    {
        $objPHPExcel->addRow($val,$rowscounter);
    }
    else
    {
        $active_sheet++;
        $objPHPExcel->createSheet();
        $objPHPExcel->setActiveSheetIndex($active_sheet);
        $rowscounter = 1;

        // add missing row
        $objPHPExcel->addRow($val,$rowscounter);
    }
    $rowscounter++;
}
// deliver header   
header("Content-Type: $mtype; charset=" . $objPHPExcel->sEncoding);
header("Content-Type:application/octet-stream");
header("Content-Disposition: inline; filename=\"" . $filename . ".$ext\"");

// Save it as an excel 2003 file
$objWriter = IOFactory::createWriter($objPHPExcel,$objPHPExcel->sFileFormat);
//echo "Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB";exit;
$objWriter->save('php://output');

a simple example of what I'm trying to explain, if $i is set to 0 (zero) the else condition is not met. So you will have an extra row in your results. If $i is set to 1 the else condition is met

$count = array(1,2,3,4,5,6,7,8,9,10);
$i=1; // set this to 0 (zero) and test, set to 1 and test

foreach($count as $cnt) {
    if($i < 10) {
        echo "If condition - Count value: ".$cnt." i value:".$i."<br />";
    } else {
        echo "Else condition - Count value: ".$cnt." i value:".$i."<br />";
    }
    $i++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文