Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8055857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:31:36+00:00 2026-06-05T08:31:36+00:00

$workbook = new PHPExcel(); $workbook->createSheet()->setTitle(‘whatt’)->setCellValue( ‘A1’, ‘Another sheet’ ); $workbook->createSheet()->setTitle(‘whatt’)->setCellValue( ‘A1’, ‘Another sheet’ );

  • 0
$workbook = new PHPExcel();
$workbook->createSheet()->setTitle('whatt')->setCellValue( 'A1', 'Another sheet' );
$workbook->createSheet()->setTitle('whatt')->setCellValue( 'A1', 'Another sheet' );
$objWriter = new PHPExcel_Writer_Excel2007($workbook);
$objWriter->setOffice2003Compatibility(true);

ob_start();
$objWriter->save( "php://output" );
$contents = ob_get_contents();
ob_end_clean();



// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

$size = count($contents);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Type: application/vnd.msexcel; charset=utf-8');
header("Content-Disposition: attachment; filename=\"test.xlsx\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$size}");
echo $contents;
exit;

When I download the sheet it comes down as 1 byte, with one sheet where the first cell is “P” ( because the file only contains that letter )
If I comment out the second createSheet line it works perfect. Any idea?

EDIT

I accepted the answer below which told me it wasn’t PHPExcel’s fault and it wasn’t.
If I remove the headers and keep output buffering, as a test, it still works.

ob_start();
$objWriter->save( "php://output" );
$contents = ob_get_contents();
ob_end_clean();

file_put_contents( "/mypath/uploads/test.xlsx", $contents );

I can download the file no problem. This leads me to believe the headers are screwing it up somehow, which is very bizarre considering there’s no issue if I only used createSheet once. I guess I’ll post that here when I figure it out.

….

And I figured out why the headers were messing it up. Apparently using count($contents) with one added sheet was fine, but with two added sheets it would only return 1. I changed it to strlen($contents) and it worked. Bonus points if anyone can tell me why that happened 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:31:38+00:00Added an answer on June 5, 2026 at 8:31 am

    No idea without a lot of debugging. Get rid of the redundant output buffering, and it works. This is the cause of your problem, nothing in PHPExcel.
    So what are you doing subsequently with $contents?

    Calling createSheet() twice in a row isn’t corrupting anything: though why are you trying to create a workbook containing two worksheets with the same title anyway? PHPExcel will actually rename the second added worksheet as whatt2 to avoid problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am copying several worksheets from one workbook to another. The first sheet contains
With Spreadsheet::WriteExcel , I can create a new workbook, but what if I want
I've got unit test with this code: Excel.Application objExcel = new Excel.Application(); Excel.Workbook objWorkbook
I have this code: app = new Application(); app.Visible = false; workbook = app.Workbooks.Add(1);
Given a workbook with cross-sheet formulas generated by Coldfusion 9 via Apache POI .
Creating a new Excel workbook as in: Dim xl As Excel.Application Dim wb As
Given the following code: using Aspose.Cells // {...} Workbook workbook = new Workbook(); Worksheet
I have a need to copy a worksheet from one workbook into another and
I have an Excel workbook with a number of features: One main user-facing sheet
After creating a new workbook, I am trying to solve a newly created worksheet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.