PHPExcel: HTML to Excel, writing remove the CSS in excel file

5.8k Views Asked by At

I want to export(force download) HTML(with CSS) to EXCEL sheet, for now I am using the PHPExcel library to perform this, it generate the excel file but remove the CSS (using inline with html tags), can anyone guide me, that how to keep CSS in excel sheet.

I am using this code, But I also want to keep the css and force to download

//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);
1

There are 1 best solutions below

16
On BEST ANSWER

You can't read styles from HTML markup at the moment, unless you rewrite PHPExcel's HTML Reader to handle styles; it simply isn't supported yet. If you're building the spreadsheet from HTML, perhaps you should reconsider building it directly from a new PHPExcel object, which gives you access to all the features of PHPExcel.

To send to the browser, send to php://output with the appropriate headings, as shown in Examples/01simple-download-xlsx.php, and described in the section of the developer documentation entitled Redirect output to a client’s web browser