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

时间:2016-09-29 11:42:56

标签: php laravel-5 phpexcel phpexcelreader

我正在使用PHPExcel_Reader_HTML并将其传递给HTML以生成excel文件,但问题是它没有突出显示excel单元格颜色,就像在' HTML'表(见图像打击),我正在使用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');

注意:(我的问题与stackoverflow上提出的问题有所不同,我的编码方案不同于所有..)

enter image description here

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

Excel2007