2

I am using PHPExcel_Reader_HTML and passing it my HTML to generate excel file, but the problem is that it does not highlight the excel cell color as in the 'HTML' table (see image blow), I am using Laravel5

<?php



$content = $title;
$content .= '<table border="1">';
$content .= '<tr>';
foreach($fields as $f )
{
    if($f['download'] =='1') $content .= '<th style="background:#f9f9f9;">'. $f['label'] . '</th>';
}
$content .= '</tr>';

foreach ($rows as $row)
{
    $content .= '<tr>';
    foreach($fields as $f )
    {
        if($f['download'] =='1'):
            $conn = (isset($f['conn']) ? $f['conn'] : array() );
            $content .= '<td> '. htmlentities(AjaxHelpers::gridFormater($row->$f['field'],$row,$f['attribute'],$conn)) . '</td>';
        endif;
    }
    $content .= '</tr>';
}
$content .= '</table>';
$path = "../storage/app/".time().".html";
file_put_contents($path, $content);

// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($path);

// Pass to writer and output as needed
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
// Delete temporary file
unlink($path);

// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');

// It will be called file.xls
header('Content-disposition: attachment; filename="'.$title.' '.date("d/m/Y").'.xlsx"');

// Write file to the browser
$objWriter->save('php://output');

Note: ( My question is different then the questions been asked on stackoverflow, my coding scenario is different then all..)

enter image description here

Bilal Maqsood
  • 1,163
  • 3
  • 15
  • 43
  • 1
    where is image, and you want to apply css in excel, really `different then the questions been asked on stackoverflow`. – Drone Sep 29 '16 at 11:48
  • Image added, because i am using `PHPExcel_IOFactory::createWriter` object and i was applying the code of previously asked questions but it was not working for me (my bad luck ) – Bilal Maqsood Sep 29 '16 at 11:52
  • 1
    Please have a look : http://stackoverflow.com/questions/36745376/how-to-apply-css-on-html-to-excel-export – Drone Sep 29 '16 at 11:53

2 Answers2

1

The PHPExcel HTML Reader isn't very sophisticated, and is more concerned with converting the data and the structure of the markup than the styling, particularly as there are many ways to apply styling in html.

What you can do is load the html document, and then use native PHPExcel functionality to set styling before saving it as an xlsx file

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Any helpfull method for `PHPExcel` in this case?? – Bilal Maqsood Sep 29 '16 at 11:57
  • 1
    You mean helpful methods such as [those described in the PHPExcel documentation](https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/08-Recipes.md#styles)? – Mark Baker Sep 29 '16 at 12:35
1

After going through the Excel2007 I got solution of my question, I've used the function getPHPExcel() of Excel2007 and Highlight my Excel Cell

<?php
function cellColor($objPHPExcel,$cells,$color){
    $objPHPExcel->getActiveSheet()->getStyle($cells)->getFill()->applyFromArray(array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array(
            'rgb' => $color
        )
    ));
}


$content = $title;
$content .= '<table border="1">';
$content .= '<tr>';
foreach($fields as $f )
{
    if($f['download'] =='1') $content .= '<th style="background:#f9f9f9;">'. $f['label'] . '</th>';
}
$content .= '</tr>';

foreach ($rows as $row)
{
    $content .= '<tr>';
    foreach($fields as $f )
    {
        if($f['download'] =='1'):
            $conn = (isset($f['conn']) ? $f['conn'] : array() );
            $content .= '<td> '. htmlentities(AjaxHelpers::gridFormater($row->$f['field'],$row,$f['attribute'],$conn)) . '</td>';
        endif;
    }
    $content .= '</tr>';
}
$content .= '</table>';
$path = "../storage/app/".time().".html";
file_put_contents($path, $content);

// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($path);

// Pass to writer and output as needed
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
$objPHPExcel = $objWriter->getPHPExcel();
$counter=0;
foreach($fields as $f )
{
    if($f['download'] =='1')
        cellColor($objPHPExcel,'A2','F28A8C');
$counter++;
}

// Delete temporary file
unlink($path);

// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');

// It will be called file.xls
header('Content-disposition: attachment; filename="'.$title.' '.date("d/m/Y").'.xlsx"');

// Write file to the browser
$objWriter->save('php://output');   
  ?>
Bilal Maqsood
  • 1,163
  • 3
  • 15
  • 43