0

We have saved Hindi & English data in our database table column by using PHP/Mysql. Now the same data should be exported in correct form: Hindi & English. English content is correctly written in xls file; but hindi content is not properly exported. Following is my code.

function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
    return;
}

function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
    return;
}

function xlsWriteLabel($Row, $Col, $Value) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
    return;
}

header("Content-Type: application/vnd.ms-excel; charset=UTF-8"); 
header("Pragma: public");
header("Expires: 0");
header('Content-Encoding: UTF-8');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=Report.xls");
header("Content-Transfer-Encoding: binary");

xlsBOF();
$columns = array('Col1', 'Col2', 'Col3');


for ($i = 0; $i < count($columns); $i++) {
    xlsWriteLabel(0, $i, $columns[$i]);
}

$xlsRow = 1;

// $inspectionsxls_array: Array contains data Hindi/Column.

if (!empty($inspectionsxls_array)) {
    foreach ($inspectionsxls_array as $list) {
        for ($j = 0; $j < count($columns); $j++) {
            xlsWriteLabel($xlsRow, $j, $list[$j]);
        }
        $xlsRow++;
    }
}

xlsEOF();
exit();
James Z
  • 12,104
  • 10
  • 27
  • 43
Sachin
  • 113
  • 2
  • 12
  • This is very old code that is copied and pasted across the internet, bugs and all. For instance, [sending multiple content type headers](https://stackoverflow.com/a/5809704/231316). My recommendation is to use a real [Excel-aware library](https://stackoverflow.com/a/5856529/231316) instead of this binary hack for what I assume is the 1995 Excel BIFF format. You could try using the `mb_` functions instead, but I’d read [this](https://stackoverflow.com/a/20999540/231316) first, too. – Chris Haas Dec 18 '21 at 11:15

0 Answers0