PHP Excel Export garbled problem
/**
* Functions for generating Excel files
* Author:walker
* @param $data Generate Excel data (two-dimensional array form)
* @param null $savefile generate the file name for Excel (the current timestamp is not specified)
* @param null $title generate Excel header (one-dimensional array form)
* @param string $sheetname generate the sheet name for Excel (default is Sheet1)
*/
function Exportexcel ($data, $savefile =null, $title =null, $sheetname = ' Sheet1 ') {
The current timestamp if no file name is specified
if (Is_null ($savefile)) {
$savefile =time ();
}
If you are referring to an Excel header, append the form to the content in front of the body.
if (Is_array ($title)) {
Array_unshift ($data, $title);
}
Import (' Org.Util.PHPExcel ');
Import (' Org.Util.PHPExcel.IOFactory ');
Import (' PHPExcel.Util.PHPExcel.Reader.Excel5 ');
$objPHPExcel = new \phpexcel ();
Excel content
foreach ($data as $k = = $v) {
$obj = $objPHPExcel->setactivesheetindex (0);
$row = $k +1;//Line
$nn = 0;
foreach ($v as $VV) {
$col =CHR (65+ $nn);//Column
$VV = Iconv ("UTF-8", "Gb2312//ignore", $VV);
$obj->setcellvalue ($col. $row, $VV);//column, row, value
$nn + +;
}
}
$objPHPExcel->getactivesheet ()->settitle ($sheetname);
$objPHPExcel->setactivesheetindex (0);
Header (' Content-type:application/vnd.ms-excel ');
Header (' Content-disposition:attachment;filename= '. $savefile. '. XLS "');
Header ("Content-type:application/vnd.ms-excel; Charset=utf-8 ");
Header (' cache-control:max-age=0 ');
$objWriter = \phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$objWriter->save (' php://output ');
Exit
}
/**
*
* Export Excel
*/
function Expuser () {//Export Excel
$xlstitle = array (' ID ', ' Product Code ', ' template number ', ' Product name ', ' Total inventory ', ' current inventory ', ' Supplier ');
$xlssql = M (' erp_inventory ');
$xlsData = $xlssql->getfield (' id,p_code,mid,pname,banlance,cur_banlance,factory ');
Show_bug ($xlsData);d ie;
$this->exportexcel ($xlsData, Time (), $xlstitle);
}
The above is my code, I use thinkphp framework, the second picture is the effect of the export, where the problem arises, how to solve?
------Solution--------------------
Now that you're using thinkphp, it's supposed to be utf-8.
The default character set for Phpexcel is also utf-8
Then why should
$VV = Iconv ("UTF-8", "Gb2312//ignore", $VV);
Turn the utf-8 into gb2312?
------Solution--------------------
Can't you read it? Did I not make myself clear? Looks like you can't accept the heuristic.
Delete all statements that use Iconv transcoding!
That should be clear, right?
Phpexcel written to Excel must be UTF-8 encoded data
That should be understood, right?
------Solution--------------------
Turn to GBK look, another iconv to Mars and traditional characters will have problems
$VV = mb_convert_encoding ($VV =, "GBK", "UTF-8");
------Solution--------------------
38 no need to delete
If you can't, just put a
------Solution--------------------
Yes, it's turned back.
As I said earlier, it's no problem for me to test your code snippets alone.
So the problem might be somewhere else: incoming data, data read from a table, even the Phpexcel class you're using.
Include ' plugin/phpexcel/classes/phpexcel.php ';
import (' Org.Util.PHPExcel.IOFactory ');
import (' PHPExcel.Util.PHPExcel.Reader.Excel5 ');