目錄
回复内容:
首頁 後端開發 php教程 php pack导出数据倒excel表中文乱码问题

php pack导出数据倒excel表中文乱码问题

Jun 06, 2016 pm 08:06 PM
php

需要用php将数据导出倒excel表中,用的是php的pack函数将字符打包成二进制,然后写入Excel表格文件,英文字符导入正常,现在中文字符全部乱码了,不知道哪里设置的问题呢?

<code><?php /**
 * Simple excel writer class with no external dependencies, drop it in and have fun
 * @author Matt Nowack
 * @link https://gist.github.com/ihumanable/929039/edit
 * @license Unlicensed
 * @version 1.0
 */
class Excel {
  private $col;
  private $row;
  private $data;
  private $title;
  
  /**
   * Safely encode a string for use as a filename
   * @param string $title The title to use for the file
   * @return string The file safe title
   */
  static function filename($title) {
    $result = strtolower(trim($title));
    $result = str_replace("'", '', $result);
    $result = preg_replace('#[^a-z0-9_]+#', '-', $result);
    $result = preg_replace('#\-{2,}#', '-', $result);
    return preg_replace('#(^\-+|\-+$)#D', '', $result);
  }
  
  /**
   * Builds a new Excel Spreadsheet object
   * @return Excel The Spreadsheet
   */
  function __construct($title) {
    $this->title = $title;
    $this-&gt;col = 0;
    $this-&gt;row = 0;
    $this-&gt;data = '';
    $this-&gt;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-&gt;title) . ".xls ");
    header("Content-Transfer-Encoding: binary ");
  }
  
  function send() {
    $this-&gt;eofMarker();
    // $this-&gt;headers();
    echo $this-&gt;data;
  }
  
  /**
   * Writes the Excel Beginning of File marker
   * @see pack()
   * @return nothing
   */
  private function bofMarker() { 
    $this-&gt;data .= pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
  }
  
  /**
   * Writes the Excel End of File marker
   * @see pack()
   * @return nothing
   */
  private function eofMarker() { 
    $this-&gt;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-&gt;col -= $amount;
    if($this-&gt;col col = 0;
    }
    return $this-&gt;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-&gt;col += $amount;
    return $this-&gt;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-&gt;row -= $amount;
    if($this-&gt;row row = 0;
    }
    return $this-&gt;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-&gt;row += $amount;
    return $this-&gt;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 way left, col = 0
   * @return nothing
   */
  function home() {
    $this-&gt;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-&gt;data .= pack("sssss", 0x203, 14, $this-&gt;row, $this-&gt;col, 0x0); 
    $this-&gt;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-&gt;data .= pack("ssssss", 0x204, 8 + $length, $this-&gt;row, $this-&gt;col, 0x0, $length); 
    $this-&gt;data .= $value; 
  }
}</code>
登入後複製
登入後複製

下面是我的调用测试代码:

<code><?php // iconv_set_encoding('output_encoding','gbk');
    require_once("excel.php");

    $xls = new Excel('Report');

    $colors = array(
        array("red", "blue", "green", "yellow", "orange", "purple"),
        array('过以上','以','c','过','目录','f')
    );

    foreach ($colors as $key => $value) {
        $xls-&gt;home();
        foreach ($value as  $row) {
          // $xls-&gt;home();
          $xls-&gt;label($row);
          $xls-&gt;right();
           // $xls-&gt;label($row);
        }
        $xls-&gt;down();
    }
    
    ob_start();
    $xls-&gt;send();
    $data = ob_get_clean();
    file_put_contents(__DIR__ .'/report.xls', $data);
</code>
登入後複製
登入後複製

回复内容:

需要用php将数据导出倒excel表中,用的是php的pack函数将字符打包成二进制,然后写入Excel表格文件,英文字符导入正常,现在中文字符全部乱码了,不知道哪里设置的问题呢?

<code><?php /**
 * Simple excel writer class with no external dependencies, drop it in and have fun
 * @author Matt Nowack
 * @link https://gist.github.com/ihumanable/929039/edit
 * @license Unlicensed
 * @version 1.0
 */
class Excel {
  private $col;
  private $row;
  private $data;
  private $title;
  
  /**
   * Safely encode a string for use as a filename
   * @param string $title The title to use for the file
   * @return string The file safe title
   */
  static function filename($title) {
    $result = strtolower(trim($title));
    $result = str_replace("'", '', $result);
    $result = preg_replace('#[^a-z0-9_]+#', '-', $result);
    $result = preg_replace('#\-{2,}#', '-', $result);
    return preg_replace('#(^\-+|\-+$)#D', '', $result);
  }
  
  /**
   * Builds a new Excel Spreadsheet object
   * @return Excel The Spreadsheet
   */
  function __construct($title) {
    $this->title = $title;
    $this-&gt;col = 0;
    $this-&gt;row = 0;
    $this-&gt;data = '';
    $this-&gt;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-&gt;title) . ".xls ");
    header("Content-Transfer-Encoding: binary ");
  }
  
  function send() {
    $this-&gt;eofMarker();
    // $this-&gt;headers();
    echo $this-&gt;data;
  }
  
  /**
   * Writes the Excel Beginning of File marker
   * @see pack()
   * @return nothing
   */
  private function bofMarker() { 
    $this-&gt;data .= pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
  }
  
  /**
   * Writes the Excel End of File marker
   * @see pack()
   * @return nothing
   */
  private function eofMarker() { 
    $this-&gt;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-&gt;col -= $amount;
    if($this-&gt;col col = 0;
    }
    return $this-&gt;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-&gt;col += $amount;
    return $this-&gt;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-&gt;row -= $amount;
    if($this-&gt;row row = 0;
    }
    return $this-&gt;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-&gt;row += $amount;
    return $this-&gt;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 way left, col = 0
   * @return nothing
   */
  function home() {
    $this-&gt;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-&gt;data .= pack("sssss", 0x203, 14, $this-&gt;row, $this-&gt;col, 0x0); 
    $this-&gt;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-&gt;data .= pack("ssssss", 0x204, 8 + $length, $this-&gt;row, $this-&gt;col, 0x0, $length); 
    $this-&gt;data .= $value; 
  }
}</code>
登入後複製
登入後複製

下面是我的调用测试代码:

<code><?php // iconv_set_encoding('output_encoding','gbk');
    require_once("excel.php");

    $xls = new Excel('Report');

    $colors = array(
        array("red", "blue", "green", "yellow", "orange", "purple"),
        array('过以上','以','c','过','目录','f')
    );

    foreach ($colors as $key => $value) {
        $xls-&gt;home();
        foreach ($value as  $row) {
          // $xls-&gt;home();
          $xls-&gt;label($row);
          $xls-&gt;right();
           // $xls-&gt;label($row);
        }
        $xls-&gt;down();
    }
    
    ob_start();
    $xls-&gt;send();
    $data = ob_get_clean();
    file_put_contents(__DIR__ .'/report.xls', $data);
</code>
登入後複製
登入後複製

你中文是什么编码的啊?Excel默认是GB2312的。如果UTF-8就乱码了。

解决办法:要么转成GB2312再写进去。
要么在最后先输出BOM头

    ob_start();
    $xls->send();
    $data = "\xEF\xBB\xBF" . ob_get_clean();
    file_put_contents(__DIR__ .'/report.xls', $data);
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

See all articles