Thư viện PHPExcel

Bảng tính là một định dạng dữ liệu phổ biến cho mục đích sử dụng cá nhân và doanh nghiệp vì nó cung cấp cách lưu trữ dữ liệu dạng bảng một cách có tổ chức. Đây cũng là một trong những định dạng được sử dụng phổ biến nhất để lưu trữ thông tin về nhiều hàng và cột. Các tệp Excel được các doanh nghiệp sử dụng rộng rãi để quản lý các hoạt động hàng ngày của họ. Nhưng những tệp này có thể khó làm việc từ dòng lệnh hoặc bên ngoài bộ phần mềm Microsoft Office, đó là lý do tại sao có nhiều thư viện PHP đã được tạo chỉ cho mục đích này. Trong bài đăng trên blog này, chúng ta sẽ khám phá năm thư viện PHP khác nhau mà bạn có thể sử dụng với các tệp Excel của mình, vì vậy hãy bắt đầu

Thư viện PHPExcel

Mục lục

bảng tính php

Thư viện phpspreadsheet là một phần mở rộng PHP có thể được sử dụng để đọc, tạo và viết bảng tính. Nó có các công cụ tính toán khác nhau với sự hỗ trợ bản địa hóa cho các thị trường như Trung Quốc, Nga và Ấn Độ. Điều này cho phép các nhà phát triển phân phối ứng dụng bảng tính của họ bằng các ngôn ngữ khác nhau bằng cách cung cấp dữ liệu cụ thể theo ngôn ngữ mà không cần bất kỳ nỗ lực phát triển hoặc lập trình bổ sung nào

PHPExcel là phiên bản cũ hơn của PhpSpreadsheet. Nhà phát triển đã phá vỡ khả năng tương thích để cải thiện đáng kể chất lượng cơ sở mã bằng cách giới thiệu các không gian tên, tuân thủ PSR và sử dụng các tính năng ngôn ngữ PHP mới nhất.  

vòi

Nếu bạn đang tìm thư viện tốt nhất để tương tác với Excel, sao cho các tệp được xử lý một cách nhanh chóng và có thể mở rộng, thì có lẽ Spout chính là thứ bạn cần. Trình đọc tệp này khác với những tệp khác. Bạn có thể đọc các tệp rất lớn trong khi vẫn giữ mức sử dụng bộ nhớ rất thấp

Spout là gói hoàn hảo cho tất cả các nhu cầu dữ liệu của dự án của bạn và cũng có thể giúp việc quản lý các tệp dữ liệu khổng lồ trở nên dễ dàng. Nó dễ cài đặt, sử dụng mạnh mẽ và với tốc độ đọc nhanh gấp đôi so với các thư viện khác được cung cấp ngay bây giờ, nó giúp bạn có nhiều thời gian để ăn trưa hoặc nghỉ giải lao trong khi hoàn thành các báo cáo đó

Trình tạo mẫu PHP Excel

Thư viện PHP này cho phép bạn xuất tệp excel từ mẫu excel. PHP Excel Templator là một thư viện tạo các tệp excel nhanh chóng từ các mẫu. Nó hoàn hảo cho các ngành tài chính, kế toán và kỹ thuật. Vì vậy, cho dù bạn cần tạo báo cáo hàng tháng, dự báo doanh thu hay bảng cân đối kế toán – chỉ cần tạo một mẫu excel và sử dụng phần mở rộng mã có thể tái sử dụng này thay vì phải mã hóa một thứ gì đó từ đầu mỗi lần

Đơn giảnXLSX

SimpleXLSX là một thư viện nhẹ, nhanh để phân tích cú pháp và truy xuất dữ liệu từ các tệp XLSx của MS Excel 2007. Thư viện PHP không phụ thuộc này tương tác với trình phân tích cú pháp SimpleXML bên dưới thông qua hàm unzip() tích hợp sẵn và không cần bất kỳ tiện ích mở rộng hoặc mô-đun bổ sung nào

PHP_XLSXWriter

PHP_XLSXWriter được thiết kế để trở thành một thư viện nhẹ, dễ sử dụng để xuất các tệp xlsx ở định dạng Office 2007+. Nó hỗ trợ công thức và định dạng ô cơ bản – hoàn hảo để tạo ngân sách đơn giản hoặc kế hoạch dự án cho người dùng Excel muốn dữ liệu của họ ở cùng định dạng

Thư viện này dành cho PHP được thiết kế như một giải pháp nhẹ có thể hoạt động trên tệp tương thích với Excel chỉ với các tính năng cơ bản được hỗ trợ. Với gói này, bạn có thể dễ dàng tạo các bảng tính lớn và phức tạp để lưu dưới dạng tệp XLSX tương thích với xlsx

Excel là một công cụ tuyệt vời để tổ chức dữ liệu ở dạng ô, thực hiện các phép toán cơ bản và phức tạp, tạo đồ họa và biểu đồ hữu ích, v.v. Trong hướng dẫn này, chúng ta sẽ xem cái nào nên sử dụng PHPExcel hoặc PHPSpreadsheet

