PHP Pack Export Data Inverted Excel table Chinese garbled problem

Source: Internet
Author: User
Keywords Php
Need to use PHP to export data into the Excel table, using the PHP Pack function to package the characters into binary, and then write to Excel table file, English characters import normal, now Chinese characters are all garbled, do not know where to set the problem?


  title = $title;    $this->col = 0;    $this->row = 0;    $this->data = ";  $this->bofmarker (); }/** * Transmits the proper headers to cause a download to occur and to identify the file properly * @return Nothi    NG */Function headers () {header ("content-type:application/force-download");    Header ("Content-type:application/octet-stream");    Header ("Content-type:application/download"); Header ("Content-type:application/vnd.ms-excel;    Charset=iso-8859-1 "); Header ("Content-disposition:attachment;filename=". Excel::filename ($this->title).    ". xls");  Header ("Content-transfer-encoding:binary");    } function Send () {$this->eofmarker ();    $this->headers ();  Echo $this->data; }/** * Writes the Excel Beginning of File marker * @see Pack () * @return nothing */Private function Bofmarke    R () {$this->data. = Pack ("Ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); }/** * Writes the Excel End of File marker *@see Pack () * @return nothing */Private Function Eofmarker () {$this->data. = Pack ("ss", 0x0A, 0x00); }/** * Moves internal cursor left by the amount specified * @param optional integer $amount The amount to move lef T by, defaults to 1 * @return An integer The current column after the move */function Left ($amount = 1) {$this-    Col-= $amount;    if ($this->col < 0) {$this->col = 0;  } return $this->col; }/** * Moves internal cursor Right by the amount specified * @param optional integer $amount the amount to move RI Ght by, defaults to 1 * @return An integer The current column after the move */function Right ($amount = 1) {$this-&    Gt;col + = $amount;  return $this->col; }/** * Moves internal cursor up by amount * @param optional integers $amount The amount to move up by, defaults to    1 * @return Integer The current row after the move */function up ($amount = 1) {$this->row-= $amount; if ($thiS->row < 0) {$this->row = 0;  } return $this->row;  }/** * Moves internal cursor down by amount * @param Optional integer $amount The amount-Move down by, defaults    to 1 * @return An integer The current row after the move */function down ($amount = 1) {$this->row + = $amount;  return $this->row; }/** * Moves internal cursor to the top of the page, row = 0 * @return Nothing */function Top () {$this-&gt  ; row = 0; }/** * Moves internal cursor All the the-left, col = 0 * @return Nothing * * Function Home () {$this->col  = 0;  }/** * Writes a number to the Excel Spreadsheet * @see pack () * @param an integer $value the value to write out * @return Nothing * * function number ($value) {$this->data. = Pack ("Sssss", 0x203, $this->row, $this->c     OL, 0x0);   $this->data. = Pack ("D", $value); }/** * Writes a string (or label) to the Excel Spreadsheet * @see pack () * @param string $value the value to write out * @return Nothing */function label ($value) {$length = strlen ($value);    Ob_iconv_handler ()//$value = Iconv (' UTF-8 ', ' GBK ', $value);    File_put_contents (' a.txt ', ' Test: '.)     $this->data. = Pack ("Ssssss", 0x204, 8 + $length, $this->row, $this->col, 0x0, $length);   $this->data. = $value; }}

Here is my call to test the code:


  
    $value) {        $xls->home();        foreach ($value as  $row) {          // $xls->home();          $xls->label($row);          $xls->right();           // $xls->label($row);        }        $xls->down();    }        ob_start();    $xls->send();    $data = ob_get_clean();    file_put_contents(__DIR__ .'/report.xls', $data);

Reply content:

Need to use PHP to export data into the Excel table, using the PHP Pack function to package the characters into binary, and then write to Excel table file, English characters import normal, now Chinese characters are all garbled, do not know where to set the problem?


  title = $title;    $this->col = 0;    $this->row = 0;    $this->data = ";  $this->bofmarker (); }/** * Transmits the proper headers to cause a download to occur and to identify the file properly * @return Nothi    NG */Function headers () {header ("content-type:application/force-download");    Header ("Content-type:application/octet-stream");    Header ("Content-type:application/download"); Header ("Content-type:application/vnd.ms-excel;    Charset=iso-8859-1 "); Header ("Content-disposition:attachment;filename=". Excel::filename ($this->title).    ". xls");  Header ("Content-transfer-encoding:binary");    } function Send () {$this->eofmarker ();    $this->headers ();  Echo $this->data; }/** * Writes the Excel Beginning of File marker * @see Pack () * @return nothing */Private function Bofmarke    R () {$this->data. = Pack ("Ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); }/** * Writes the Excel End of File marker *@see Pack () * @return nothing */Private Function Eofmarker () {$this->data. = Pack ("ss", 0x0A, 0x00); }/** * Moves internal cursor left by the amount specified * @param optional integer $amount The amount to move lef T by, defaults to 1 * @return An integer The current column after the move */function Left ($amount = 1) {$this-    Col-= $amount;    if ($this->col < 0) {$this->col = 0;  } return $this->col; }/** * Moves internal cursor Right by the amount specified * @param optional integer $amount the amount to move RI Ght by, defaults to 1 * @return An integer The current column after the move */function Right ($amount = 1) {$this-&    Gt;col + = $amount;  return $this->col; }/** * Moves internal cursor up by amount * @param optional integers $amount The amount to move up by, defaults to    1 * @return Integer The current row after the move */function up ($amount = 1) {$this->row-= $amount; if ($thiS->row < 0) {$this->row = 0;  } return $this->row;  }/** * Moves internal cursor down by amount * @param Optional integer $amount The amount-Move down by, defaults    to 1 * @return An integer The current row after the move */function down ($amount = 1) {$this->row + = $amount;  return $this->row; }/** * Moves internal cursor to the top of the page, row = 0 * @return Nothing */function Top () {$this-&gt  ; row = 0; }/** * Moves internal cursor All the the-left, col = 0 * @return Nothing * * Function Home () {$this->col  = 0;  }/** * Writes a number to the Excel Spreadsheet * @see pack () * @param an integer $value the value to write out * @return Nothing * * function number ($value) {$this->data. = Pack ("Sssss", 0x203, $this->row, $this->c     OL, 0x0);   $this->data. = Pack ("D", $value); }/** * Writes a string (or label) to the Excel Spreadsheet * @see pack () * @param string $value the value to write out * @return Nothing */function label ($value) {$length = strlen ($value);    Ob_iconv_handler ()//$value = Iconv (' UTF-8 ', ' GBK ', $value);    File_put_contents (' a.txt ', ' Test: '.)     $this->data. = Pack ("Ssssss", 0x204, 8 + $length, $this->row, $this->col, 0x0, $length);   $this->data. = $value; }}

Here is my call to test the code:


  
    $value) {        $xls->home();        foreach ($value as  $row) {          // $xls->home();          $xls->label($row);          $xls->right();           // $xls->label($row);        }        $xls->down();    }        ob_start();    $xls->send();    $data = ob_get_clean();    file_put_contents(__DIR__ .'/report.xls', $data);

What is the code of your Chinese? Excel is GB2312 by default. If the UTF-8 is garbled.

Solution: Either turn it into a GB2312 and write it in.
Or output BOM header at the end of the first

    ob_start();    $xls->send();    $data = "\xEF\xBB\xBF" . ob_get_clean();    file_put_contents(__DIR__ .'/report.xls', $data);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.