【问题标题】:How to format Excel cell with PHPExcel_Reader_HTML如何使用 PHPExcel_Reader_HTML 格式化 Excel 单元格
【发布时间】:2017-02-07 17:22:29
【问题描述】:

我正在使用 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 上提出的问题不同,我的编码方案也不同..)

【问题讨论】:

  • 图片在哪里,你想在excel中应用css,真的different then the questions been asked on stackoverflow
  • 添加了图片,因为我正在使用PHPExcel_IOFactory::createWriter 对象,并且我正在应用以前提出的问题的代码,但它对我不起作用(我的运气不好)

标签: php laravel-5 phpexcel phpexcelreader


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2013-11-27
    相关资源
    最近更新 更多