PHPExcel: HTML to Excel, record removes CSS in excel file

I want to export (force load) HTML (with CSS) to an EXCEL sheet, while I use the PHPExcel library to accomplish this, it generates an excel file, but it removes the CSS (using inline html tags), can someone tell me how to save CSS in excel sheet.

I am using this code but I also want to keep css and force to load

//html
$html = "<table> 
<thead> <tr> <td colspan='2'> <h1> Main Heading </h1> <td> </tr> </thead>
<tbody>
<tr> 
  <th style='background:#ccc; color:red; font-size:15px'> Name <th> 
  <th style='background:#ccc; color:red; font-size:15px'> Class <th> 
</tr> 
<tr> 
  <td style='background:#fff; color:green; font-size:13px'> Jhon <th> 
  <td style='background:#fff; color:gree; font-size:13px'> 9th <th> 
</tr> 
</tbody>

</table>";

// Put the html into a temporary file
$tmpfile = time().'.html';
file_put_contents($tmpfile, $html);

// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML; 
$content = $reader->load($tmpfile);  

// Pass to writer and output as needed
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
$objWriter->save('excelfile.xlsx');

// Delete temporary file
unlink($tmpfile);

      

+3


source to share


1 answer


You cannot read styles from HTML markup at this time unless you rewrite PHPExcel HTML Reader to handle styles; it just isn't supported yet. If you are creating a spreadsheet from HTML, you might want to reconsider creating it directly from a new PHPExcel object, which will give you access to all the functionality of PHPExcel.



To send to the browser, send to the php://output

appropriate headers as shown in Examples/01simple-download-xlsx.php

, and described in the developer documentation sectionRedirect output to a client’s web browser

+1


source







All Articles