700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > php pack导出数据倒excel表中文乱码问题

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

时间:2022-03-31 08:04:58

相关推荐

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

后端开发|php教程

php

后端开发-php教程

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

网页版博彩源码,ubuntu老是失去键盘,tomcat稳定性怎么样,爬虫如何突破限制,学习php还可以做什么外包,seo外包公lzw

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, est: .) $this->data .= pack("ssssss", 0x204, 8 + $length, $this->row, $this->col, 0x0, $length);$this->data .= $value; }}

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

易语言工具箱源码,ubuntu终端使用代理,tomcat搭建系统无法刷新,windows 爬虫开发,php配置外网访问,重庆seo关键词排名优化学习lzw

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

微代言系统源码,vscode 的作用,ubuntu gid,dubbo部署tomcat,sqlite中文排序c,js网页加载特效插件,web前端框架可以做什么,猫影视爬虫规则详解,php 图片上传 显示,seo服务投放费用,类似站酷的素材网站,网页设计返回按钮,怎么上传dede模板lzw

回复内容:

需要用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 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, est: .) $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);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。