简体   繁体   English

错误XLS文件生成并使用html,php打开

[英]Error XLS file generate and open using html,php

I have generate xls file using html and php everthing is fine. 我已经使用html和php生成了xls文件,一切都很好。 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 . 如果另存为.xls则Excel需要正确格式化的文件。 You have to write explicit data reserved to Excel and not just HTML tables. 您必须编写保留给Excel的显式数据,而不仅仅是HTML表。

In order to prevent the popup error window, save your file as .csv and not .xls . 为了防止出现弹出错误窗口,请将文件另存为.csv而不是.xls

.xls files are "binary" files, as opposed to the data you wish to enter which is "text-based" . .xls文件是“二进制”文件,与您希望输入的“基于文本”的数据相反。 That is why Excel prompts you when opening the file. 这就是为什么Excel在打开文件时提示您的原因。

You could even save it with an .html extension, that will work also, I tried it. 您甚至可以将其保存为.html扩展名,这也可以工作,我尝试过。

One of the libraries you could use is called PHPExcel and can be found at the following URL: 您可以使用的一种库称为PHPExcel,可以在以下URL中找到:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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