PHPEXCEL เป็นห้องสมุด MS Office Excel ที่ทรงพลังมาก เมื่อจำเป็นต้องส่งออกข้อมูลรูปแบบที่ค่อนข้างซับซ้อน phpexcel เป็นตัวเลือกที่ดี อย่างไรก็ตามการใช้งานค่อนข้างซับซ้อน แสดงรายการเพื่อบันทึก
การคัดลอกรหัสมีดังนี้:
-
// ตั้งค่าเส้นทางรวมของไลบรารีคลาส phpexcel
set_include_path ('.'. path_separator
'D:/zeal/php_libs' path_separator
get_include_path ());
-
* ต่อไปนี้เป็นตัวอย่างของการใช้งาน สำหรับบรรทัดที่เริ่มต้นด้วย //// มีวิธีการเลือกที่แตกต่างกัน โปรดใช้ตามความต้องการที่แท้จริง
* เปิดความคิดเห็นในบรรทัดที่เกี่ยวข้อง
* หากใช้ Excel5 เอาต์พุตควรเข้ารหัส GBK
-
require_once 'phpexcel.php';
// ไม่สม่ำเสมอ
//// require_once 'phpexcel/writer/excel5.php'; // ใช้สำหรับ XLS รุ่นล่างอื่น ๆ
// หรือ
//// require_once 'phpexcel/writer/excel2007.php'; // สำหรับรูปแบบ Excel-2007
// สร้างอินสแตนซ์วัตถุประมวลผล
$ objexcel = new phpexcel ();
// สร้างรูปแบบไฟล์เพื่อเขียนอินสแตนซ์ของวัตถุ
///// $ objwriter = ใหม่ phpexcel_writer_excel5 ($ objexcel); // ใช้สำหรับรูปแบบเวอร์ชันอื่น ๆ
// หรือ
///// $ objwriter = ใหม่ phpexcel_writer_excel2007 ($ objexcel); // สำหรับรูปแบบปี 2007
// $ objwriter-> setOffice2003 ความสามารถในการเข้ากันได้ (จริง);
-
// ตั้งค่าคุณสมบัติเอกสารพื้นฐาน
$ objprops = $ objexcel-> getProperties ();
$ objprops-> setCreator ("zeal li");
$ objprops-> setlastmodifiedby ("zeal li");
$ objprops-> settitle ("Office XLS Test Document");
$ objprops-> setSubject ("เอกสารการทดสอบ Office XLS, Demo");
$ objprops-> setDescription ("เอกสารทดสอบสร้างโดย phpexcel");
$ objprops-> setkeywords ("Office Excel phpexcel");
$ objprops-> setCategory ("ทดสอบ");
-
// ตั้งค่าดัชนีชีตปัจจุบันสำหรับการดำเนินการเนื้อหาที่ตามมา
// โดยทั่วไปจำเป็นต้องใช้การโทรเมื่อใช้หลายแผ่น
// โดยค่าเริ่มต้น phpexcel จะสร้างแผ่นแรกโดยอัตโนมัติและตั้งค่า sheetIndex = 0
$ objexcel-> setactiveSheetIndex (0);
$ objactsheet = $ objexcel-> getactiveSheet ();
// ตั้งชื่อของแผ่นงานปัจจุบัน
$ objactsheet-> settitle ('แผ่นทดสอบ');
-
// ตั้งเนื้อหาเซลล์
-
// phpexcel กำหนดประเภทเนื้อหาของเซลล์โดยอัตโนมัติตามเนื้อหาที่เข้ามา
$ objactsheet-> setCellValue ('A1', 'เนื้อหาสตริง'); // เนื้อหาสตริง
$ objactsheet-> setCellValue ('A2', 26); // ค่า
$ objactsheet-> setCellValue ('a3', true); // ค่าบูลีน
$ objactsheet-> setCellValue ('a4', '= sum (a2: a2)'); // สูตร
// คำอธิบายระบุประเภทเนื้อหา
$ objactsheet-> setcellvalueexplict ('A5', '847475847857487584'
phpexcel_cell_datatype :: type_string);
// รวมเซลล์
$ objactsheet-> mergecells ('b1: c22');
// แยกเซลล์
$ objactsheet-> unmergecells ('b1: c22');
-
// ตั้งค่าสไตล์เซลล์
-
// ตั้งค่าความกว้าง
$ objactsheet-> getColumnDimension ('b')-> setautosize (จริง);
$ objactsheet-> getColumnDimension ('a')-> setWidth (30);
$ objstylea5 = $ objactsheet-> getStyle ('a5');
// ตั้งค่ารูปแบบตัวเลขของเนื้อหาเซลล์
-
// ถ้าใช้ phpexcel_writer_excel5 เพื่อสร้างเนื้อหา
// หมายเหตุที่นี่ว่าตัวแปร const ที่กำหนดไว้ในคลาส phpexcel_style_numberformat
// ในวิธีการจัดรูปแบบที่กำหนดเองต่างๆสามารถใช้ประเภทอื่น ๆ ได้ตามปกติ แต่เมื่อ setFormatCode
// เมื่อเป็น format_number เอฟเฟกต์จริงจะไม่ถูกตั้งค่าเป็น "0" ความต้องการ
// แก้ไขเมธอด getXF ($ style) ในซอร์สโค้ดของคลาส phpexcel_writer_excel5_format
// เพิ่มหนึ่งก่อนถ้า ($ this-> _ biff_version == 0x0500) {(ใกล้บรรทัด 363)
// รหัสบรรทัด:
// ถ้า ($ ifmt === '0') $ ifmt = 1;
-
// ตั้งค่ารูปแบบเป็น phpexcel_style_numberformat :: format_number เพื่อหลีกเลี่ยงจำนวนมาก
// มันจะแสดงโดยใช้วิธีการนับวิทยาศาสตร์ ร่วมกับวิธีการ setautosize ต่อไปนี้เนื้อหาของแต่ละบรรทัดสามารถทำได้
// ทั้งหมดจะแสดงตามเนื้อหาต้นฉบับ
$ objstylea5
-> getNumberFormat ()
-> setFormatCode (phpexcel_style_numberformat :: format_number);
// ตั้งค่าตัวอักษร
$ objfonta5 = $ objstylea5-> getFont ();
$ objfonta5-> setName ('Courier ใหม่');
$ objfonta5-> setsize (10);
$ objfonta5-> setbold (จริง);
$ objfonta5-> setunderline (phpexcel_style_font :: underline_single);
$ objfonta5-> getColor ()-> setArgb ('FF9999999');
// ตั้งค่าการจัดตำแหน่ง
$ objaligna5 = $ objstylea5-> getalignment ();
$ objaligna5-> sethorizontal (phpexcel_style_alignment :: horizontal_right);
$ objaligna5-> setvertical (phpexcel_style_alignment :: vertical_center);
// ตั้งเส้นขอบ
$ objbordera5 = $ objstylea5-> getBorders ();
$ objbordera5-> getTop ()-> setBorderstyle (phpexcel_style_border :: border_thin);
$ objbordera5-> getTop ()-> getColor ()-> setArgb ('ffff0000'); // สี
$ objbordera5-> getBottom ()-> setBorderstyle (phpexcel_style_border :: border_thin);
$ objbordera5-> getLeft ()-> setBorderstyle (phpexcel_style_border :: border_thin);
$ objbordera5-> getright ()-> setborderstyle (phpexcel_style_border :: border_thin);
// ตั้งค่าสีเติม
$ objfilla5 = $ objstylea5-> getFill ();
$ objfilla5-> setFillType (phpexcel_style_fill :: fill_solid);
$ objfilla5-> getStartColor ()-> setArgb ('ffeeeee');
// คัดลอกข้อมูลสไตล์จากเซลล์ที่ระบุ
$ objactsheet-> duplicatestyle ($ objstylea5, 'b1: c22');
-
// เพิ่มรูปภาพ
$ objdrawing = new phpexcel_worksheet_drawing ();
$ objdrawing-> setName ('zealimg');
$ objdrawing-> setDescription ('ภาพแทรกโดยความกระตือรือร้น');
$ objdrawing-> setPath ('./ zeali.net.logo.gif');
$ objdrawing-> setheight (36);
$ objdrawing-> setCoordinates ('C23');
$ objdrawing-> setoffsetx (10);
$ objdrawing-> setrotation (15);
$ objdrawing-> getShadow ()-> setVisible (จริง);
$ objdrawing-> getShadow ()-> setDirection (36);
$ objdrawing-> setworksheet ($ objactsheet);
// เพิ่มแผ่นงานใหม่
$ objexcel-> createSheet ();
$ objexcel-> getsheet (1)-> settitle ('ทดสอบ 2');
// ปกป้องเซลล์
$ objexcel-> getsheet (1)-> getProtection ()-> setsheet (จริง);
$ objexcel-> getsheet (1)-> protectcells ('A1: C22', 'phpexcel');
-
// เนื้อหาเอาต์พุต
-
$ outputFilename = "output.xls";
// ถึงไฟล์
///// $ objwriter-> บันทึก ($ outputfilename);
//หรือ
// ไปที่เบราว์เซอร์
//// Header ("Content-type: Application/Force-Download");
//// Header ("Content-type: Application/Octet-Stream");
//// Header ("content-type: แอปพลิเคชัน/ดาวน์โหลด");
////header('Content-disposition:inline;filename="'.$OutputFilename. '"');
//// Header ("Content-transfer-encoding: Binary");
//// Header ("หมดอายุ: จันทร์, 26 ก.ค. 1997 05:00:00 GMT");
//// Header ("Modified ล่าสุด:". gmdate ("d, d myh: i: s"). "gmt");
//// Header ("Cache-Control: ต้องทำการตรวจสอบ, โพสต์-ตรวจสอบ = 0, pre-check = 0");
//// Header ("Pragma: No-cache");
///// $ objwriter-> บันทึก ('php: // output');
-