简体   繁体   English

PHPExcel - 一次设置具有相同值的单元格

[英]PHPExcel - Set cells with same value at one time

I'm trying to write 18000 rows x 42 cols.我正在尝试写入 18000 行 x 42 列。 The values from col 'E' to 'DD' must set with value 0. I'm trying to use increment but I found that is caused memory exhausted.从 col 'E' 到 'DD' 的值必须设置为值 0。我正在尝试使用增量,但我发现这会导致内存耗尽。

My Code is like this :我的代码是这样的:

$col = $lastcol;
foreach ($diag0 as $key) {
    $sheet->setCellValue('A'.(string)($col + 1), '0');
    $sheet->setCellValue('B'.(string)($col + 1), $key->cdDiag);
    $sheet->setCellValue('D'.(string)($col + 1), $key->nmDiag);

    for ($char='E'; $char <= 'J' ; $char++) { 
        $sheet->setCellValue($char.(string)($col + 1), '0');
    }

    $col++;
}

I'd trying the PHPExcel cached config, but it still give the same result.我想尝试 PHPExcel 缓存配置,但它仍然给出相同的结果。 I think since the col 'E' to 'DD' is have the same value, can I make it like set 'E' to 'DD' with 0 at one loop?我认为由于 col 'E' 到 'DD' 具有相同的值,我可以像在一个循环中将 'E' 设置为 'DD' 一样设置为 0 吗?

You can set the format for a range of cells:您可以设置一系列单元格的格式:

$objPHPExcel->getActiveSheet()    // THIS SHOULD BE THE SAME AS $sheet
    ->getStyle('E2:DD18000')
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);

A list with the available formats:包含可用格式的列表:

http://apigen.juzna.cz/doc/ouardisoft/PHPExcel/class-PHPExcel_Style_NumberFormat.html http://apigen.juzna.cz/doc/ouardisoft/PHPExcel/class-PHPExcel_Style_NumberFormat.html

Also, maybe this way of inserting rows works better:另外,也许这种插入行的方式效果更好:

$objPHPExcel->getActiveSheet()->fromArray(array(
    0, //COL A
    $key->cdDiag,  //COL B
    '', 
    $key->nmDiag,
    0,0,0,0,0, ... 0,0,0,0   // your 26 COLUMNS E to DD, 
),NULL,'A' . $row + 1);

You could also set the columns as empty string '' since you added the setColumnsFormat.您还可以将列设置为空字符串''因为您添加了 setColumnsFormat。 And your $col variable, not that it matters, but it should be called $row... you are moving through rows not cols...而你的 $col 变量,并不重要,但它应该被称为 $row ......你正在通过行而不是 cols ......

I never tried this but:我从未尝试过,但是:

you can use $objPHPExcel->garbageCollect();你可以使用$objPHPExcel->garbageCollect(); after inserting each row to solve your memory allocation problem, if this doesn't fix the problem try giving a little more memory with:插入每一行以解决内存分配问题后,如果这不能解决问题,请尝试提供更多内存:

ini_set('memory_limit', '256M');

default is 128, just be sure you don't consume too much resources...默认为 128,请确保您不会消耗太多资源...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM