Home >Backend Development >PHP Tutorial >PHP Spreadsheet_Excel_Reader imports excel Chinese display garbled_PHP tutorial

PHP Spreadsheet_Excel_Reader imports excel Chinese display garbled_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:57:191186browse

This article will introduce to you the solution to the problem that PHP Spreadsheet_Excel_Reader imports Excel into Chinese and displays garbled characters and cannot read files in .xlsx format. Students who have encountered such problems can refer to it.

Spreadsheet_Excel_Reader is a commonly used php class for importing excel files.
Under normal circumstances, the code to import excel using this class is as follows

$data->read('20110630_ctms.xls'); //Print output sheet array, single or multiple
The code is as follows
 代码如下 复制代码

//引入系统公共文件。
require_once ('../global.php');
//引入excel读取功能类
require_once ('./excel/reader.php');
//实例化
$data = new Spreadsheet_Excel_Reader();
//设置输出类型(可以是“GBK”或者“UTF-8”),注意插入数据库时的编码转换
$data->setOutputEncoding('CP936');
//读取excel文件地址
$data->read('20110630_ctms.xls');
//打印输出sheet数组,单个或多个
print_r($data->sheets);
echo '
';
//打印输出每个sheet的行数,$k从零开始
$k = 0;
echo $data->sheets[$k]['numRows'];

Copy code

//Introduce system public files.

require_once ('../global.php');
//Introduce excel reading function class
require_once ('./excel/reader.php');

//Instantiation

$data = new Spreadsheet_Excel_Reader();

//Set the output type (can be "GBK" or "UTF-8"), pay attention to the encoding conversion when inserting into the database

$data->setOutputEncoding('CP936');

//Read excel file address
print_r($data->sheets); echo '
';

//Print out the number of rows in each sheet, $k starts from zero

$k = 0; I encountered the following problems when using this class. 1. Cannot read files in .xlsx format. Files in .xlsx format cannot be read directly. The solution is to open the file and save it as an .xls file. Note: Please select "Microsoft Excel 5.0/95 workbook (*.xls)". This is easier to use. "Excel 97-2003 workbook (*.xls)" is also possible, but sometimes there will be problems, such as loss of read data.
2. Encoding problem. Chinese characters appear garbled.
$data->setOutputEncoding(‘CP936′); is used to set the output encoding, but you cannot output the desired encoding at will. You can try changing it to gbk or utf-8 and then convert it with iconv.
http://www.bkjia.com/PHPjc/632103.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632103.htmlTechArticleThis article will introduce to you how to solve the problem of garbled Chinese characters when imported into excel using PHP Spreadsheet_Excel_Reader and the inability to read files in .xlsx format. Methods, students who have encountered such problems can enter for reference. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn