gpt4 book ai didi

PHP电子表格在浏览器选项卡中显示空白

转载 作者:行者123 更新时间:2023-12-02 00:35:30 25 4
gpt4 key购买 nike

我用过https://github.com/PHPOffice/phpspreadsheet这个包来下载codeigniter 中的 csv 文件

当我尝试下载时,它在浏览器选项卡中显示空白并且没有显示下载选项

没有错误

尝试使用以下代码

   <?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

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

class Reports extends Admin_Controller
{
public function download()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
}
}

有人可以帮忙吗??谢谢

最佳答案

希望对您有所帮助:

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

class Reports extends Admin_Controller
{

public function index()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');

$spreadsheet->setActiveSheetIndex(0);

$filename = 'hello world';

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');

$writer->save('php://output');
}
}

对于 csv,只需将扩展名 .xlsx 更改为 .csv(区分大小写)

关于PHP电子表格在浏览器选项卡中显示空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49794384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com