phpspreadsheet 和 codeigniter 3 libraray

问题描述 投票:0回答:1
Can help me to create class library of phpspreadsheet like PHPexcel in codeigniter 3 to load it    in autoload config
and how to move this folder under third_party under my application 
I am create the file under library folder 
but not working , have  Unable to load the requested class: Spreadsheet

i am create the file under library folder 

but not working , have  Unable to load the requested class: Spreadsheet

when try to load it same library  

`目录。 '供应商/autoload.php';

use \phpoffice\phpspreadsheet\Spreadsheet;
use \phpoffice\phpspreadsheet\Writer\Xlsx;

class Phpspreadsheet extends Spreadsheet
    {
    public function __construct()
    {
    parent::__construct();
   }
}

`

codeigniter spreadsheet phpexcel phpspreadsheet
1个回答
0
投票

使用 Composer 下载 PHPSpreadsheet Excel

composer require phpoffice/phpspreadsheet

defined('BASEPATH') OR exit('No direct script access allowed');

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class SpreadsheetController extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Load PhpSpreadsheet library
        // $this->load->library('PhpSpreadsheet');
    }

    public function index() {
        // Load Composer autoloader
        require_once FCPATH . 'vendor/autoload.php'; // Adjust the path as needed

        // Create a new PhpSpreadsheet object
        $spreadsheet = new Spreadsheet();

        // Set active sheet
        $spreadsheet->setActiveSheetIndex(0);
        $sheet = $spreadsheet->getActiveSheet();

        // Add data to the spreadsheet
        $sheet->setCellValue('A1', 'Name');
        $sheet->setCellValue('B1', 'Age');

        $sheet->setCellValue('A2', 'John');
        $sheet->setCellValue('B2', '30');

        $sheet->setCellValue('A3', 'Alice');
        $sheet->setCellValue('B3', '25');

        // Save the spreadsheet
        $writer = new Xlsx($spreadsheet);
        $filename = 'example.xlsx';
        $writer->save($filename);

        // Download the spreadsheet
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'. $filename .'"');
        header('Cache-Control: max-age=0');
        $writer->save('php://output');
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.