Use phpexcel to export excel garbled characters

Source: Internet
Author: User
Use phpexcel to export the excel garbled database and the page is utf8.
Header ('content-Type: application/vnd. ms-excel ;');
Header ('content-Disposition: attachment?filename='.w.filename.'.xls ');
Header ('cache-Control: max-age = 0 ');
$ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5 ');
$ ObjWriter-> save ('php: // output ');//
After export, execel is garbled.
If you export data in a local window, it will be fine. if you upload the data to the server for export, it will be garbled.
Directly var_dump ($ ob1_reter) to the page and no garbled characters are found.
Header ('content-Type: application/vnd. ms-excel; charset = gb2312 ') is garbled;
What is this garbled code?


Reply to discussion (solution)

Add the ob_end_clean () function before the header () to clear the buffer. this will not cause garbled characters!
Ob_end_clean (); // clear the buffer to avoid garbled characters.
Header ('content-Type: application/vnd. ms-excel ');
.....
.....

$ ObjWriter-> save ('php: // output'); outputs a binary document stream, which is similar to the image and does not contain character sets.
Opening in excel is garbled because the file contains content that cannot be recognized by him.
Most of the errors include php errors.
You can take a screenshot and paste it, or you can put the xls file on the network disk.

Add the ob_end_clean () function before the header () to clear the buffer. this will not cause garbled characters!
Ob_end_clean (); // clear the buffer to avoid garbled characters.
Header ('content-Type: application/vnd. ms-excel ');
.....
.....




This is indeed the problem, and it cannot afford to hurt the buffer zone.

$ ObjWriter-> save ('php: // output'); outputs a binary document stream, which is similar to the image and does not contain character sets.
Opening in excel is garbled because the file contains content that cannot be recognized by him.
Most of the errors include php errors.
You can take a screenshot and paste it, or you can put the xls file on the network disk.





Add the ob_end_clean () function before the header () to clear the buffer. this will not cause garbled characters!
Ob_end_clean (); // clear the buffer to avoid garbled characters.
Header ('content-Type: application/vnd. ms-excel ');
.....
.....




This is indeed the problem, and it cannot afford to hurt the buffer zone.

No. I used a local test. alas! You can do it locally.

The content in your cell A1.
Check whether there are redundant output.

Up/down code
/* -------------- Set the header information ------------------*/
$ ObjPHPExcel-> setActiveSheetIndex (0)
-> SetCellValue ('A1', 'A ')
-> SetCellValue ('b1 ',' B ')
-> SetCellValue ('C1', 'C ')
-> SetCellValue ('d1 ', 'D ')
-> SetCellValue ('e1 ', 'E ')
-> SetCellValue ('F1', 'F ')
-> SetCellValue ('G1', 'g ')
-> SetCellValue ('h1 ', 'H ')
-> SetCellValue ('i1', I ')
-> SetCellValue ('j1', 'F ');
/* -------------- Start to extract information from the database and insert it into the Excel table ------------------*/
$ I = 2; // defines an I variable to control the number of rows in cyclic output data.
For ($ j = 0; $ j // $ Rm = iconv ("GB2312", "UTF-8", $ rs [1]); // Encode the characters to convert the Chinese characters of GB2312 in the database to the UTF-8 format
$ ObjPHPExcel-> setActiveSheetIndex (0)
-> SetCellValue ("A". $ I, $ result [$ j] ['ori _ stu_name '])
-> SetCellValue ("B". $ I, $ result [$ j] ['ori _ stu_candidateNum '])
-> SetCellValue ("C". $ I, $ result [$ j] ['ori _ stu_sex '])
-> SetCellValue ("D". $ I, $ result [$ j] ['ori _ stu_id_card '])
-> SetCellValue ("E". $ I, $ result [$ j] ['ori _ stu_institution '])
-> SetCellValue ('F'. $ I, $ result [$ j] ['ori _ stu_specialty '])
-> SetCellValue ('g'. $ I, $ result [$ j] ['ori _ stu_phone '])
-> SetCellValue ('H'. $ I, $ result [$ j] ['ori _ stu_isMember '])
-> SetCellValue ('I'. $ I, $ result [$ j] ['ori _ stu_domicile '])
-> SetCellValue ('j'. $ I, $ result [$ J] ['ori _ stu_isReport ']);
$ I ++;
}

/* -------------- The following shows how to set other information ------------------*/
$ ObjPHPExcel-> getActiveSheet ()-> setTitle ('student table'); // Set the sheet name
$ ObjPHPExcel-> setActiveSheetIndex (0); // you can specify the start position of a sheet.
$ Filename = date ('Y-M', time (). "Information Table ";
Header ('content-Type: application/vnd. ms-excel ');
Header ('content-Disposition: attachment?filename='.w.filename.'.xls ');
Header ('cache-Control: max-age = 0 ');
$ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5 ');
$ ObjWriter-> save ('php: // output'); // an excel file is generated and downloaded.

The content in your cell A1.
Check whether there are redundant output.



Let me code it.
Vendor ("Excel. PHPExcel "); Vendor (" Excel. PHPExcel. IOFactory "); $ objPHPExcel = new PHPExcel (); $ db = M (" student_info "); $ where =" where fsi_logistics.ori_log_stuCandidateNum = batch "; $ SQL =" select logistics. *, student_info.ori_stu_name from student_info, logistics ". $ where; $ result = $ db-> query ($ SQL); $ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('A1', 'name ') -> setCellValue ('b1 ', 'Student id')-> setCellValue ('C1 ', 'commodity')-> setCellValue ('d1 ', 'price '); $ I = 2; for ($ j = 0; $ j
 
  
SetActiveSheetIndex (0)-> setCellValue ("". $ I, $ result [$ j] ['ori _ stu_name '])-> setCellValue ("B ". $ I, $ result [$ j] ['ori _ log_stuCandidateNum '])-> setCellValue ("C ". $ I, $ result [$ j] ['product'])-> setCellValue ("D ". $ I, $ result [$ j] ['price']); $ I ++;} $ objPHPExcel-> getActiveSheet ()-> setTitle ('student table '); $ objPHPExcel-> setActiveSheetIndex (0); $ filename = date ('Y-M', time ()). "Daily necessities table"; ob_end_clean (); header ('content-Type: application/vnd. ms-excel '); header ('content-Disposition: attachment?filename='.w.filename.'.xls'); header ('cache-Control: max-age = 0'); $ objWriter = PHPExcel_IOFactory :: createWriter ($ objPHPExcel, 'excel5'); $ objWriter-> save ('php: // output'); exit;
 

There are no other outputs. export in winddow is normal.

I have solved the bom header problem. thank you.

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.