iliaal / php_excel

PHP Extension interface to the Excel writing/reading library
http://ilia.ws
534 stars 131 forks source link

Applying a style after a style has already been defined #209

Closed sgiacinto closed 7 years ago

sgiacinto commented 7 years ago

Hello, I am writing alternating colored rows in my excel sheet like this:

foreach($subjectdata as $subjectrow) {
        if($cnt % 2 == 0) {
                $xlSheet1->writeRow($cnt, $subjectrow, 0, $right_text); 
            }
            else {
                $xlSheet1->writeRow($cnt, $subjectrow, 0, $fill_grey_right);    
          }

        $cnt++; 
}

The styles are defined as such:


$fill_grey_right = $ex->addFormat();
$fill_grey_right->fillPattern(ExcelFormat::FILLPATTERN_SOLID);
$fill_grey_right->patternBackgroundColor(22);
$fill_grey_right->patternForegroundColor(22);
$fill_grey_right->horizontalAlign(3);   

$fill_grey = $ex->addFormat();
$fill_grey->fillPattern(ExcelFormat::FILLPATTERN_SOLID);
$fill_grey->patternBackgroundColor(22);
$fill_grey->patternForegroundColor(22);

$left_text =    $ex->addFormat();
$left_text->horizontalAlign(1);

After adding the rows, (all cells are right aligned as per the style) I want to align column 1 LEFT, and leave all the other columns aligned RIGHT.

However when I try and do this:

$xlSheet1->setColWidth(1, 1, 30, false, $text_left); It has no effect and column 1 (0 is hidden) remains right aligned.
How do I add the left-alignment to the column after it was created with right alignment?

thanks, SG

johmue commented 7 years ago

I think you can not add a format it's always a replacement. So you have to be explicit about the formatting for each cell and you have to apply all formats for a cell in the write step.

<?php

// untested

$ex = new \ExcelBook(null, null, true);
$xlSheet1 = new \ExcelSheet($ex, 'Sheet1');

$fill_grey = $ex->addFormat();
$fill_grey->fillPattern(\ExcelFormat::FILLPATTERN_SOLID);
$fill_grey->patternBackgroundColor(22);
$fill_grey->patternForegroundColor(22);

$fill_grey_right = clone $fill_grey;
$fill_grey_right->horizontalAlign(3);

$fill_grey_left = clone $fill_grey;
$fill_grey_left->horizontalAlign(1);

$left_text = $ex->addFormat();
$left_text->horizontalAlign(1);

$right_text = $ex->addFormat();
$right_text->horizontalAlign(3);

$subjectdata = [
    ['a', 'b', 'c'],
    ['a', 'b', 'c'],
    ['a', 'b', 'c'],
    ['a', 'b', 'c']
];

$cnt = 1;
$colIndex = 1;
foreach($subjectdata as $subjectrow) {
    if($cnt % 2 == 0) {
        $xlSheet1->writeRow($cnt, $subjectrow, 0, $right_text); // write row
        $xlSheet1->setCellFormat($cnt, $colIndex, $left_text); // overwrite format for column 1
    } else {
        $xlSheet1->writeRow($cnt, $subjectrow, 0, $fill_grey_right); // write row
        $xlSheet1->setCellFormat($cnt, $colIndex, $fill_grey_left); // overwrite format for column 1
    }

    $cnt++;
}

$ex->save(__FILE__ . '.xlsx');
sgiacinto commented 7 years ago

Thank you. This worked for me. Sorry to have posted it as an "issue" as it was a support request, not an issue with the software.

Thanks again.