Malde Chavda
Malde Chavda

Reputation: 351

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

error display whene open

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>';

Upvotes: 0

Views: 1672

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

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:

Upvotes: 1

Related Questions