需要用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->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 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 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; } }</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->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); </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->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 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 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; } }</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->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); </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);

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

Dreamweaver Mac版
ビジュアル Web 開発ツール

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

ホットトピック



