2016-09-29 3 views
2

PHPExcel_Reader_HTMLを使用していますが、HTMLを渡してExcelファイルを生成していますが、 'HTML'テーブルのようにExcelセルの色を強調表示しないという問題があります。私はLaravel5ExcelExcel_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'); 

注意を使用しています。(私の質問はstackoverflowの上頼まれて質問し、別のある、私のコーディングのシナリオは、すべて異なっている。)

enter image description here

+1

私のExcelのセルを強調表示します。 – Drone

+0

イメージは、 'PHPExcel_IOFactory :: createWriter'オブジェクトを使用していて、以前に質問されたコードを適用していたので追加されましたが、私にとってはうまくいきませんでした(私の不運) –

+1

見てください:http://stackoverflow.com/questions/36745376/how-to-apply-css-on-html-to-excel-export – Drone

答えて

1

としてそれを保存する前にスタイルを設定するには、ネイティブPHPExcelの機能を使用している、私が使用してきました機能Excel2007getPHPExcel()

画像であり、ExcelでのCSSを適用したい、本当に `異なるが、その後の質問はstackoverflow`に頼まれて
<?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ドキュメントをロードした後、私は私の質問の溶液を得たExcel2007を通過した後のxlsxファイル

+0

この場合、PHPExcelの役に立つ方法はありますか? –

+1

あなたは[PHPExcelのドキュメントに記載されている](https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/08-Recipes.md#styles)などの有益なメソッドを意味しますか? –

+0

ありがとう! getPHPExcel()は私のために働いた:) –

関連する問題