Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use laravel+PhpSpreadsheet to export excel and download

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report