how can I download a table on mysql database using a PHP page?

Dear Experts,
how can I download mysql table as Excel using PHP page?
I want to download the table on the database as Excel not the existing PHP page...
thank you
LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
You have three options
1. CSV
2. XML
3. Library that handles Excel files.

I personally use XML - it is more versatile than CSV and not as top heavy as the libraries out there.

How does it work.

1. Make an Excel mockup of the data you want to export to Excel
2. Save it as an XML file
3. Make header and footer files from the XML by taking out the top part of the document (to the start of the table) and the bottom part (from the end of the table to the end of the document)
4. Take a sample row from the document - this becomes your template.

When you are outputing your file
1. Output the header (either to a temporary file or read it into a string)
2. Loop through your data and use the template to create the rows - add to the file / string from 2
3. Output your footer as above
4. Setup your headers to tell the browser you are sending an attachment (calc filesize and set name here)
5. die / exit

Sample
	
$output = file_get_contents('xmxls_header.php');
foreach($rows as $row) {
  $output .= <<< XML
    <Row>
      <Cell><Data ss:Type="String">' . $row['firstname'] . '</Data></Cell>
      <Cell><Data ss:Type="String">' . $row['lastname'] . '</Data></Cell>
    </Row>
XML;
}	
$output .= file_get_contents('xmxls_footer.php');
$filename = "My-secret-data-". date('Y-m-d-H-i-s') . ".xml";
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: application/vnd.ms-excel");
header("Content-Length: " . strlen($output));
header("Content-Disposition: inline; filename=".$filename);
die($output);

Open in new window

BRMarketingAuthor Commented:
Dear Julian, I need to export it to Excel :(
Julian HansenCommented:
That is exactly what I explained how to do.

Excel will recognise and open an XML file.

If that does not work for you your only option is to use one of the PHP libraries that handles Excel documents for you.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

BRMarketingAuthor Commented:
how can I download mysql table as Excel using PHP page?  I know how to export as csv. But i need to export it as excel.
Julian HansenCommented:
Excel supports an XML format for its documents. This format is feature rich - you can specify pretty much anything you want.

My explanation of how to use this is given above.

If you don't want to go that route (as I have explained as well) - you can use one of the PHP libraries that supports saving in Excel format.

As an example https://github.com/PHPOffice/PhpSpreadsheet

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lenamtlCommented:
Hi,

You can use Datatables that have this options with no need to code anything.
https://datatables.net/extensions/buttons/examples/initialisation/export.html

If you don't want to display the table it is easy to customize and keep only the export features.

otherwise the @Julian methods are very good too.
BRMarketingAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.