Tuesday, April 29, 2014

PHP Arabic Export Excel Problem

 PHP Arabic Excel Export


there are many problems in encoding when you export arabic excel sheet, this is solved here


// convert to excel

function toExcel($data, $filename) {
echo "
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='application/vnd.ms-excel; charset=UTF-8'>
<style type='text/css'>
*{
direction: ltr;
text-align: center;
font: 11px arial;
border-collapse: collapse;
}
table,td,tr{
border: #ebebeb solid 1px;
}
.msg{
color:#ff0000;
font-size=13px;
}
</style>";

header('Pragma: public'); // required
header('http-equiv=Content-Type Content-type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment; filename=' . $filename . '.xls');

echo "</head>
<body>";
//        //echo the entire table headers
echo '<table>';
$headers = array();
foreach ($data as $row) {
foreach ($row as $key => $value)
$headers[] = $key;
break;
}

echo "<tr>";
foreach ($headers as $value)
echo "<th>$value</th>";
echo "</tr>";
foreach ($data as $row) {
echo '<tr>';
foreach ($row as $val)
$this->writeRow($val);
echo '</tr>';
}
echo '</table></body></html>';
}

// create table cell
private function writeRow($val) {
echo '<td>' . $val . '</td>';
}

0 comments:

Post a Comment