0

I have a question about exporting selected mysql data to excel. I have pulled data from a mysql table and I arranged it as I wanted.

Data that i select from mysql table

How can I export data from current page to excel??

I have tried to use this code,

<input type= "button" onclick=location.href="export.php" value="Export"/>

this is for the export button on hrviewdate2.php file and this is export.php file:

<?php

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

header("Content-Disposition: attachment; filename=zakat.xls");

include 'hrviewdate2.php';
?>

but it just exports an html page without data that I needed.

Chris Trudeau
  • 1,427
  • 3
  • 16
  • 20
GagakAngkasa
  • 39
  • 1
  • 9
  • 2
    You could use CSV, separating each column and its data by a comma, or you can use a library to generate spreadsheets for you: https://github.com/PHPOffice/PHPExcel – David Tkachuk Jan 05 '16 at 03:30

1 Answers1

0

Your best option is to export your data as a csv file that can be opened by most of the popular spreadsheet application including Google Docs and Excel.

You can see this post Export to CSV via PHP for info on how to do this.

The only problem you will run into is the lack of support for non English characters by Excel when data is in CSV format. You will need to toy with some encoding settings to get the CSV file to open right in Excel.

If you do want to generate .xls files, you can look into using this library over at GitHub.

Community
  • 1
  • 1
williamli
  • 3,846
  • 1
  • 18
  • 30