2016-09-29 2 views
2

PHPExcel_Reader_HTML을 사용하고 있으며 Excel HTML을 전달하여 Excel 파일을 생성하지만 문제는 'HTML'테이블 에서처럼 셀 색상을 강조 표시하지 않는다는 것입니다 (이미지 불기 참조). 내가 Laravel5PHPExcel_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

을 강조, 당신은 엑셀에서 CSS, 정말 '다른 다음 질문 stackoverflow'에 요청을받은을 적용 할. – Drone

+0

이미지가'PHPExcel_IOFactory :: createWriter' 객체를 사용하고 있기 때문에 이전에 질문 한 코드를 적용했기 때문에 이미지가 추가되었지만 (내 불운) –

+1

보십시오 : http://stackoverflow.com/questions/36745376/how-to-apply-css-on-html-to-excel-export – Drone

답변

1

를 저장하기 전에 스타일을 설정하는 기본 PHPExcel 기능을 사용하여, 내가 사용했습니다 기능 Excel2007getPHPExcel() 및 이미지 내 엑셀 셀

<?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() 나를 위해 일했다 :) –

관련 문제