4

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);
Qazi
  • 5,015
  • 8
  • 42
  • 62

1 Answers1

2

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Hi, @Mark for your this sentence, "unless you rewrite PHPExcel's HTML Reader to handle styles; " Can you please give me the link or code, which handle All the HTML with css etc ? – Qazi Dec 10 '14 at 03:49
  • The beauty of Open Source: it's the HTML Reader file.... it's called `HTML.php` and you can find it in the `Classes/PHPExcel/Reader` folder..... it doesn't handle any CSS at all though, only the basic HTML elements, tables and the rowspan/colspan properties of table cells – Mark Baker Dec 10 '14 at 07:57
  • so its mean, I have to write new code for CSS ? Right ? If yes, can you give me a simple example, Which helps me to read the css from HTML and then put that for excel ? – Qazi Dec 10 '14 at 08:45
  • If I could give you an example, it would be because I'd already written that code myself, in which case it would be part of PHPExcel already.... but you can take a look at this [pull request](https://github.com/PHPOffice/PHPExcel/pull/205) that makes some efforts to read style information from html – Mark Baker Dec 10 '14 at 09:18
  • Didn't understand the pull request code, Actually I am beginner, Can you please elaborate it little more or some working example ? – Qazi Dec 10 '14 at 10:27
  • No I can't elaborate any more.... if I had a working example, then it would probably already be part of the PHPExcel library – Mark Baker Dec 10 '14 at 10:31
  • Where does your HTML actually come from? Are you building it elsewhere in your script? – Mark Baker Dec 10 '14 at 10:32
  • I created the general method, I pass required parameters, and then it echo the requested HTML, after that I add those HTML into temp file and follow PHPExcel methods to read it and create the xlsx file. But excel file missing the css. – Qazi Dec 10 '14 at 10:43
  • Perhaps you should reconsider the approach you're taking to this, and just create a new PHPExcel object and populate it from your data instead of generating HTML.... that's what most people do – Mark Baker Dec 10 '14 at 10:44
  • Sorry, I didn't understand the last comment, Please give a example, If you can ? – Qazi Dec 10 '14 at 10:47
  • The PHPExcel documentation is filled with examples..... and there's an entire folder called `/Examples` that's has plenty of examples on how to create an Excel file – Mark Baker Dec 10 '14 at 11:05
  • before PHPExcel library I was using manual way(headers etc), see below header("Pragma: public"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename="abc.xls"); echo $html; – Qazi Dec 10 '14 at 11:06
  • Try reading some of the documentation and looking at some of the examples on how to use PHPExcel – Mark Baker Dec 10 '14 at 11:08
  • manual way generate the proper excel file, but now problem is, whenever I open the exported excel file in office 2013, it gives the warning alert box, "the file formate and extension of zbc.xls dot not match. the file could be corrupted or unsafe, unless you trust its source. Do you want to open it anyway ?". Can you tell me that how to get rid of this warning ? is there any special header required for the xls file to open in office 2013 ? without the warning? – Qazi Dec 10 '14 at 11:23
  • Use the correct extension for the writer that you're using: The `Excel2007` Writer generates an OfficeOpenXML-format `.xlsx` file, the `Excel5` Writer generates a BIFF-format `.xls` file – Mark Baker Dec 10 '14 at 11:37
  • I tried this (manual way not php excel) header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); but generated file is not opening – Qazi Dec 10 '14 at 11:53