0

how to save a selected data in excel format in php when i click the link? and i want to save the file in the location given at the run time of the file? please advice me..

header('Content-Type: application/vnd.ms-excel');   //define header info for browser
    header('Content-Disposition: attachment; 
    filename='.$dbTable.'-'.date('Ymd').'.xls');
    header('Pragma: no-cache');
    header('Expires: 0');
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • please search before posting , [export-mysql-data-to-excel-in-php](http://stackoverflow.com/questions/15699301/export-mysql-data-to-excel-in-php) and for select just post checkboxes or other means of selecting via post and use in WHERE clause of query – Bor691 Apr 08 '14 at 06:03

1 Answers1

0

Try this :

header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $dbTable.'-'.date('Ymd') . ".xls\"");
header("Pragma: no-cache");
header("Expires: 0");

Or simply :

header("Location: http://path/to/file/filename.xlsx");
/* Make sure that code below does not get executed when we redirect. */
exit;
Paul Bele
  • 1,514
  • 1
  • 11
  • 12
  • I think he does not have a xls file on the server yet and wants to generate it with php from data selected by user from browser ... – Bor691 Apr 08 '14 at 06:11
  • If this is the case, then using this library : https://phpexcel.codeplex.com is my best bet . It's extremely easy to custom excel files. After the file is created with the proper name, he can use the examples above – Paul Bele Apr 08 '14 at 06:16
  • maybe give us the form fields and i can give you an example using the php excel class – Paul Bele Apr 08 '14 at 06:16
  • It all depends on what do you want exactly, and what do you currently have. If you want to create the XLS based on form fields, please give us the form , so i can give you an example. If you already have the XLS created, you can use the code i already wrote. – Paul Bele Apr 08 '14 at 08:10