How to solve the problem that php download excel cannot be opened
The main content of this article is "how to solve the problem that php download excel cannot be opened", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the problem that php download excel can't open".
Php downloads the excel file
1. Do not output any non-file information, such as echo log information, during the download process. Otherwise, the downloaded file cannot be opened, indicating that the format is incorrect or the file is corrupted.
2. The output excel format must be kept with the suffix name, otherwise it will also prompt the format error or the file is corrupted.
The copy code is as follows:
If (file_exists (CACHE_PATH. $file_name) {/ / $this- > logger- > error ('file realpath:'.realpath (CACHE_PATH. $file_name); header ('Pragma: public'); header (' Expires: 0'); header ('Content-Encoding: none'); header (' Cache-Control: must-revalidate, post-check=0, pre-check=0'); header ('Cache-Control: public'); header (' Content-Type: application/vnd.ms-excel'); header ('Content-Description: File Transfer') Header ('Content-Disposition: attachment; filename='. $file_name); header ('Content-Transfer-Encoding: binary'); header (' Content-Length:'. Filesize (CACHE_PATH. $file_name); readfile (CACHE_PATH. $file_name);} else {$this- > logger- > error ('export model:'. $id.' Error: not producing files'); echo 'alert (\' export error, file not exists!\')';} so far, I believe you have a better understanding of "how to solve the problem that php download excel can't open". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!