Sử dụng PHP, chúng ta có thể thực hiện nhiều thao tác hữu ích và vì mục đích này, có 2 thư viện nổi tiếng có sẵn. PHPExcel & PHPSpreadsheet

Sự khác biệt chính giữa hai điều này là PHPSpreadsheet là phiên bản nâng cấp của PHPExcel. PHPSpreadsheet chứa các tính năng mới nhất được giới thiệu trong phiên bản mới hơn của PHP. e. , v5. 5

Làm cách nào để sử dụng PHPEXcel?

Để cài đặt PHPExcel trong dự án hiện tại, trước tiên, chúng ta cần sao chép thư mục Lớp học vào vị trí mong muốn của bạn. Vị trí này có thể là thư mục lớp gốc của dự án và thứ hai, chúng ta cần sao chép PHPExcel. tệp php là điểm vào cho các hoạt động của Excel



/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Hãy hiểu từng dòng ví dụ trên-

Đầu tiên, chúng tôi đã bao gồm tệp lớp PHPExcel sẽ thực hiện tất cả các hoạt động liên quan đến excel. Điều này có thể được bao gồm bằng cách sử dụng câu lệnh bao gồm/yêu cầu HOẶC nên sử dụng tự động tải để đảm bảo rằng khi ứng dụng khởi động, thì lớp này sẽ tự động khả dụng

Sau khi tạo đối tượng PHPExcel, chúng tôi sử dụng chuỗi phương thức để thực hiện nhiều thao tác trên từng trang đang hoạt động. Ở đây,


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
1 được sử dụng để đặt trang tính đang hoạt động sẽ được sử dụng trong Sổ làm việc. Trong trường hợp của chúng tôi, chúng tôi đã sử dụng chỉ mục 0 để làm việc trên trang tính đầu tiên

Sau đó, chúng tôi đã thêm các giá trị trong A1, A2, B1, B2. Trong ví dụ thực tế, đây có thể là nhiều hàng bằng cách sử dụng vòng lặp


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
2 /

/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
3. Để đặt giá trị cho từng ô, chúng tôi đã sử dụng hàm

/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
4

Khi mọi thứ đã được thiết lập, bây giờ dữ liệu của chúng tôi đã sẵn sàng để được ghi vào excel. đối với điều này, chúng tôi đã sử dụng lớp


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
5 được sử dụng để thực hiện thao tác ghi bằng hàm

/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
6

Lớp


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
5 này có thể ghi dữ liệu ở các định dạng khác nhau như-

  • excel2007
  • Excel5
  • Excel2003XML
  • OOCalc
  • SYLK
  • Gnumeric
  • HTML
  • CSV

Bây giờ, thao tác này sẽ lưu trang tính excel vào vị trí được đề cập trong hàm


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
1

Làm cách nào để ghi dữ liệu vào nhiều sheet bằng PHPExcel?


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Vì vậy, trong mã, chúng tôi đã thêm một chức năng mới ngay sau khi cập nhật trang tính 0, đó là


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
2, chức năng này sẽ thêm một trang tính mới trong sổ làm việc hiện có

Sau đó, chúng tôi đã thực hiện thao tác tương tự như đối với sheet 0 bằng cách cung cấp chỉ số 1 cho phương thức


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
1

Nhiều lần, chúng ta cần tập trung vào trang đầu tiên thay vì trang cuối cùng vì lý do rõ ràng. Vì vậy, điều đó có thể được thực hiện bằng cách gọi lại trang đầu tiên tôi. e. ,


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
4

Vì vậy, chúng tôi đã học cách tạo một sổ làm việc/excel mới và thêm các trang tính khác nhau và ghi dữ liệu vào đó

Bây giờ, hãy xem về PHPSpreadsheet

PHPSpreadsheet là gì?

PhpSpreadsheet là một thư viện được viết bằng PHP cung cấp khả năng đọc và ghi các định dạng tệp bảng tính khác nhau như Excel và LibreOffice Calc, v.v. Về cơ bản, đây là phiên bản nâng cao của PHPExcel với nhiều hỗ trợ định dạng hơn và hỗ trợ cho phiên bản PHP mới

PhpSpreadsheet yêu cầu tối thiểu PHP 5. 6 để hỗ trợ các chức năng của nó

Hãy xem ví dụ cơ bản về việc sử dụng PHPSpreadsheet và sau đó chúng ta sẽ xem từng bước-


require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'id')
    ->setCellValue('A2', 1)
    ->setCellValue('B1', 'name')
    ->setCellValue('B2', 'John');
$writer = new Xlsx($spreadsheet);
$writer->save('studentInformation.xlsx');

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Vì vậy, trong ví dụ trên, chúng tôi đã tự động tải tất cả các tệp thư viện cần thiết cần thiết cho hoạt động của excel

Tiếp theo, chúng tôi đã nhập lớp bảng tính cần thiết để thực hiện một thao tác trên excel giống như chúng tôi đã làm cho chức năng Excel trong PHPExcel và cũng bao gồm trình ghi để ghi dữ liệu vào bảng tính

