How does “new PhpOfficePhpSpreadsheetSpreadsheet()” work The Next CEO of Stack OverflowPHPExcel Export for Larger Files failshow to download excel file in internet explorer browser with phpexcel libraryHow does PHP 'foreach' actually work?PhpSpreadsheet: Permissions | ZipArchive::close(): Failure to create temporary fileExport dynamic array to Excel PHPSpreadsheetHow to avoid Excel “bad format error” when saving spreadsheetPhpSpreadsheet without composer. “use” doesn't work in php fileHow do i use Php Spreadsheet with Fat free to read and edit an excel file?PhpSpreadsheet : SUMIF formula is not working
How do scammers retract money, while you can’t?
Why has the US not been more assertive in confronting Russia in recent years?
Between two walls
Do I need to enable Dev Hub in my PROD Org?
Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?
Should I tutor a student who I know has cheated on their homework?
What's the best way to handle refactoring a big file?
How did people program for Consoles with multiple CPUs?
Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis
Are there any unintended negative consequences to allowing PCs to gain multiple levels at once in a short milestone-XP game?
Rotate a column
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
How to make a variable always equal to the result of some calculations?
Is HostGator storing my password in plaintext?
What flight has the highest ratio of time difference to flight time?
Why does the UK parliament need a vote on the political declaration?
Is it professional to write unrelated content in an almost-empty email?
Is "for causing autism in X" grammatical?
Indicator light circuit
What can we do to stop prior company from asking us questions?
In excess I'm lethal
Preparing Indesign booklet with .psd graphics for print
How did the Bene Gesserit know how to make a Kwisatz Haderach?
Non-deterministic sum of floats
How does “new PhpOfficePhpSpreadsheetSpreadsheet()” work
The Next CEO of Stack OverflowPHPExcel Export for Larger Files failshow to download excel file in internet explorer browser with phpexcel libraryHow does PHP 'foreach' actually work?PhpSpreadsheet: Permissions | ZipArchive::close(): Failure to create temporary fileExport dynamic array to Excel PHPSpreadsheetHow to avoid Excel “bad format error” when saving spreadsheetPhpSpreadsheet without composer. “use” doesn't work in php fileHow do i use Php Spreadsheet with Fat free to read and edit an excel file?PhpSpreadsheet : SUMIF formula is not working
I've got a simple code as the following (docs) :
// Set the headers to a downloadble content
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="nomfichier.xlsx"');
// Initialize spreadsheet
$objPHPExcel = new PhpOfficePhpSpreadsheetSpreadsheet();
$objPHPExcel->setActiveSheetIndex(0);
// Get the sheet and set the value of the first cell
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
// Create the .xlsx file and download it
$writer = PhpOfficePhpSpreadsheetIOFactory::createWriter($objPHPExcel, 'Xlsx');
$writer->save('/tmp/temp.csv');
Knowing that this code is in the middle of my PHP page, the downloaded content is the whole text of the HTML page trying to fit into my excel file in the first column and not the simple result I'm looking for : Value named ID in the first cell and first column.
What did I miss? Does this code needs to be contained in it's own file?
php spreadsheet phpspreadsheet
|
show 3 more comments
I've got a simple code as the following (docs) :
// Set the headers to a downloadble content
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="nomfichier.xlsx"');
// Initialize spreadsheet
$objPHPExcel = new PhpOfficePhpSpreadsheetSpreadsheet();
$objPHPExcel->setActiveSheetIndex(0);
// Get the sheet and set the value of the first cell
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
// Create the .xlsx file and download it
$writer = PhpOfficePhpSpreadsheetIOFactory::createWriter($objPHPExcel, 'Xlsx');
$writer->save('/tmp/temp.csv');
Knowing that this code is in the middle of my PHP page, the downloaded content is the whole text of the HTML page trying to fit into my excel file in the first column and not the simple result I'm looking for : Value named ID in the first cell and first column.
What did I miss? Does this code needs to be contained in it's own file?
php spreadsheet phpspreadsheet
Can you clarify your question?
– Justinas
Mar 8 at 14:18
@Justinas defineclarify? What is it you don't understand? :-)
– Islam Elshobokshy
Mar 8 at 14:18
It doesn't need to be contained in it's own file. You can move it to the top of the file if you want. (don't forget to have anexit;after this code then). The reason for it is that the web server (and client) don't know what your intended response should be so it can't "remove" the already outputted HTML for you. Also, anyheader()-call must be before any output at all. Headers are always sent before any content, so if you output any content, you can't set additional headers for that response.
– Magnus Eriksson
Mar 8 at 14:24
@MagnusEriksson putting the code at the top of the file and exiting right after the save does download an empty excel file - but not with the cell value I specified. Any idea why?
– Islam Elshobokshy
Mar 8 at 14:28
That's because you're not sending the file to the client. You're just saving it on your server astemp.csvin thetmp-folder. If you want to output it instead, use$writer->save('php://output');
– Magnus Eriksson
Mar 8 at 14:36
|
show 3 more comments
I've got a simple code as the following (docs) :
// Set the headers to a downloadble content
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="nomfichier.xlsx"');
// Initialize spreadsheet
$objPHPExcel = new PhpOfficePhpSpreadsheetSpreadsheet();
$objPHPExcel->setActiveSheetIndex(0);
// Get the sheet and set the value of the first cell
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
// Create the .xlsx file and download it
$writer = PhpOfficePhpSpreadsheetIOFactory::createWriter($objPHPExcel, 'Xlsx');
$writer->save('/tmp/temp.csv');
Knowing that this code is in the middle of my PHP page, the downloaded content is the whole text of the HTML page trying to fit into my excel file in the first column and not the simple result I'm looking for : Value named ID in the first cell and first column.
What did I miss? Does this code needs to be contained in it's own file?
php spreadsheet phpspreadsheet
I've got a simple code as the following (docs) :
// Set the headers to a downloadble content
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="nomfichier.xlsx"');
// Initialize spreadsheet
$objPHPExcel = new PhpOfficePhpSpreadsheetSpreadsheet();
$objPHPExcel->setActiveSheetIndex(0);
// Get the sheet and set the value of the first cell
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
// Create the .xlsx file and download it
$writer = PhpOfficePhpSpreadsheetIOFactory::createWriter($objPHPExcel, 'Xlsx');
$writer->save('/tmp/temp.csv');
Knowing that this code is in the middle of my PHP page, the downloaded content is the whole text of the HTML page trying to fit into my excel file in the first column and not the simple result I'm looking for : Value named ID in the first cell and first column.
What did I miss? Does this code needs to be contained in it's own file?
php spreadsheet phpspreadsheet
php spreadsheet phpspreadsheet
edited Mar 8 at 14:20
Islam Elshobokshy
asked Mar 8 at 14:13
Islam ElshobokshyIslam Elshobokshy
1
1
Can you clarify your question?
– Justinas
Mar 8 at 14:18
@Justinas defineclarify? What is it you don't understand? :-)
– Islam Elshobokshy
Mar 8 at 14:18
It doesn't need to be contained in it's own file. You can move it to the top of the file if you want. (don't forget to have anexit;after this code then). The reason for it is that the web server (and client) don't know what your intended response should be so it can't "remove" the already outputted HTML for you. Also, anyheader()-call must be before any output at all. Headers are always sent before any content, so if you output any content, you can't set additional headers for that response.
– Magnus Eriksson
Mar 8 at 14:24
@MagnusEriksson putting the code at the top of the file and exiting right after the save does download an empty excel file - but not with the cell value I specified. Any idea why?
– Islam Elshobokshy
Mar 8 at 14:28
That's because you're not sending the file to the client. You're just saving it on your server astemp.csvin thetmp-folder. If you want to output it instead, use$writer->save('php://output');
– Magnus Eriksson
Mar 8 at 14:36
|
show 3 more comments
Can you clarify your question?
– Justinas
Mar 8 at 14:18
@Justinas defineclarify? What is it you don't understand? :-)
– Islam Elshobokshy
Mar 8 at 14:18
It doesn't need to be contained in it's own file. You can move it to the top of the file if you want. (don't forget to have anexit;after this code then). The reason for it is that the web server (and client) don't know what your intended response should be so it can't "remove" the already outputted HTML for you. Also, anyheader()-call must be before any output at all. Headers are always sent before any content, so if you output any content, you can't set additional headers for that response.
– Magnus Eriksson
Mar 8 at 14:24
@MagnusEriksson putting the code at the top of the file and exiting right after the save does download an empty excel file - but not with the cell value I specified. Any idea why?
– Islam Elshobokshy
Mar 8 at 14:28
That's because you're not sending the file to the client. You're just saving it on your server astemp.csvin thetmp-folder. If you want to output it instead, use$writer->save('php://output');
– Magnus Eriksson
Mar 8 at 14:36
Can you clarify your question?
– Justinas
Mar 8 at 14:18
Can you clarify your question?
– Justinas
Mar 8 at 14:18
@Justinas define
clarify? What is it you don't understand? :-)– Islam Elshobokshy
Mar 8 at 14:18
@Justinas define
clarify? What is it you don't understand? :-)– Islam Elshobokshy
Mar 8 at 14:18
It doesn't need to be contained in it's own file. You can move it to the top of the file if you want. (don't forget to have an
exit; after this code then). The reason for it is that the web server (and client) don't know what your intended response should be so it can't "remove" the already outputted HTML for you. Also, any header()-call must be before any output at all. Headers are always sent before any content, so if you output any content, you can't set additional headers for that response.– Magnus Eriksson
Mar 8 at 14:24
It doesn't need to be contained in it's own file. You can move it to the top of the file if you want. (don't forget to have an
exit; after this code then). The reason for it is that the web server (and client) don't know what your intended response should be so it can't "remove" the already outputted HTML for you. Also, any header()-call must be before any output at all. Headers are always sent before any content, so if you output any content, you can't set additional headers for that response.– Magnus Eriksson
Mar 8 at 14:24
@MagnusEriksson putting the code at the top of the file and exiting right after the save does download an empty excel file - but not with the cell value I specified. Any idea why?
– Islam Elshobokshy
Mar 8 at 14:28
@MagnusEriksson putting the code at the top of the file and exiting right after the save does download an empty excel file - but not with the cell value I specified. Any idea why?
– Islam Elshobokshy
Mar 8 at 14:28
That's because you're not sending the file to the client. You're just saving it on your server as
temp.csv in the tmp-folder. If you want to output it instead, use $writer->save('php://output');– Magnus Eriksson
Mar 8 at 14:36
That's because you're not sending the file to the client. You're just saving it on your server as
temp.csv in the tmp-folder. If you want to output it instead, use $writer->save('php://output');– Magnus Eriksson
Mar 8 at 14:36
|
show 3 more comments
1 Answer
1
active
oldest
votes
There are a couple of issues with your code.
Location of the code
Since a file download simply is the server sending a bunch of data to the client, together with some headers that tells the browser how to act on the content, you can't output anything other than the file contents. Doing so will add that to the file content as well (and your downloaded file will most likely be corrupt).
Also, any header()-call in needs to be before any output at all. Headers are sent first, then the content. So if you've already outputted content, you can't send additional headers.
Solutions:
Move your code to it's own file or move it to the top of the current file (before any other output like HTML etc). If it's in the top, don't forget to add an exit; after your code to stop it from parsing and sending the HTML as well.
Outputting the Excel document
Your current code doesn't actually output the document at any point. All it does is saving it on the server, in the /tmp/-folder.
Solutions:
To output the document instead of saving it, change:
$writer->save('/tmp/temp.csv');
to
$writer->save('php://output'); // Send it to the output buffer instead
If you want to save it and send it to the client, you could try (I'm not sure if this works, but it should), then just keep both:
$writer->save('/tmp/temp.csv');
$writer->save('php://output');
If that, for some reason, wouldn't work, you can save it first and then read it back to the client with readfile(). Something like this:
$writer->save('/tmp/temp.csv');
readfile('/tml/temp.csv');
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065001%2fhow-does-new-phpoffice-phpspreadsheet-spreadsheet-work%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are a couple of issues with your code.
Location of the code
Since a file download simply is the server sending a bunch of data to the client, together with some headers that tells the browser how to act on the content, you can't output anything other than the file contents. Doing so will add that to the file content as well (and your downloaded file will most likely be corrupt).
Also, any header()-call in needs to be before any output at all. Headers are sent first, then the content. So if you've already outputted content, you can't send additional headers.
Solutions:
Move your code to it's own file or move it to the top of the current file (before any other output like HTML etc). If it's in the top, don't forget to add an exit; after your code to stop it from parsing and sending the HTML as well.
Outputting the Excel document
Your current code doesn't actually output the document at any point. All it does is saving it on the server, in the /tmp/-folder.
Solutions:
To output the document instead of saving it, change:
$writer->save('/tmp/temp.csv');
to
$writer->save('php://output'); // Send it to the output buffer instead
If you want to save it and send it to the client, you could try (I'm not sure if this works, but it should), then just keep both:
$writer->save('/tmp/temp.csv');
$writer->save('php://output');
If that, for some reason, wouldn't work, you can save it first and then read it back to the client with readfile(). Something like this:
$writer->save('/tmp/temp.csv');
readfile('/tml/temp.csv');
add a comment |
There are a couple of issues with your code.
Location of the code
Since a file download simply is the server sending a bunch of data to the client, together with some headers that tells the browser how to act on the content, you can't output anything other than the file contents. Doing so will add that to the file content as well (and your downloaded file will most likely be corrupt).
Also, any header()-call in needs to be before any output at all. Headers are sent first, then the content. So if you've already outputted content, you can't send additional headers.
Solutions:
Move your code to it's own file or move it to the top of the current file (before any other output like HTML etc). If it's in the top, don't forget to add an exit; after your code to stop it from parsing and sending the HTML as well.
Outputting the Excel document
Your current code doesn't actually output the document at any point. All it does is saving it on the server, in the /tmp/-folder.
Solutions:
To output the document instead of saving it, change:
$writer->save('/tmp/temp.csv');
to
$writer->save('php://output'); // Send it to the output buffer instead
If you want to save it and send it to the client, you could try (I'm not sure if this works, but it should), then just keep both:
$writer->save('/tmp/temp.csv');
$writer->save('php://output');
If that, for some reason, wouldn't work, you can save it first and then read it back to the client with readfile(). Something like this:
$writer->save('/tmp/temp.csv');
readfile('/tml/temp.csv');
add a comment |
There are a couple of issues with your code.
Location of the code
Since a file download simply is the server sending a bunch of data to the client, together with some headers that tells the browser how to act on the content, you can't output anything other than the file contents. Doing so will add that to the file content as well (and your downloaded file will most likely be corrupt).
Also, any header()-call in needs to be before any output at all. Headers are sent first, then the content. So if you've already outputted content, you can't send additional headers.
Solutions:
Move your code to it's own file or move it to the top of the current file (before any other output like HTML etc). If it's in the top, don't forget to add an exit; after your code to stop it from parsing and sending the HTML as well.
Outputting the Excel document
Your current code doesn't actually output the document at any point. All it does is saving it on the server, in the /tmp/-folder.
Solutions:
To output the document instead of saving it, change:
$writer->save('/tmp/temp.csv');
to
$writer->save('php://output'); // Send it to the output buffer instead
If you want to save it and send it to the client, you could try (I'm not sure if this works, but it should), then just keep both:
$writer->save('/tmp/temp.csv');
$writer->save('php://output');
If that, for some reason, wouldn't work, you can save it first and then read it back to the client with readfile(). Something like this:
$writer->save('/tmp/temp.csv');
readfile('/tml/temp.csv');
There are a couple of issues with your code.
Location of the code
Since a file download simply is the server sending a bunch of data to the client, together with some headers that tells the browser how to act on the content, you can't output anything other than the file contents. Doing so will add that to the file content as well (and your downloaded file will most likely be corrupt).
Also, any header()-call in needs to be before any output at all. Headers are sent first, then the content. So if you've already outputted content, you can't send additional headers.
Solutions:
Move your code to it's own file or move it to the top of the current file (before any other output like HTML etc). If it's in the top, don't forget to add an exit; after your code to stop it from parsing and sending the HTML as well.
Outputting the Excel document
Your current code doesn't actually output the document at any point. All it does is saving it on the server, in the /tmp/-folder.
Solutions:
To output the document instead of saving it, change:
$writer->save('/tmp/temp.csv');
to
$writer->save('php://output'); // Send it to the output buffer instead
If you want to save it and send it to the client, you could try (I'm not sure if this works, but it should), then just keep both:
$writer->save('/tmp/temp.csv');
$writer->save('php://output');
If that, for some reason, wouldn't work, you can save it first and then read it back to the client with readfile(). Something like this:
$writer->save('/tmp/temp.csv');
readfile('/tml/temp.csv');
edited Mar 8 at 15:04
answered Mar 8 at 14:59
Magnus ErikssonMagnus Eriksson
7,60041328
7,60041328
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065001%2fhow-does-new-phpoffice-phpspreadsheet-spreadsheet-work%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you clarify your question?
– Justinas
Mar 8 at 14:18
@Justinas define
clarify? What is it you don't understand? :-)– Islam Elshobokshy
Mar 8 at 14:18
It doesn't need to be contained in it's own file. You can move it to the top of the file if you want. (don't forget to have an
exit;after this code then). The reason for it is that the web server (and client) don't know what your intended response should be so it can't "remove" the already outputted HTML for you. Also, anyheader()-call must be before any output at all. Headers are always sent before any content, so if you output any content, you can't set additional headers for that response.– Magnus Eriksson
Mar 8 at 14:24
@MagnusEriksson putting the code at the top of the file and exiting right after the save does download an empty excel file - but not with the cell value I specified. Any idea why?
– Islam Elshobokshy
Mar 8 at 14:28
That's because you're not sending the file to the client. You're just saving it on your server as
temp.csvin thetmp-folder. If you want to output it instead, use$writer->save('php://output');– Magnus Eriksson
Mar 8 at 14:36