×Warning! ใช้วิธีในบทความ PHPExcel: จัดรูปแบบ format ข้อมูล จะดีกว่าวิธีนี้
เขียนระบบส่งออกข้อมูล ทดสอบดูโดยใช้ LibreOffice Calc ปกติดี แต่ใน Microsoft Excel 2013 กลับแสดงตัวเลขทั้งหมดเป็นวันที่
งานรีบ (ตลอด) ไม่อยากเขียนเงื่อนไขให้เช็กว่าช่องนี้คอลัมน์นั้นเป็นตัวหนังสือ หรือว่าตัวเลข ก็ตั้งให้ที่ column เป็น text ไปเลยละกัน
<?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') ;
ที่แก้จริง ๆ คือ ใช้
$objWorkSheet->setCellValueExplicit ($colStrings[$key] . $rowNo, $value, PHPExcel_Cell_DataType::TYPE_STRING) ;
กรอกข้อมูลพร้อมกำหนดชนิดของข้อมูลใน column จะ formula, inline, null, numeric, string ที่ต้องการ จะใช้ชนิดอื่นก็ใช้ตามคู่มือ Class: PHPExcel_Cell_DataType