When an excel file is generated in php, an error is reported ?!!!

Source: Internet
Author: User
{Code...} is an error reported when an EXCEL file is opened? Why? (What's even more confusing is that the same code block works perfectly in another project !) Supplement: When I found my question, I tried it, but I didn't have a feasible TXT file. When I opened it, I found garbled characters, so I...
Public function check_daochu ($ data) {ob_end_clean (); // *** add $ this-> load-> library ('phpexcel ') here '); $ this-> load-> library ('phpexcel/IOFactory '); $ objPHPExcel = new PHPExcel (); $ objWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel ); // set the header $ this-> Excel_bt ($ objPHPExcel, $ data ['bt _ data']); // export the class capacity $ this-> Excel_lr ($ objPHPExcel, $ data ['data']); // set the cell width $ objPHPExcel-> getActiveSheet ()-> getColumnDimension ('B')-> setWidth (15 ); foreach ($ data ['hb _ data'] as $ key => $ value) {// merge cells $ objPHPExcel-> getActiveSheet ()-> mergeCells ($ key. ':'. $ value);} // horizontal vertical center setting $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getAlignment ()-> setVertical (PHPExcel_Style_Alignment :: VERTICAL_CENTER); $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER ); $ fieldname = iconv ('utf-8', 'gb2312', date ("Y-m-d", time ()). '_'. $ data ['bt _ data'] ['a1']); ob_end_clean (); header ("Pragma: public"); header ("Expires: 0 "); header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0"); header ("Content-Type: application/force-download "); header ("Content-Type: application/vnd. ms-execl "); header (" Content-Type: application/octet-stream "); header (" Content-Type: application/download "); header ('content-Disposition: attachment; filename = "'. $ fieldname. '.xlsx "'); header (" Content-Transfer-Encoding: binary "); $ objWriter-> save ('php: // output ');} // set the export title to public function Excel_bt ($ objPHPExcel, $ bt_data) {foreach ($ bt_data as $ key => $ value) {$ objPHPExcel-> getActiveSheet () -> setCellValue ($ key, $ value) ;}// set the exported public function Excel_lr ($ objPHPExcel, $ select_data) {$ I = 3; $ data = array ('A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h ', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R ', 'S ', 't', 'U', 'V', 'w', 'x', 'y', 'z '); foreach ($ select_data as $ key = >$ value) {$ j = 0; foreach ($ value as $ k = >$ v) {$ objPHPExcel-> getActiveSheet () -> setCellValue ($ data [$ j]. $ I, $ v); $ j ++;} $ I ++ ;}}

An error is reported when an EXCEL file is opened? Why? (What's even more confusing is that the same code block runs perfectly in another project.!)

Supplement: I found the problem and tried the following, but none of them is feasible.

  1. Generate me.txtFile, garbled characters are found when opening, so I think of character encoding problems, so incheck_daochuIn this method, use the data arrayiconvFunction Conversion code, no!

  2. Add it to the header fileheader("Content-Type:application/vnd.ms-execl;charset=utf-8");No!

  3. Put the sameClass LibraryAndCode blockAll of them are covered. No way!

Open the. txt file.

I wiped and solved the problem !!!Buffer ProblemsAfter reading this article, I can understand that I added it before the output.ob_end_cleanBut this is not enough. Addob_end_cleanIncheck_daochuThe first line of the method. I don't know why I need to add one here, but the problem has been solved.

Reply content:
Public function check_daochu ($ data) {ob_end_clean (); // *** add $ this-> load-> library ('phpexcel ') here '); $ this-> load-> library ('phpexcel/IOFactory '); $ objPHPExcel = new PHPExcel (); $ objWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel ); // set the header $ this-> Excel_bt ($ objPHPExcel, $ data ['bt _ data']); // export the class capacity $ this-> Excel_lr ($ objPHPExcel, $ data ['data']); // set the cell width $ objPHPExcel-> getActiveSheet ()-> getColumnDimension ('B')-> setWidth (15 ); foreach ($ data ['hb _ data'] as $ key => $ value) {// merge cells $ objPHPExcel-> getActiveSheet ()-> mergeCells ($ key. ':'. $ value);} // horizontal vertical center setting $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getAlignment ()-> setVertical (PHPExcel_Style_Alignment :: VERTICAL_CENTER); $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER ); $ fieldname = iconv ('utf-8', 'gb2312', date ("Y-m-d", time ()). '_'. $ data ['bt _ data'] ['a1']); ob_end_clean (); header ("Pragma: public"); header ("Expires: 0 "); header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0"); header ("Content-Type: application/force-download "); header ("Content-Type: application/vnd. ms-execl "); header (" Content-Type: application/octet-stream "); header (" Content-Type: application/download "); header ('content-Disposition: attachment; filename = "'. $ fieldname. '.xlsx "'); header (" Content-Transfer-Encoding: binary "); $ objWriter-> save ('php: // output ');} // set the export title to public function Excel_bt ($ objPHPExcel, $ bt_data) {foreach ($ bt_data as $ key => $ value) {$ objPHPExcel-> getActiveSheet () -> setCellValue ($ key, $ value) ;}// set the exported public function Excel_lr ($ objPHPExcel, $ select_data) {$ I = 3; $ data = array ('A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h ', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R ', 'S ', 't', 'U', 'V', 'w', 'x', 'y', 'z '); foreach ($ select_data as $ key = >$ value) {$ j = 0; foreach ($ value as $ k = >$ v) {$ objPHPExcel-> getActiveSheet () -> setCellValue ($ data [$ j]. $ I, $ v); $ j ++;} $ I ++ ;}}

An error is reported when an EXCEL file is opened? Why? (What's even more confusing is that the same code block runs perfectly in another project.!)

Supplement: I found the problem and tried the following, but none of them is feasible.

  1. Generate me.txtFile, garbled characters are found when opening, so I think of character encoding problems, so incheck_daochuIn this method, use the data arrayiconvFunction Conversion code, no!

  2. Add it to the header fileheader("Content-Type:application/vnd.ms-execl;charset=utf-8");No!

  3. Put the sameClass LibraryAndCode blockAll of them are covered. No way!

Open the. txt file.

I wiped and solved the problem !!!Buffer ProblemsAfter reading this article, I can understand that I added it before the output.ob_end_cleanBut this is not enough. Addob_end_cleanIncheck_daochuThe first line of the method. I don't know why I need to add one here, but the problem has been solved.

Guess$objPHPExcelParameter passing must be referenced

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.