Jonny B'Good
Jonny B'Good

Reputation: 123

phpspreedsheet data coming out corrupt format

Just starting with PHP spreed sheets.

But when I save it's creating the file weird, please see the photos attached.

I don't understand why it is doing it....

now updated so straight to point with code below...?

Thanks in advance for help ....................

        <?php
    require __DIR__ . '/vendor/autoload.php';
    use PhpOffice\PhpSpreadsheet\IOFactory;
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

    $spreadsheet = new Spreadsheet();


    $spreadsheet->setActiveSheetIndex(0)
    ->setCellValue('A1', 'Reorder Report')
    ->setCellValue('A3', 'Report Created Date:')
    ->setCellValue('A4', 'Supplier:')
    ->setCellValue('A6', 'Period 1:')
    ->setCellValue('A7', 'Period 2:')
    ->setCellValue('A8', 'Period 3:')
    ->setCellValue('A10', 'Order to delivery time:')
    ->setCellValue('A11', 'Hold stock after order arrived:')
    ->setCellValue('A14', 'Product Code')

    ->setCellValue('B3', '')//create date *to add todays date
    ->setCellValue('B4', '')//supplier name
    ->setCellValue('B5', 'Days:')
    ->setCellValue('B6', '')
    ->setCellValue('B7', '')
    ->setCellValue('B8', '')
    ->setCellValue('B10', '')
    ->setCellValue('B11', '')
    ->setCellValue('B14', 'Description')

    ->setCellValue('C5', 'Start Date:')
    ->setCellValue('C6', '')
    ->setCellValue('C7', 'End Date:')
    ->setCellValue('C8', '')
    ->setCellValue('C14', 'Stock')
    ;



// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$spreadsheet->setActiveSheetIndex(0);

// Redirect output to a client’s web browser (Xls)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0

$writer = IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
exit;

?>

enter image description here

Upvotes: 0

Views: 1307

Answers (3)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

You can detect extraneous output this way:

$writer = IOFactory::createWriter($spreadsheet, 'Xls');
if (headers_sent($file, $line)) {
    throw new RuntimeException("Unexpected output at $file:$line");
}
$writer->save('php://output');

It's then just a matter of checking what's going on in the reported file and line.

Upvotes: 0

Jonny B&#39;Good
Jonny B&#39;Good

Reputation: 123

fixed it

something to do with the top 3 lines replaced with

<?php
require __DIR__ . '/vendor/autoload.php';
//use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

Here's your problem....

<html> 
<head>

at the top of your file.... that's sent to the output stream by PHP before the file itself, so you end up with a corrupted file

If you're sending a file to the browser; you must not send any additional data, such as html markup

Upvotes: 1

Related Questions