I need to know how one can generate an excel report from html output for example below

<?

header("Content-Type: application/vnd.ms-excel");

echo "<table border=1>

<tr>

<td>Cell 1</td>

<td>Cell 2</td>

</tr>

</table>";

?>

Recommended Answers

All 16 Replies

<?php
$file="test.xls";
$test="<table border=1><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
echo $test;
?>

This works.

does not quite works
gives me the message
Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/adwatch/invoice/excelnow.php:2) in /opt/lampp/htdocs/adwatch/invoice/excelnow.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/adwatch/invoice/excelnow.php:2) in /opt/lampp/htdocs/adwatch/invoice/excelnow.php on line 6

You need to show us your entire script. :)

Here is the problem
the output file is not in excel format but a plain text or an html file.
I need to generate an excel file in the rows and columns and in the cells.

my code works just fine :)

the output comes out like this

<table border=1><tr><td>Cell 1</td><td>Cell 2</td></tr></table>


this is not excel file

I have no idea what's on line number 2 of your script. (And, I am not a mind reader.)

code is giving output alrigt but the output is as shown above its not excel format it plaintext or html

<table border=1><tr><td>Cell 1</td><td>Cell 2</td></tr></table>

Hi,
make sure there is no white space outside of the php start tag. While a blank line before the <?php start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line.
So the Excel-Headers cannot be send and you will see a common html-page.

Maybe the late answer will help you or anybody else.

Greetings from Germany

sunfish

Utter BS
a html table does not magically become an excell file by giving it a header.
excell file format is comprised largely of characters outside any of the printable charactersets which have to be hex encoded.
If a .csv format of the data that a spreadsheet will read is not sufficient then

http://www.sitepoint.com/article/pear-spreadsheet_excel_writer/

http://www.phpclasses.org/browse/package/1919.html

http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8

http://www.opensubscriber.com/message/php-general@lists.php.net/8566005.html

In some cases it can be usefull, to get a html-table as an excel-file. The file is smaller and e.g. phpMyAdmin uses this technique to transform mySQL data as an excel-file. It works (magically) :-/

Utter BS
a html table does not magically become an excell file by giving it a header.
excell file format is comprised largely of characters outside any of the printable charactersets which have to be hex encoded.

Well actually, excel has a html based encoder which allows files exported to html to be reimported back into excel. That is what makes the magic happen when setting the headers. Because if you set a html file to load in excel the html to excel decoder will come into place where the data is reloaded. The only thing is that for best compatibility you need to use the Microsoft sloppy version of html. But there are also true techniques to make it a proper excel file in the first place as mentioned earlier.

Does anyone know how to create an excel file without the

excellent code! worked perfectly.

how could I go about about giving some format to outputted excel?

if the table is stored in a hidden element and you are getting your value from $_POST then do stripslashes() on the value before echoing on excel. otherwise sometimes it can cause problems.

Sundeep dangol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.