简体   繁体   中英

Error XLS file generate and open using html,php

I have generate xls file using html and php everthing is fine. But problem is that when tring to open file then following error display

打开时显示错误

My code is here:

    $filename = "file.xls";
    header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
    header("Content-type:   application/x-msexcel; charset=utf-8");
    header("Content-Disposition: attachment;filename=".$filename); //tell browser what's the file name
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: max-age=0");
    echo '<table>
               <tr>
                   <th>heading1</th>
                   <th>heading2</th>
                   <th>heading3</th>
               </tr>
               <tr>
                   <td>data1</td>
                   <td>data2</td>
                   <td>data3</td>
               </tr>
           </table>';

It's because of the data you are writing to it. Excel expects a properly formatted file if saving as an .xls . You have to write explicit data reserved to Excel and not just HTML tables.

In order to prevent the popup error window, save your file as .csv and not .xls .

.xls files are "binary" files, as opposed to the data you wish to enter which is "text-based" . That is why Excel prompts you when opening the file.

You could even save it with an .html extension, that will work also, I tried it.

One of the libraries you could use is called PHPExcel and can be found at the following URL:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM