Personally I've never used this method before and I havent been able to find any information to help me.
The page I'm updating uses the following to export a html table as an Excel file.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Target-Report.xls");
header("Pragma: no-cache");
header("Expires: 0");
It generates the file just fine, I just need to make the report more presentable.
Is there a way to set Cell type in Excel (i.e. Percentage) and set decimal places (in cells where I've created excel formulas).
I'd prefer to use PHPExcel, but I dont have time to practically rewrite the entire script
Related
I have been trying to save html data in excel format(export to excel functionality basically) in PHP. I achieved that by changing header to
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
But on opening the file, it shows a warning for above excel 2003 applications. So can I save the excel in 2008 or 2010 format just by modifying the header or do I have to go for other frameworks providing this feature like "PHPExcel" or something. And yes, I am using CodeIgniter for my website
Thanks in advance.
You need to use some libraries to generate Excel tables.
Standart HTML output is not converted automatically to the specified format if you only set the Content-Type you want.
Try changing the MIME type to:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
instead of
application/vnd.ms-excel
I've seen a couple of similar questions but have not had one that answers my issue.
I have an export to CSV button which exports results from my database to CSV.
I don't want to save the file, but I just want to use headers to export the echo content to a file which should then be opened in Excel (or similar product).
All works fine but Excel does not appear to separate the values, but rather shows all rows in 1 row with the commas intact. I found a solution elsewhere where I should add "sep=,\r\n" to the first line to tell Excel to use commas as the delimiter, which then makes it work great; but, in other products it now shows the sep=, on the first line and continues with the remaining of the output.
Here is the code I'm using:
header("Expires: 0");
header("Cache-control: private");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=export.csv");
echo "sep=,\r\n"; // This makes it work in excel but fails in other products such as openoffice
echo "this,is,just,a,test\r\n";
exit;
As Mark Baker suggested, as the dilimeter is set by locale settings, I changed it to ; and it worked like a charm :) Thank you!
An Excel file is stored in a database (hexvalues of former binary data). I need to read it and make it available for download.
Here's the code...
$out = hex2bin($out); // the stuff from the database
header("Content-Type: application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename='$filename'");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
die($out);
Actually I receive stuff... and it looks like the original file with a slight difference.
When using a hex-editor, it shows
ÐÏࡱá
instead of
ÐÏࡱá
(looks like an additional tab). So Excel isn't able to show the sheet but the raw data.
That's not from the data in $out; the conversion bin2hex (during import in database) and hex2bin (export from database) works without problems. If I write $out to a file via fopen / fwrite, it's exactly the original Excel file (which I can open normally).
How can I accomplish this?
Ensure you don't have this extra tab character before the opening <?php tag or after the closing one ?>. Actually, it is good practice to omit the closing tag exactly for this reason.
I have written a script using header function that generates CSV file.Let me explain in detail.
Step 1) I am saving records in mysql.Step 2) I am creating CSV file from saved records from database Step 3) I want to attach that CSV in attachments so that i could send it to different recipents!! I have completed the two steps.But i dont want download functionality in CSV file generation process as i dont want this file available to anyone. My code looks like this:
$file_name = "Register_" . date('l');
$file_name.=".csv";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
Where do I need to make changes?
just remove all calls to header() and the last print
You could do what you try to by using the ob_buffer functions and 'catching' the generated output. However the whole approach is a little strange:
the header functions have nothing to do with creating emails. They serve the purpose to specify how a browser should handle transferred data. 'Cause this is what you do: you send headers and data. Don't.
Instead: write the csv data into a buffer or file and create an email using one of the available email classes (google...). Then take that crafted email and send it. No header() function required for that.
You can use AddStringAttachment:
$file_name = "Register_" . date('l') . ".csv";
$excelContent = "$header\n$data";
$phpmailer->AddStringAttachment($excelContent, $file_name, 'base64', 'application/vnd.ms-excel');
I have a code like this.
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
Now, this gets saved in my desktop. If i wish to save the xls file in D:/xls/
then how to add the code.
Please help me.
Thanks -
Haan
If i wish to save the xls file in D:/xls/ then how to add the code.
The server can not dictate to the client where to save a file.
That is a decision only the client can make.
Imagine if the server could tell the client where to save a file. You could overwrite any file on the user's system. That'd be the biggest security breach in the history of poorly designed internet security breaches.