gpt4 book ai didi

php - 当我使用 CakePHP 2.x 生成 Excel 文件时文件损坏

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:19 24 4
gpt4 key购买 nike

我正在尝试使用 CakePHP 2.5.4 将我的数据库信息导出到 Excel。但是,Excel 无法打开生成的文件 myfilename.xlsx。给出的错误是:“文件格式或文件扩展名无效。请确认文件未损坏且文件扩展名与文件格式匹配”。

我使用以下代码在我的 Controller 中搜索数据并导出到 Excel:

<?php
$this->PhpExcel->createWorksheet();
$this->PhpExcel->setDefaultFont('Calibri', 12);

// define table cells
$table = array(
array('label' => __('Name'), 'width' => 'auto', 'filter' => true),
array('label' => __('Email'), 'width' => 'auto'),
array('label' => __('Second Email'), 'width' => 'auto'),
array('label' => __('Third Email'), 'width' => 'auto'),

);

// heading
$this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true));

// data
foreach ($data as $d) {

//if($d['Applicant']['name'] != '(NO BORRAR!!!!!)'){
$this->PhpExcel->addTableRow(array(
$d['Applicant']['name'],
$d['Applicant']['mail'],
$d['Applicant']['mail2'],
$d['Applicant']['mail_mother']
));
//}
}

$this->PhpExcel->addTableFooter();
$this->PhpExcel->output();
exit;
?>

谁能告诉我解决方法?

最佳答案

Excel cannot open the file 'filename.xlsx' because the file format for the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

但是,此错误消息不限于用户通过电子邮件获取的 Excel 文件。相反,它也经常遇到常用文件。您可能会发现,他们在上面工作了很长时间的 Excel 工作簿在您尝试打开它时突然开始抛出此错误消息。

要找出与此错误消息关联的唯一编号,请同时按“CTRL+SHIFT+I”。之后,除了错误消息外,您还会收到以下编号:

"101590"

在以下情况下遇到此错误消息:

原因:

  1. 文件与使用的 Excel 版本不兼容。
  2. 文件已损坏。

解决方案:您可以尝试下面提到的解决方案来纠正此错误消息并重新获得对 Excel 工作表的访问权限。

更改 Excel 工作表的文件扩展名:

有时,问题是由于您的 Excel 文件的文件扩展名不受支持而导致的。要检查 xlsx 扩展的支持,请转到“我的电脑”。浏览“工具 >> 文件夹选项 >> 文件类型”。检查那里是否包含“xlsx”扩展名。如果不是,则重命名此文件并将“.xlsx”更改为“.xls”。

enter image description here

使用 MS Excel 的“打开并修复”选项:

为此,打开 Excel,然后转到“文件 >> 打开”。选择要打开的文件,但不要双击它。转到“打开”对话框中提供的“打开”下拉菜单,然后从那里选择“打开并修复”选项。此过程修复损坏的 Excel 工作表并正常打开它。

enter image description here

Excel 恢复软件:

如果上述解决方案都不能为您做任何需要的事情,那么建议使用 Excel 修复软件。 Stellar Phoenix Excel Repair 就是这样一款软件。它修复损坏的 Excel 工作表并恢复存储在其中的整个数据而不改变其原始格式。

You can try any of the above mentioned resolutions to rectify this error message: however, Excel repair software is best-suited one for the case wherein other two methods fail to do any needful to you.

关于php - 当我使用 CakePHP 2.x 生成 Excel 文件时文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28630908/

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