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');
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.