2016-09-29 67 views
2

我正在使用PHPExcel_Reader_HTML並將其傳遞給我的HTML以生成excel文件,但問題在於它沒有突出顯示「HTML」表格中的excel單元格顏色(請參閱圖像打擊),我使用Laravel5如何使用PHPExcel_Reader_HTML格式化Excel單元格

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

注:(我的問題是不同的,那麼問題被問過計算器,我的編碼的情況是不同的,那麼所有的..)

enter image description here

+1

哪裏是形象,要應用在Excel中的CSS,真是'不同,那麼問題被問過stackoverflow'。 – Drone

+0

添加了圖像,因爲我使用的是'PHPExcel_IOFactory :: createWriter'對象,並且我正在應用以前提出的問題的代碼,但它不適用於我(我的運氣不好) –

+1

請看一看:http://stackoverflow.com/questions/36745376/how-to-apply-css-on-html-to-excel-export – Drone

回答

1

通過Excel2007我得到了我的問題的解決方案要經過之前設置的造型,我使用功能Excel2007getPHPExcel()並選中我的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'); 
    ?> 
1

的PHPExcel HTML回覆ader不是非常複雜,並且更關心轉換數據和標記的結構而不是樣式,特別是因爲有很多方法可以在html中應用樣式。

你可以做的是加載HTML文件,然後使用本地PHPExcel功能,它保存爲xlsx檔案

+0

在這種情況下,對PHPExcel有任何有用的方法? –

+1

您的意思是有用的方法,如[PHPExcel文檔中描述的](https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/08-Recipes.md#styles)? –

+0

謝謝! getPHPExcel()爲我工作:) –

相關問題