在使用PHPElectron 表格和PHP8.2中的CodeIgniter 3导出Excel时,我遇到了一个问题.错误消息如下图所示:

enter image description here

以下是我正在使用的代码片段:

$fileName = 'activityReport.xlsx';
                        $this->load->library('ciSpreadsheet');
                        $spreadsheet = new Spreadsheet();
                        $spreadsheet->getProperties()->setCreator("CalibOn")
                                ->setLastModifiedBy("CalibOn")
                                ->setTitle("Activity Report")
                                ->setSubject("Activity Report")
                                ->setDescription("Activity Report")
                                ->setKeywords("")
                                ->setCategory("Report");
                        $spreadsheet->setActiveSheetIndex(0);
                        $col_array = array('Activity Date', 'Activity Time', 'Account Name', 'Activity Type', 'Contacted', 'Outcome', 'Description', 'Activity By', 'Created Date', 'Created Time');
                        $col_array_key = array('ActivityDate', 'ActivityTime', 'AccountName', 'ActivityType', 'Contacted', 'Outcome', 'Description', 'ActivityBy', 'CreatedDate', 'CreatedTime');
                        $col_count = 1;
                        $row_count = 1;
                        $sheet = $spreadsheet->getActiveSheet();
                        foreach ($col_array as $value) {
                            $sheet->setCellValue($this->getNameFromNumber($col_count) . $row_count, $value);
                            $col_count++;
                        }
                        $row_count++;

                        foreach ($report_array as $key => $value) {

                            $col_count = 1;
                            foreach ($col_array_key as $key_c => $value_col) {

                                $sheet->setCellValue($this->getNameFromNumber($col_count) . $row_count, $value->$value_col);
                                $col_count++;
                            }
                            $row_count++;
                        }

                        header('Content-Type: application/vnd.ms-excel');
                        header('Content-Disposition: attachment;filename="' . $fileName . '"');
                        header('Cache-Control: max-age=0');
                        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');


                        $writer->save("php://output");
                        exit;

从数据库获取的reportData变量遵循以下示例格式:

Array(
    [0] => Array(
        [ActivityDate] => 2022-06-28,
        [ActivityTime] => 14:00:00,
        [AccountName] => "Name",
        [ActivityType] => "Visit",
        [Description] => "desc",
        [Contacted] => "completed by",
        [Outcome] => ,
        [ActivityBy] => "generated by",
        [CreatedDate] => 2022-07-08,
        [CreatedTime] => 17:13:59
    )
);

我正在寻求帮助以解决错误并成功导出Excel文件.如有任何关于解决问题的指导或建议,我们将不胜感激.

推荐答案

请在编写器初始化后使用此选项.

$writer->setOffice2003Compatibility(true);

另外,将标题内容类型更改为下面的给定代码段

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

Php相关问答推荐

打印SQL表内容时在DIV内添加断点

未在moodle上运行的计划任务

在冲突和几何上插入的Postgres参数化查询在PHP中不起作用

从AJAX响应中获取一个未定义的html

根据WooCommerce Cart小计阈值自动应用优惠券

在php中,方法之间的属性链接是如何工作的

防止同时从Laravel中的用户帐户中提取

在WooCommercestore 页面上显示库存产品属性的值

更改特定产品标签的 Woocommerce 产品名称

Firefox 115 会话行为:为什么页面无法找到要加载的现有会话 ID?

当所有对象都属于同一类型时,我可以省略 PHP in_array() 中的 strict 参数吗?

CodeIgniter 4中嵌套过滤器组不起作用

仅在管理员新订单通知中显示WooCommerce订单商品自定义元数据

在PHP WordPress插件中使用变量连接路径时的问题解决方法

Laravel 路由参数中的 ":" 是什么?

我需要一个正则表达式模式来获取在不包含特定字符串后面的模式中的文本

判断类别是否存在 Shopware 6

PHP 将数组值插入到 csv 列

Laravel 10 单元测试看不到来自 Artisan Command 的数据库更改

Laravel 从嵌套数组的第二级获取最后一项