1. The resource list of my task, plan to add an export button at the head of the resource list, as shown in Figure 1
Figure 1
2. Search in github: yii2 export excel, the result: moonsoft/yii2-phpexcel, kartik-v/yii2-export, as shown in Figure 2
Figure 2
3. After comparative analysis, it is mainly considered from the richness of the supported export formats and the extension of the support for importing in the future, and the final decision is decided: Moonlandsoft/Yii2-phpExcel
The export format of MoonLandsoft/Yii2-phpExcel: XLS, XLSX, XML, ODS, SLK, GNumeric, CSV, HTML
Export formats of kartik-v/yii2-export: XLS, XLSX, CSV, HTML, TXT, PDF
4. Install this extension based on Composer, execute the following commands:, Note: Due to network problems, a total of 3 times are executed, and the third time is to climb the wall and then execute, as shown in Figure 3
Figure 3
PS E:\wwwroot\pcs-api-feature-task-statistics-print> composer require --prefer-dist moonlandsoft/yii2-phpexcel "*"
Content-Length mismatch, received 16128 bytes out of the expected 3759703
https://asset-packagist.org could not be fully loaded, package information was loaded from the local cache and may be ou
t of date
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
https://asset-packagist.org could not be fully loaded, package information was loaded from the local cache and may be ou
t of date
Package operations: 5 installs, 0 updates, 0 removals
- Installing markbaker/matrix (1.1.4): Downloading (100%)
- Installing markbaker/complex (1.4.7): Downloading (100%)
- Installing psr/simple-cache (1.0.1): Downloading (100%)
- Installing phpoffice/phpspreadsheet (1.9.0): Downloading (100%)
PS E:\wwwroot\pcs-api-feature-task-statistics-print> composer require --prefer-dist moonlandsoft/yii2-phpexcel "*"
The "https://asset-packagist.org/p/provider-latest/49c795e8ff9b455adc45e73e45b8fa84fd39ce4761894526a7d05b455390a960.json
" file could not be downloaded: failed to open stream: HTTP request failed!
https://asset-packagist.org could not be fully loaded, package information was loaded from the local cache and may be ou
t of date
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Content-Length mismatch, received 16128 bytes out of the expected 3759943
https://asset-packagist.org could not be fully loaded, package information was loaded from the local cache and may be ou
t of date
Package operations: 2 installs, 0 updates, 0 removals
- Installing phpoffice/phpspreadsheet (1.9.0):
PS E:\wwwroot\pcs-api-feature-task-statistics-print> composer require --prefer-dist moonlandsoft/yii2-phpexcel "*"
Content-Length mismatch, received 16128 bytes out of the expected 3759943
https://asset-packagist.org could not be fully loaded, package information was loaded from the local cache and may be ou
t of date
./composer.json has been updated
Loading composer repositories with package information
https://asset-packagist.org could not be fully loaded, package information was loaded from the local cache and may be ou
t of date
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Installing phpoffice/phpspreadsheet (1.9.0): Loading from cache
- Installing moonlandsoft/yii2-phpexcel (2.0.0): Downloading (100%)
phpoffice/phpspreadsheet suggests installing mpdf/mpdf (Option for rendering PDF with PDF Writer)
phpoffice/phpspreadsheet suggests installing dompdf/dompdf (Option for rendering PDF with PDF Writer)
phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf (Option for rendering PDF with PDF Writer)
phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph (Option for rendering charts, or including charts with PDF
or HTML Writers)
Writing lock file
Generating autoload files
5. New features for exporting data, if you are familiar with Yii GridView, you can use this function. This is the same as the GridView data column. The valid parameters of the columns in the array pattern areAttribute,Header,Format,valueand footer(todo). The columns in the valid layout of the string pattern areattribute: format: header: footer(todo). as shown in Figure 4
Figure 4
6. Copy \api\rests\plan_task\indexaction.php to \API\RESTS\plan_task\exportaction.php, since the export task is all records, set the number of resources per page to the total number of resources
9. Open the URL in the browser: http://api.pcs-api.localhost/v1/plan-tasks/export , successfully downloaded the file: my task-2019-09-20-19-39-11.xlsx, as shown in Figure 5
Figure 5
10. Open the file: my task-2019-09-20-19-39-11.xlsx, as expected, as shown in Figure 6
Figure 6
11. Open the URL in Postman: http://api.pcs-api.localhost/v1/plan-tasks/export , click the “send” button to respond to garbled characters, as shown in Figure 7
Figure 7
12. Open the URL in Postman: http://api.pcs-api.localhost/v1/plan-tasks/export , click “Send and download” button, download a file named: response.txt, and there is no content in the file, as shown in Figure 8
8
13. Set headers in postman, accept:application/xml; version=0.0, open the URL: http://api.pcs-api.localhost/v1/plan-tasks/export , click “Send and download” button, the download name is: *-2019-09-26-10-44-40.xlsx, the Chinese in the file name is garbled, but the content in the file is as expected, as shown in Figure 9
Figure 9
14. In the front-end (based on the Ant Design) page, the request URL: http://api.pcs-api.localhost/v1/plan-tasks/export , in response to garbled characters, the file is not downloaded, as shown in Figure 10
Figure 10
15. The reason for the preliminary analysis should be that the front-end code does not realize the complete parsing of the response header. Therefore, even if the server responds to the front-end to download the file, the front-end still parses the data based on the JSON format, as shown in Figure 11
Figure 11
16. The final decision is implemented based on pure links, using the download attribute, this attribute instructs the browser to download the URL instead of navigating to it, so the user will be prompted to save it as a local file. , click the “Export” button, download the file named: My Task-2019-09-26-10-44-40.xlsx, as shown in Figure 12
Figure 12
Leave a Reply