如何使用 PHPExcel_Reader_HTML 格式化 Excel 单元格

How to format Excel cell with PHPExcel_Reader_HTML

我正在使用 PHPExcel_Reader_HTML 并将其传递给我的 HTML 以生成 excel 文件,但问题是它不会突出显示 excel 单元格颜色,如'HTML' table(见下图),我用的是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');

注意:(我的问题与在 Whosebug 上提出的问题不同,我的编码场景与所有问题都不同..)

PHPExcel HTML Reader 不是很复杂,它更关心转换数据和标记的结构而不是样式,特别是因为有很多方法可以应用样式在 html.

您可以加​​载 html 文档,然后使用本机 PHPExcel 功能设置样式,然后再将其保存为 xlsx 文件

完成 Excel2007 后我得到了问题的解决方案,我使用了 Excel2007 的函数 getPHPExcel() 并突出显示我的 Excel 单元格

<?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');   
  ?>