PhpSpreadsheet read, write, produce excel template file

https://blog.csdn.net/appAndWxy/article/details/84889713

 

And then on their own code

 

 

Then use

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

 

public function export()
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //查询数据信息
        $book = model('Book')->select();
        
        $sheet->setCellValue("A1", "书籍名");
        $sheet->setCellValue("B1", "ID号");


        foreach($book as $k => $v){
            $sheet->setCellValue("A".($k+2), $v['book_name']);
            $sheet-> setCellValue (. "B" ( $ K +2), $ V [ 'ID' ]); 
        } 
    
        header ( 'the Type-the Content: file application / vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // tell the browser output 07Excel file 
        //header('Content-Type:application/vnd.ms-excel');// tell the browser to be output Excel03 version of the file 
        header ( 'Content-Disposition: Attachment; filename = "01simple.xlsx"' ); // tell the browser name browser output 
        header ( 'cache-Control: max-Age = 0'); // disable caching of 
        $ Writer = new new Xlsx ( $ Spreadsheet );
         $ Writer -> the Save ( 'PHP: / / Output ' );

        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
    }

Here is the direct download of the output, and afterwards pay attention to the destruction of memory

 

Guess you like

Origin www.cnblogs.com/chenliuxiao/p/12576022.html