สร้างไฟล์ XLS ด้วย PHP

ฟังก์ชั่นสำหรับสร้างไฟล์ ฟอร์แมต XLS จาก PHP พร้อมตัวอย่างการใช้งาน อย่างง่ายๆ ครับ

<?
  //header สำหรับดาวน์โหลดเป็นไฟล์
  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/octet-stream");
  header("Content-Type: application/download");;
  header("Content-Disposition: attachment;filename=test.xls "); // ชื่อไฟล์ .XLS สำหรับดาวน์โหลด
  header("Content-Transfer-Encoding: binary ");

  //function
  function xlsHeader()
  {
    echo pack( "ssssss" , 0x809 , 0x8 , 0x0 , 0x10 , 0x0 , 0x0 );
    return;
  }

  function xlsClose()
  {
    echo pack( "ss" , 0x0A , 0x00 );
    return;
  }

  function xlsWriteNumber( $Row , $Col , $Value )
  {
    echo pack( "sssss" , 0x203 , 14 , $Row , $Col , 0x0 );
    echo pack( "d" , $Value );
    return;
  }

  function xlsWriteString( $Row , $Col , $Value )
  {
    $L = strlen( $Value );
    echo pack( "ssssss" , 0x204 , 8 + $L , $Row , $Col , 0x0 , $L );
    echo $Value;
    return;
  }

  //เริ่มต้นเขียน header ของ XLS
  xlsHeader();
  //เขียน header เป็น string
  xlsWriteString( 0 , 0 , 'h1' );
  xlsWriteString( 0 , 1 , 'h2' );
  xlsWriteString( 0 , 2 , 'h3' );
  //แถวที่ 1
  xlsWriteString( 1 , 0 , 'ทดสอบ' );
  xlsWriteString( 1 , 1 , 'R1C2' );
  xlsWriteNumber( 1 , 2 , '1' );
  //แถวที่2
  xlsWriteString( 2 , 0 , 'R2C1' );
  xlsWriteString( 2 , 1 , 'R2C2' );
  xlsWriteNumber( 2 , 2 , '2' );
  //ปิดเอกสาร
  xlsClose();
?>

คงไม่ต้องอธิบายมากนะครับ ให้ output เป็นไฟล์ ฟอร์แมต XLS เลย อยากรู้แนะนำให้เอาตัวอย่างไปทดสอบเอาได้เลยครับ

หรือถ้าต้องการแบบที่มีคุณสมบัติมากกว่านี้ซักหน่อย ลองดูที่นี่ครับ http://www.bettina-attack.de/...ects/php_writeexcel/
ผู้เขียน goragod โพสต์เมื่อ 01 เม.ย. 2551 เปิดดู 12,452 ป้ายกำกับ PHPExcel
^