use PhpSpreadsheet Class หลังเครื่องหมายปีกกา จะขึ้น error HTTP ERROR 500

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา use PhpSpreadsheet Class หลังเครื่องหมายปีกกา จะขึ้น error HTTP ERROR 500

use PhpSpreadsheet Class หลังเครื่องหมายปีกกา จะขึ้น error HTTP ERROR 500
if  ($command == "Upload")
{	// include composer autoload
		require '../vendor/autoload.php';
		 
		// import the PhpSpreadsheet Class
		use PhpOfficePhpSpreadsheetSpreadsheet;
		use PhpOfficePhpSpreadsheetWriterXlsx;
		 
		$spreadsheet = new Spreadsheet();
		$sheet = $spreadsheet->getActiveSheet();
		 
		$sheet->setCellValue('A1', 'Hello World !'); 
		$sheet->setCellValue('B1', 'Test Value B1  !'); 
		 
		$writer = new Xlsx($spreadsheet);
		$output_file = "../temp/hello_world.xlsx";
		$writer->save($output_file);
		 
		if(file_exists($output_file))
			{	echo '<a href="'.$output_file.'" target="_blank">Download</a>';
			}

}  // end if  ($command == "Upload")

*** ลองรันโปรแกรมแล้ว จะขึ้น error

This page isn’t working

lct-pension.lion.co.th is currently unable to handle this request.

HTTP ERROR 500
**********
แต่ถ้า comment บรรทัด if และ บรรทัด { และบรรทัด }    โปรแกรมจะ run ได้ปกติ

??????  มีสมาชิกท่านใด เคยเจอปัญหาอันนี้บ้างไหมคะ
 

 


Pj_wanich 01-05-2025 14:53:51

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1

ลองปรับประมาณนี้ดู

<?php
require '../vendor/autoload.php';

use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;

if ($command == "Upload") {   
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
  
    $sheet->setCellValue('A1', 'Hello World !'); 
    $sheet->setCellValue('B1', 'Test Value B1 !'); 
  
    $writer = new Xlsx($spreadsheet);
    $output_file = "../temp/hello_world.xlsx";
    $writer->save($output_file);
  
    if (file_exists($output_file)) {
        echo '<a href="'.$output_file.'" target="_blank">Download</a>';
    }
}



ใน PHP, use ต้องอยู่ นอกเงื่อนไข และอยู่ บนสุด ของไฟล์ หลังจาก require หรือ include เท่านั้น ไม่สามารถใส่ไว้ใน block ของ if ได้



บทความแนะนำที่เกี่ยวข้อง
ออกรายงานเป็น Excel ไฟล์ ด้วย PhpSpreadsheet เบื้องต้น ตอนที่ 1อ่าน 18,230
ninenik 02-05-2025
 ความคิดเห็นที่ 2
ขอบคุณมากค่ะ


pj_wanich 02-05-2025 14:27






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