-2

Try to export xlsx file using PhpSpreadSheet in CodeIgniter 3. When i run the url, it shows This site can't be reach. Web might be temporarily down or it may have moved permanently to a new web address.

Controller

require 'vendor/autoload.php';
                
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

function generateLR(){
                    // create file name
  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment;filename="result.xlsx');
  header('Cache-Control: max-age=0');
  $spreadsheet = new Spreadsheet();
  $sheet = $spreadsheet->getActiveSheet();
  $sheet->setCellValue('A1', 'Hello World !');
  $writer = new Xlsx($spreadsheet); 
  $writer->save('php://output');
}

But if i try to save locally $writer->save('hello world.xlsx');. The output save successfully in project folder.

Also try using IOFactory but the result is the same : web is temporary down

require 'vendor/autoload.php';
                
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;

function generateLR(){
                    // create file name
  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment;filename="result.xlsx');
  header('Cache-Control: max-age=0');
  $spreadsheet = new Spreadsheet();
  $sheet = $spreadsheet->getActiveSheet();
  $sheet->setCellValue('A1', 'Hello World !');
  $writer = IOFactory::createWriter($spreadsheet,'Xlsx');
  $writer->save('php://output');
}

Error image enter image description here Is there any env problem ?

lauwis
  • 323
  • 1
  • 4
  • 15

2 Answers2

1

Found the answer. The problem is the permission of sys_get_temp_dir(). My temp folder is unaccessable by default in Macbook. After change the permission. Excel can be created in localhost

lauwis
  • 323
  • 1
  • 4
  • 15
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 31 '22 at 02:21
-2

1.Check for your php version and make sure it's not 8.0 source : https://stackoverflow.com/a/77005820/18430781

This is a known issue with PhpSpreadsheet on PHP 8.0.

2.Try to log the error:

<?php
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', 'error_log.txt');
error_reporting(E_ALL);
// Rest of your PHP code here
  • Please don't post answers that solely link to any other question. Instead, add all relevant parts to your answer. Also, add some explanation such that others can learn from it. Why should using 8.0 cause such a problem? – Nico Haase Aug 30 '23 at 08:52
  • If saving the file works properly, but directly sending it to the browser does not, how could that problem be related to PHP 8? Also, keep in mind that the OP posted an answer that resolve their problem – Nico Haase Aug 30 '23 at 08:58
  • @NicoHaase saving works but without any data in it ( blank file ). But if we output it to the browser then it will show "This site can't be reached" like in the screenshot provided in the question. I got same issue and solved by updating php version to 8.1 I know this question is one year old and I'm talking about new issue, but I guess this may help someone. https://github.com/PHPOffice/PhpSpreadsheet/issues/3684 – rickoonidioser Aug 30 '23 at 09:15
  • What makes you think that a blank file was saved? The OP hasn't written that in the initial post – Nico Haase Aug 30 '23 at 10:21
  • @NicoHaase I didn't mention that I've been thinking about whether a blank file was saved (: I'm uncertain whether my response will fully address the issue raised in this question. As I encountered a similar problem before, I simply shared the approach I took to resolve it. Hoping it might be of assistance to someone. Would you like me to remove my response so that someone else can offer better insights? – rickoonidioser Aug 30 '23 at 11:36
  • Give the best answer you can. Offer the best response you can to any open-ended or newly added questions. – Mukhila Asokan Sep 01 '23 at 06:06