Sau khi tạo đối tượng bảng tính, chúng ta sử dụng hàm


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
5 để lấy trang tính đang hoạt động. Ở đây, chúng ta có thể thấy rằng, chúng ta không truyền tham số chỉ mục có nghĩa là đối tượng bảng tính đủ thông minh để chọn trang tính hiện đang hoạt động. Sau đó, chúng tôi đã cập nhật bảng tính với các giá trị bằng cách sử dụng hàm

/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
4

Cuối cùng, chúng tôi đã chuyển tất cả đối tượng bảng tính sang đối tượng lớp nhà văn (


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
7) để ghi dữ liệu trong excel và lưu bằng tệp

/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
8

Bây giờ, nếu chúng ta muốn viết nhiều tờ cùng một lúc thì sao?. Vì vậy, trong trường hợp này, chúng tôi sẽ sử dụng cùng một đối tượng và thêm một trang tính khác giống như bên dưới

Làm cách nào để ghi dữ liệu vào nhiều sheet bằng PHPSpreadsheet?


require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'id')
    ->setCellValue('A2', 1)
    ->setCellValue('B1', 'name')
    ->setCellValue('B2', 'John');
$sheet_2 = $spreadsheet->createSheet();
$sheet_2->setCellValue('A1', 'id')
    ->setCellValue('A2', 1)
    ->setCellValue('B1', 'name')
    ->setCellValue('B2', 'Alexa');
$writer = new Xlsx($spreadsheet);
$writer->save('studentInformation.xlsx');

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Chúng tôi đã sử dụng hàm


/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// write data to first sheet
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'John');
// add new sheet to the same excel 
$objPHPExcel->createSheet();
// write data to second sheet in the same workbook
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'id')
            ->setCellValue('A2', 1)
            ->setCellValue('B1', 'name')
            ->setCellValue('B2', 'Alexa');
// set focus to first sheet of the workbook
$objPHPExcel->setActiveSheetIndex(0);
// write data to defined excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Classes/studentInformation.xlsx');
9 mà chúng tôi cũng đã sử dụng trong ví dụ về thư viện PHPExcel, hàm này cuối cùng sẽ tạo một trang tính mới trong cùng một sổ làm việc

Bây giờ, với tất cả thông tin này, câu hỏi chính vẫn không thay đổi

Phần kết luận

Thư viện PHPExcel hiện được lưu trữ do các chức năng của phiên bản PHP cũ. Luôn luôn khuyến nghị sử dụng phiên bản PHP mới nhất để tận hưởng các tính năng mới trong đó

Có rất nhiều chức năng bổ sung có sẵn trong cả hai thư viện có thể được sử dụng dựa trên yêu cầu

Vì vậy, để trả lời câu hỏi chính, nên sử dụng PHPExcel hay PHPSpreadsheet cái nào tùy thuộc vào phiên bản PHP hiện tại được cài đặt trên máy chủ và yêu cầu. Nếu PHPExcel không có khả năng cung cấp một số tính năng nhất định mà PHPSpreadsheet có thể cung cấp thì việc nâng cấp phiên bản PHP luôn là điều tốt nhưng cùng với đó, chúng ta cũng cần kiểm tra các chức năng không dùng nữa và cần phải làm việc với chúng

Thư viện PHPExcel là gì?

PHPExcel. PHPExcel là một thư viện được viết bằng PHP thuần túy và cung cấp một tập hợp các lớp cho phép bạn ghi và đọc từ các định dạng tệp bảng tính khác nhau , như Excel (BIFF . xls, Excel 2007 (OfficeOpenXML). xlsx, CSV, Libre/OpenOffice Calc.

PHPExcel PHP là gì?

PHPExcel là thư viện PHP cung cấp khả năng làm việc (đọc/tạo/chỉnh sửa) tệp bảng tính (các định dạng. . xl,. xlsx, CSV,. ods, và một số khác). Trang thư viện PHPExcel chính thức trên GitHub https. //github. com/PHPOffice/PHPExcel.

Làm cách nào để cài đặt PHP Excel?

Cách đơn giản và được đề xuất để cài đặt thư viện PHP bằng Trình soạn thảo là sử dụng lệnh yêu cầu của nhà soạn nhạc. Cách khác là thêm mã bên dưới vào trình soạn thảo theo cách thủ công. tập tin json. "yêu cầu". {. "phpoffice/phpexcel". "1. 8

Làm cách nào để đọc tệp XLSX trong PHP?

Đọc tệp Excel (XLSX)​ .
Tạo Trình đọc Excel bằng ReaderEntityFactory. tạoXLSXReader()
Mở tệp XLSX bằng $reader->open($path)
Duyệt từng Sheet của bảng tính với $reader->getSheetIterator()
Duyệt qua từng Hàng của Trang tính với $sheet->getRowIterator()
Duyệt qua từng Ô của Hàng với $row->getCells()