php - Modify code to export HTML table to Excel -
i'm using phpexcel 1.8
in application. want download file generated.html
, content.
<!doctype html> <html> <head> <title>phpexcel</title> <style> table, td, th { border: 1px solid black; } </style> </head> <body> <table style="width:100%"> <tr> <td bgcolor="#ff0000">jill</td> <td>smith</td> <td>50</td> </tr> <tr> <td>jack</td> <td>paper</td> <td>123</td> </tr> <tr> <td>eve</td> <td>jackson</td> <td>94</td> </tr> <tr> <td bgcolor="#ff0000">john</td> <td>doe</td> <td>80</td> </tr> </table> </body> </html>
for now, have dl.php
perform download example.xls
doesn't have styles , colors applied it.
<?php require_once 'phpexcel-1.8/classes/phpexcel.php'; $objreader = new phpexcel_reader_html(); $objphpexcel = @$objreader->load($file_path.'generated.html'); header('content-type: application/vnd.ms-excel'); header('content-disposition: attachment;filename="example.xls"'); header('cache-control: max-age=0'); $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5'); $objwriter->save('php://output');
what modifications should do?
i think, phpexcel not supporting css , styles. here example code doing directly.
this excel.php
file, gives download of table generated inside file.
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="x-ua-compatible" content="ie=emulateie7"> <title>testing</title> </head> <style type="text/css"> body { font-family:verdana, arial, helvetica, sans-serif; font-size:12px; margin:0px; padding:0px; } </style> <html> <body> <?php $table = '<table border="0" cellpadding="0" cellspacing="0"><tr><td>'; $table .= '<tr><td rowspan="2" style="background-color:#000099;color:#ffffff;">test</td>'; $table .= '<td colspan="2" style="background-color:#ffff33">test</td>'; $table .= '<td colspan="2" style="background-color:#ffff33">test</td>'; $table .= '<td rowspan="2" style="background-color:#000099;">test<br>test</td>'; $table .= '</tr><tr><td style="background-color:#fffccc">test</td>'; $table .= '<td style="background-color:#fffccc">test</td>'; $table .= '<td style="background-color:#fffccc">test</td>'; $table .= '<td style="background-color:#fffccc">test</td>'; $table .= '</tr><tr></td></tr></table>'; header("content-type: application/x-msdownload"); header('content-disposition: attachment; filename="filename.xls"'); header("pragma: no-cache"); header("expires: 0"); echo $table; ?> </body> </html>
Comments
Post a Comment