2

我正在使用PHPExcel_Reader_HTML并将其传递给我HTML以生成 excel 文件,但问题是它没有像“HTML”表中那样突出显示 excel 单元格颜色(参见图片打击),我使用的是 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上提出的问题不同,我的编码方案也不同......)

在此处输入图像描述

4

2 回答 2

1

PHPExcel HTML 阅读器不是很复杂,它更关心转换数据和标记的结构而不是样式,特别是在 html 中应用样式的方法有很多。

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

于 2016-09-29T11:54:22.157 回答
1

在完成了Excel2007我的问题的解决方案之后,我使用了功能getPHPExcel()Excel2007突出显示我的 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');   
  ?>
于 2016-09-29T12:56:11.793 回答