php pack匯出資料倒excel表中文亂碼問題

來源:互聯網
上載者:User
關鍵字 php
需要用php將資料匯出倒excel表中,用的是php的pack函數將字元打包成二進位,然後寫入Excel表格檔案,英文字元匯入正常,現在中文字元全部亂碼了,不知道哪裡設定的問題呢?

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 nothing   */  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 bofMarker() {     $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 left by, defaults to 1   * @return 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 right by, defaults to 1   * @return integer The current column after the move   */  function right($amount = 1) {    $this->col += $amount;    return $this->col;  }    /**   * Moves internal cursor up by amount   * @param optional integer $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 to move down by, defaults to 1   * @return 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->row = 0;  }    /**   * Moves internal cursor all the way left, col = 0   * @return nothing   */  function home() {    $this->col = 0;  }    /**   * Writes a number to the Excel Spreadsheet   * @see pack()   * @param integer $value The value to write out   * @return nothing   */  function number($value) {     $this->data .= pack("sssss", 0x203, 14, $this->row, $this->col, 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;   }}

下面是我的調用測試代碼:

 $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);

回複內容:

需要用php將資料匯出倒excel表中,用的是php的pack函數將字元打包成二進位,然後寫入Excel表格檔案,英文字元匯入正常,現在中文字元全部亂碼了,不知道哪裡設定的問題呢?

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 nothing   */  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 bofMarker() {     $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 left by, defaults to 1   * @return 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 right by, defaults to 1   * @return integer The current column after the move   */  function right($amount = 1) {    $this->col += $amount;    return $this->col;  }    /**   * Moves internal cursor up by amount   * @param optional integer $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 to move down by, defaults to 1   * @return 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->row = 0;  }    /**   * Moves internal cursor all the way left, col = 0   * @return nothing   */  function home() {    $this->col = 0;  }    /**   * Writes a number to the Excel Spreadsheet   * @see pack()   * @param integer $value The value to write out   * @return nothing   */  function number($value) {     $this->data .= pack("sssss", 0x203, 14, $this->row, $this->col, 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;   }}

下面是我的調用測試代碼:

 $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);

你中文是什麼編碼的啊?Excel預設是GB2312的。如果UTF-8就亂碼了。

解決辦法:要麼轉成GB2312再寫進去。
要麼在最後先輸出BOM頭

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

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.