HI,
I am trying to display results from a database in results.php. In the same file, after all the data has been displayed in the current webpage,I am placing a button to create a file based on this results to produce a Excel file.
I am not sure how to do this. I have tried but it says you have to force excel file download before any echo, but I want to display as well as produce an excel file. Is this possible or am I missing something? can anyone please let me know how to do this correctly?
You have to change the header for the page, so you have to open the output for the excel file in a new window.
Header type: "application/vnd.ms-excel" .
If you want to create "good" Excel sheets , take a look on PHPExcel
PHPExcel
You're probably faceing an 'Header allready set' error. You need to separe the two steps in order to make that work. One step for displaying and one for excel file creation and downloading.
On th other hand you could combine the data display and excel creation too and eventually display a download link.
Just pokin in the dark, let us know which way you wanna go and we'll be helping you along...
This has been discussed before at StackOverflow:
Export MYSQL result to Excel..
PHP code to convert a MySQL query to CSV
See also these examples and tutorials:
Easy way to create XLS file from PHP
Export MYSQL data to CSV – PHP tutorial
PHP Script: Export MYSQL Table Data to CSV
Advanced CSV Export
Set your headers first and then echo your CSV output:
header("Content-type: application/text/x-csv");
header("Content-disposition: attachment; filename=report.csv");
echo "value1,value2\n";
echo "value3,value4\n";
You could create a .cvs-file which are a lot simpler than xls-files, and the best part is that excel supports .cvs-files.
Then while you are outputting your data to the user, have a seperate variable called something like $cvs which you apply the same data as you output, just in the .cvs-format and when you're done you write $cvs to a file.
Then you just link to that file at the bottom of the page.
You may have to force the download upon the user though.
Related
I want to select a file and display its contents as it is in the browser. Can anyone suggest me how to do this?
Here is a library that reads contents of csv file and displays it as table. It is basically a class. if you have object oriented concepts you can do with it.
View my answer
Codeigniter REST CSV import to mysql
I would like to read excel file (.xls) contents to a web page that will be looking like a calendar where each day will have some things set in the .xls file. I'll be doing the calendar, but I don't know from where to start on reading the excel file. Also, I want to know how to change the value of specific cells using PHP, data will be provided by user.
I've personally never had to read excel files with PHP, but according to similar post, PHP-ExcelReader is a popular option.
I've got a custom table in WordPress and I'd like to be able to export the data in that table as a FILE in CSV (semi-colon seperated) format.
I've got the data coming out properly, but how do I then save it as an attachment?
Keep in mind that when using WordPress, headers have already been set... and I really don't know how to get around that.
I'm not sure exactly HOW you're doing what you're TRYING to do ...
... but this link gives you a simple, straightforward way to write the data as a .csv file you can do a "save as" from your browser:
http://wordpress.org/support/topic/export-wordpress-db-table-to-excel
For these plugins, they use database to store row & column details. You have to extract the data in MySQL database tables ( those table names are usually named after the plugin's name ) .
If you are still not sure how to do, you can just copy the table being displayed in page in browser, then paste in Excel. Last, save as CSV or XLS if you like.
you can use this code. just copy and past and you have it :)
https://gist.github.com/umairidrees/8952054#file-php-save-db-table-as-csv
Okay, here's my dilemma.
I've been working on a Wordpress plugin for Medical Marcom to automatically update his List of US Twitter Doctors. Basically, it provides the ability to create a form where users can request to be added to the list, requests can be confirmed in the admin panel, when added they're available in the Excel file and the initial data is filled out, and finally, certain fields are automatically updated throughout the week.
Here's the problem.
My code is generating an Excel file with PHP using PHPExcel. However, I need to have a simple autofilter applied to the sheet at startup (honestly, I don't know what the big deal is... anyone can easily apply an autofilter in Excel, but he wants it available from the start). So, I tried applying the code I found:
$excel->getActiveSheet()->setAutoFilter('A1:J' . $row);
$excel is my PHPExcel instance. $row is the last row being outputted from the database. The file is generated immediately when the url is clicked and PHP's headers are set to translate the output as an Excel file, like so:
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=" . $file);
But when I open the file, no autofilters are set... I tried posting a question over at PHPExcel's website, but I didn't get any replies, so I decided to ask here.
Does anyone know what I may be doing wrong? For now he's going with the original file (updated a bit, though) until this issue is resolved.
In case anyone comes across this question. The feature has been implemented both for XLSX and XLS.
You just need to specify the range of your header row for it to work:
$excel->getActiveSheet()->setAutoFilter('A1:J1');
PHPSpreadsheet have a much nicer implementation if you want your whole sheet auto-filtered:
$sheet->setAutoFilter(
$sheet->calculateWorksheetDimension()
);
I understand from this link that it has not been implemented yet.
It seems like it is still a work item (with a low priority sorry).
[EDIT] seems like it may work with Excel 2007 (see this work item). Which version of Excel do your client use?
I am generating a simple csv file using php. The file contains some user's personal data.
When I open the generated file in office, the addresses are not displayed in full height. I have to double click on the cell for the address to be shown fully (in full width and height) otherwise I can only see the first word/number of the address.
Also, I have date of births displayed as ######, I have to expand the whole column to see them fully.
This doesn't happen in open office.
Is there any way to force MS Office to show all fields in full? Because otherwise it'll be to confusing for the people who will use (Hey where are all the details!:)
Thanks :)
I don't think you can "format" your sheets with CSV. You will have to produce some other file format that Excel understands. I would suggest XML which is really easy to generate.
Just make a sample sheet with the data you want, save it as XML and you'll see how your file should be generated.
Or you could use some ready-made PHP solution for writing excel files if you can't be bothered with analysing the XML file.
you could try the auto-size columns feature.
This is a UI issue with how Excel works, you can't force Excel or anything else how they handle it.
The quickest work around is to perhaps create an XLS file that runs a macro to retrieve the CVS file and format the cells as needed, but there's nothing you can do inside the CSV to affect what Excel is displaying.