Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use laravel+PhpSpreadsheet to export excel and download

Shulou Source: shulou.com Published: 2022-06-02 07:34:49 09月27日 Update

This article mainly introduces "how to use laravel+PhpSpreadsheet to export excel and download". In daily operation, I believe many people have doubts about how to use laravel+PhpSpreadsheet to export excel and download problems. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use laravel+PhpSpreadsheet to export excel and download". Next, please follow the editor to study!

Effect picture:

Official website: https://phpspreadsheet.readthedocs.io/en/latest/#getting-started

Step 1: import phpspreadsheet

Install using composer in IDE with the following command:

Composer require phpoffice/phpspreadsheet

Step 2: use the plug-in in the laravel project

The logic of the following code is to write the required data into excel and then save it in the specified directory. Click on the a tag at the front end to download the excel file.

$spreadsheet = new Spreadsheet (); / / instantiate $spreadsheet- > setActiveSheetIndex (0); / / set the index of excel $sheet=$spreadsheet- > getActiveSheet (); / * set cell width * / $sheet- > getColumnDimension ('A')-> setWidth (20); $sheet- > getColumnDimension ('B')-> setWidth (15); $sheet- > getColumnDimension ('C')-> setAutoSize (true) / * set font size * / $sheet- > getStyle ('A1purc1')-> getFont ()-> setBold (true)-> setName (' Arial')-> setSize (10); / / Lock header $sheet- > freezePane ('A2') $sheet- > setCellValue-> setCellValue (the name of the respondent)-> setCellValue (the text of the answer); $sheet- > fromArray ($result,null,'A2'); $writer = new Xls ($spreadsheet); $pathUrl = public_path (). '\ excel/'; $filePath=$pathUrl.$fileName; / / determines whether the directory exists. If it does not exist, create a new if (! is_dir ($pathUrl)) mkdir ($pathUrl,0755,true); $writer- > save ($filePath); / / Save excel

We can see from the code that we can control the style of the generated excel through some specific settings, such as setting cell width, setting font size and color, and so on. These can be set up according to your needs.

Click the button at the front end to download the code as follows:

$.ajax ({url:ajaxUrl, type:'GET', data: {'_ token':token, 'paperId':paperId,' optionId':optionId} Success:function (data) {if (data.code===1) {if (data.data.length > 0) {var path = data.data,// returns the address of the excel file url = "{asset (')}" + path / / packing address createA=document.createElement ('a') / / create an a tag createA.href=url;// to set the href attribute document.body.appendChild (createA) to the a tag; / / compatible with Firefox createA.click () / bind the click event to the a tag, and download the function document.body.removeChild (createA) through the href attribute and click event of the a tag;}} else {layer.msg (data.msg) }) at this point, the study on "how to export excel with laravel+PhpSpreadsheet and download" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Tags learning code events front-end units addresses sizes fonts attributes files more directories answers websites help utility and then functions available through the Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Redmi Linux MySQL