Php – html table to excel with PHP

export-to-excelPHP

I have looked for answer to other similar questions but couldn't see useful..

I have an html table generated with php from mysql db.
I use this fragment in my php file to save as excel

$file="program.xls";
header('Content-Type: text/html');
$table = $_POST['tablehidden'];//i get this from another php file.It is HTML table
header("Content-type: application/x-msexcel"); //tried adding  charset='utf-8' into header
header("Content-Disposition: attachment; filename=$file");
echo $table;
  1. Till this time everything is ok. But when I save this i get character problem and tried to add charset='utf-8' into my header, I don't know but It does not work.
  2. I have my table with style in HTML but when it comes to excel it lost the style which is implemented in css.

How can I solve the charset and styling problems? I have looked at phpexcel but couldn't see a method to include a table directly.

Best Answer

I personally would use a PHP Excel generating class to make the Excel file. You can find a good one called PHPExcel here: http://phpexcel.codeplex.com/