PHP Excel Export garbled problem

Source: Internet
Author: User
Tags php excel
/** * functions for generating Excel Files * Author:walker * @param $data generate Excel data (two-dimensional array form) * @param null $savefile generate the file name of Excel (The current timestamp is not specified) * @param null $title generate Excel header (one-dimensional array form) * @param string $sheetname generate Excel's sheet name (default is Sheet1) * /function Exportexcel ($data, $savefile =null, $title =null, $sheetname = ' Sheet1 ') {//If the file name is not specified is the current timestamp if (Is_nul        L ($savefile)) {$savefile =time ();        }//If the word is in the Excel header, append the form to the body content before the 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 ', ' suppliers ');        $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?

Reply to discussion (solution)

Comment out 29 lines $vv = Iconv ("UTF-8", "Gb2312//ignore", $VV);

Comment out 29 lines $vv = Iconv ("UTF-8", "Gb2312//ignore", $VV);


In the beginning there is no 29 lines, I added later, add not the effect of the same Oh!!

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?

Posted 52 lines, $xlsData printed part of the code, is a two-dimensional array, there should be no problem. Hoping to give the great God a little help.

$VV = Iconv ("UTF-8", "Gb2312//ignore", $VV);
$VV = Iconv ("UTF-8", "Utf-8//ignore", $VV);
$VV = Iconv ("GB2312", "Utf-8//ignore", $VV);
The above 3 kinds have tried, the result is same!!

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?

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?

Turn to GBK look, another iconv to Mars and traditional characters will have problems
$VV = mb_convert_encoding ($VV =, "GBK", "UTF-8");

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?

Understand, 29, 38 row deleted or useless, but also to delete where?

Turn to GBK look, another iconv to Mars and traditional characters will have problems
$VV = mb_convert_encoding ($VV =, "GBK", "UTF-8");

$VV =, what do you mean?

38 no need to delete

If you can't, just put a

Turn to GBK look, another iconv to Mars and traditional characters will have problems
$VV = mb_convert_encoding ($VV =, "GBK", "UTF-8");

$VV =, what do you mean?
Clerical errors, $VV

Turn to GBK look, another iconv to Mars and traditional characters will have problems
$VV = mb_convert_encoding ($VV =, "GBK", "UTF-8");

$VV =, what do you mean?
Clerical errors, $VV
Can't get over it,

38 no need to delete
If you can't, just put a

38 line up, is it because of the WPS reason? WPS used by the company, home with the office2007, but at present is garbled. How to break

Method One:
SetLocale (Lc_all, ' zh_cn ');
Method Two:
$str = mb_convert_encoding ($str, "UTF-8", "GBK"); Known source is GBK, converted to Utf-8
You can see blogs. Although I lead a CSV file, I think the truth should be the same.
http://blog.csdn.net/phpfenghuo/article/details/17483261

Method One:
SetLocale (Lc_all, ' zh_cn ');
Method Two:
$str = mb_convert_encoding ($str, "UTF-8", "GBK"); Known source is GBK, converted to Utf-8
You can see blogs. Although I lead a CSV file, I think the truth should be the same.

Method One: Lc_all is something
Method Two: Error
Where do you put these two sentences? I put in 28 lines, instead of $VV,

It's not clear how Untitled Spreadsheet, Unknown Creator.
Why is $obj = $objPHPExcel->setactivesheetindex (0);
Instead of $obj = $objPHPExcel->getactivesheet ();

Just test it alone, your starter code should be no problem, of course, there is no iconv
The problem is how Untitled Spreadsheet, Unknown Creator are produced in the document

It's not clear how Untitled Spreadsheet, Unknown Creator.
Why is $obj = $objPHPExcel->setactivesheetindex (0);
Instead of $obj = $objPHPExcel->getactivesheet ();

As you say, Untitled Spreadsheet, Unknown creator.

Turn to GBK look, another iconv to Mars and traditional characters will have problems
$VV = mb_convert_encoding ($VV =, "GBK", "UTF-8");

$VV =, what do you mean?
Clerical errors, $VV
Can't get over it,
Extension=php_mbstring.dll to expand this open.

Turn to GBK look, another iconv to Mars and traditional characters will have problems
$VV = mb_convert_encoding ($VV =, "GBK", "UTF-8");

$VV =, what do you mean?
Clerical errors, $VV
Can't get over it,
Extension=php_mbstring.dll to expand this open.
PHP Extension=php_mbstring.dll Open, restart Apache, export no error, but still garbled

Are you not aware of the error message?
A4 's worksheet ... Feuilles de calcul
The original
$objPHPExcel->getactivesheet ()->settitle ($sheetname);
$objPHPExcel->setactivesheetindex (0);
There's nothing wrong with that!

It's not clear how Untitled Spreadsheet, Unknown Creator.
Why is $obj = $objPHPExcel->setactivesheetindex (0);
Instead of $obj = $objPHPExcel->getactivesheet ();

As you say, Untitled Spreadsheet, Unknown creator.
Just read the wrong, it should be 28 rows, or garbled.

Are you not aware of the error message?
A4 's worksheet ... Feuilles de calcul
The original
$objPHPExcel->getactivesheet ()->settitle ($sheetname);
$objPHPExcel->setactivesheetindex (0);
There's nothing wrong with that!


The code is back in the beginning, the garbled Excel can I change the code in the Excel settings?

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 '); $data = Array (' Medium ', ' text ', ' C '), Array (' A ', ' B ', ' C '), Array (' A ', ' B ', ' C ');        SheetName = ' 123 '; $savefile = time ();        $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 (' GBK ', ' utf-8 ', $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 ');

Yes, there's a turn back.

I said earlier, I have no problem testing 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 '); $data = Array (' Medium ', ' text ', ' C '), Array (' A ', ' B ', ' C '), Array (' A ', ' B ', ' C ');        SheetName = ' 123 '; $savefile = time ();        $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 (' GBK ', ' utf-8 ', $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 ');

source data, and get all no problem, Excel class is under the official website, I Project Excel import also use this this package ah, should be no problem.

Turn to GBK look, another iconv to Mars and traditional characters will have problems
$VV = mb_convert_encoding ($VV =, "GBK", "UTF-8");

$VV =, what do you mean?
Clerical errors, $VV
Can't get over it,
Extension=php_mbstring.dll to expand this open.
PHP Extension=php_mbstring.dll Open, restart Apache, export no error, but still garbled
Header ("Content-type:application/vnd.ms-excel; Charset=utf-8 "); Comment out this sentence.

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 '); $data = Array (' Medium ', ' text ', ' C '), Array (' A ', ' B ', ' C '), Array (' A ', ' B ', ' C ');        SheetName = ' 123 '; $savefile = time ();        $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 (' GBK ', ' utf-8 ', $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 ');

I get a set of test data, export or garbled ah? You're not a test, no problem?

Well, I exported, exportexcel rewrite, or this method write a problem, or thank you.

You can see the error message in lines 5th and 11
You can remove the title, just keep the data body, test
Exportexcel it out in the middle of the line.
$objPHPExcel->getactivesheet ()->settitle ($sheetname);

You can see the error message in lines 5th and 11

You can remove the title, just keep the data body, test
Exportexcel it out in the middle of the line.
$objPHPExcel->getactivesheet ()->settitle ($sheetname);

It's still not working!

function Exportexcel ($data =array (), $title =array (), $filename = ' report ') {    header ("content-type:application/ Octet-stream ");    Header ("Accept-ranges:bytes");    Header ("Content-type:application/vnd.ms-excel");      Header ("Content-disposition:attachment;filename=". $filename. ". XLS ");    Header ("Pragma:no-cache");    Header ("expires:0");    Export xls start    if (!empty ($title)) {        foreach ($title as $k = = $v) {            $title [$k]=iconv ("UTF-8", "GB2312", $v); c10/>}        $title = implode ("\ T", $title);        echo "$title \ n";    }    if (!empty ($data)) {        foreach ($data as $key + $val) {            foreach ($val as $ck + = $CV) {                $data [$key] [$ck]= Iconv ("UTF-8", "GB2312", $CV);            }            $data [$key]=implode ("\ t", $data [$key]);                    }       echo implode ("\ n", $data);    } }

I use the above method to lead out no problem, the problem is that each group of data in each row of Excel table in a grid, not sorted by column. Can you change it for me?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.