getProperties()->setCreator("NEEC-Team") ->setLastModifiedBy("NEEC-Team") ->setTitle("exam_field") ->setSubject("exam_field") ->setDescription("exam_field") ->setKeywords("exam excel php nfe onie download") ->setCategory("exam_field"); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'SchoolID') ->setCellValue('B1', 'SchoolName'); $tmpfname = "test_excel.xlsx"; //กำหนดให้อ่านข้อมูลจากไฟล์จากไฟล์ชื่อ //สร้าง object สำหรับอ่านข้อมูล ชื่อ $excelReader $excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname); $excelObj = $excelReader->load($tmpfname); //อ่านข้อมูลจากไฟล์ชื่อ test_excel.xlsx $worksheet = $excelObj->getSheet(0); //อ่านข้อมูลจาก sheet แรก $lastRow = $worksheet->getHighestRow(); //นับว่า sheet แรกมีทั้งหมดกี่แถวแล้วเก็บจำนวนแถวไว้ในตัวแปรชื่อ $lastRow for ($row = 2; $row <= $lastRow; $row++) //วน loop อ่านข้อมูลเอามาแสดงทีละแถว { $objPHPExcel->getActiveSheet()->setCellValue('A' . $row, $worksheet->getCell('A' . $row)->getValue()); $objPHPExcel->getActiveSheet()->setCellValue('B' . $row, $worksheet->getCell('B' . $row)->getValue()); } $objPHPExcel->getActiveSheet()->setTitle('TestExport'); $objPHPExcel->setActiveSheetIndex(0); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="TestExport.xlsx"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit;