Reputation: 305
I would like to add a functionality/facility to export a table type results to an excel spreadsheet. I got a large table result(probably 1000 rows and 50 columns) in a php/html page like below. I want to add a button or some kind of functionality for the user in my php/html web page(which is with the underneath table result) to export the table type result to an excel file rather than to leave the user to hard copy the result and paste it in an excel file. When the user to press a button or like kind any functionality the system should ask the file name and where to save window(Like a window when we try to save a file in Microsoft windows).How can we simply achieve this?. Thanks.
RefID VisitID ActionDesc Actiondateandtime
1 85 Submiited to Management 9/10/2014 12:03
2 86 Sent to lab 9/10/2014 1:06
3 86 Sent to lab 9/10/2014 1:07
4 86 Sent to lab 9/10/2014 1:21
5 86 Sent to lab 9/10/2014 1:28
6 87 Followed with Soil scientist 9/10/2014 1:32
7 87 Followed with Soil scientist 9/10/2014 1:33
8 85 Submitted to Management 15/10/2014 3:44
Upvotes: 2
Views: 107
Reputation: 1719
You can use a php to xls-module like PhpExcel
Or you can just create a CSV-file, which can be read by every excel. (File -> Open File -> Comma Separated Value (CSV))
<?php
$fp = fopen('excel_list.csv', 'w');
// Column Header
fwrite($fp, 'column1;column2;column3;column4;\n');
// The data of the csv-sheet. Make a loop here or something
fwrite($fp, $value1.";".$value2.";".value3.";"value4.";\n");
fclose($fp);
?>
EDIT (Thanks to mark Baker)
Better use the php-built in CSV-function (for version >= PHP 5.1): This example-code is from php.net - fputcsv
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
Upvotes: 1