×Warning! ใช้วิธีในบทความ PHPExcel: จัดรูปแบบ format ข้อมูล จะดีกว่าวิธีนี้
เขียนระบบส่งออกข้อมูล ทดสอบดูโดยใช้ LibreOffice Calc ปกติดี แต่ใน Microsoft Excel 2013 กลับแสดงตัวเลขทั้งหมดเป็นวันที่
งานรีบ (ตลอด) ไม่อยากเขียนเงื่อนไขให้เช็กว่าช่องนี้คอลัมน์นั้นเป็นตัวหนังสือ หรือว่าตัวเลข ก็ตั้งให้ที่ column เป็น text ไปเลยละกัน
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | <?php error_reporting (E_ALL); ini_set ( 'display_errors' , true); ini_set ( 'display_startup_errors' , true); include 'config.php' ; /* PHPExcel_IOFactory - Reader */ include '../vendor/phpoffice/phpexcel/Classes/PHPExcel.php' ; $objPHPExcel = new PHPExcel(); /* Set default style */ $defaultStyle = $objPHPExcel ->getDefaultStyle(); $defaultStyle ->getFont() ->setName( 'Arial' ) ->setSize(11); $defaultStyle ->getNumberFormat() ->setFormatCode( 'yyyy-mm-dd' ); /* Set document properties */ $title = 'Subscription_Datas_' . date ( 'Y-m-d_H-i' ); $objPHPExcel ->getProperties()->setCreator( 'Pitt Phunsanit' ) ->setCategory( 'Subscription Datas' ) ->setDescription( $title ) ->setKeywords( 'Subscription Datas ' . date ( 'Y-m-d' )) ->setSubject( $title ) ->setTitle( $title ); /* create new sheet */ $objWorkSheet = $objPHPExcel ->getActiveSheet(); $objWorkSheet ->setTitle( 'Subscription Datas' ); $objWorkSheet ->setCellValue( 'A1' , 'subscription_id' ); $objWorkSheet ->setCellValue( 'B1' , 'first name' ); $objWorkSheet ->setCellValue( 'C1' , 'last name' ); $objWorkSheet ->setCellValue( 'D1' , 'email' ); $objWorkSheet ->setCellValue( 'E1' , 'mobile' ); $objWorkSheet ->setCellValue( 'F1' , 'major' ); $objWorkSheet ->setCellValue( 'G1' , 'congratulations year' ); $objWorkSheet ->setCellValue( 'H1' , 'date create' ); $sql = "SELECT * FROM subscriptions WHERE enable = 1 ORDER BY sort ASC, skill_id ASC;"; $query = $conn ->prepare( $sql ); $query ->execute(); $results = $query ->fetchAll(PDO::FETCH_ASSOC); $colNo = -1; $rowNo = 1; foreach ( $results [0] as $key => $value ) { $colNo ++; $colStrings [ $key ] = $column = PHPExcel_Cell::stringFromColumnIndex( $colNo ); $objWorkSheet ->setCellValue( $column . $rowNo , $key ); } $objPHPExcel ->getActiveSheet()->getStyle( 'A1:' . $column . '1' )->getFont()->setBold(true); foreach ( $results as $result ) { $rowNo ++; foreach ( $result as $key => $value ) { $objWorkSheet ->setCellValueExplicit( $colStrings [ $key ] . $rowNo , $value , PHPExcel_Cell_DataType::TYPE_STRING); } } $objWriter = PHPExcel_IOFactory::createWriter( $objPHPExcel , 'Excel2007' ); header( 'Content-Type: application/vnd.ms-excel' ); header( 'Content-Disposition: attachment; filename="' . $title . '.xlsx"' ); header( 'Cache-Control: max-age=0' ); header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Cache-Control: post-check=0, pre-check=0' , false); header( 'Pragma: no-cache' ); $objWriter ->save( 'php://output' ); |
ที่แก้จริงๆ คือ ใช้
1 | $objWorkSheet ->setCellValueExplicit( $colStrings [ $key ] . $rowNo , $value , PHPExcel_Cell_DataType::TYPE_STRING); |
กรอกข้อมูลพร้อมกำหนดชนิดของข้อมูลใน column จะ formula, inline, null, numeric, string ที่ต้องการ จะใช้ชนิดอื่นก็ใช้ตามคู่มือ Class: PHPExcel_Cell_DataType
About the author