我正在使用phpspredsheet从codeigniter中的数据库生成excel文件。当我试图打开导出的excel文件时,它显示了一个错误:
excel无法打开文件“.xlsx”,因为文件格式或扩展名无效。验证文件是否已损坏,以及文件扩展名是否与文件格式匹配
这是我的密码:
模型
public function getAllMembersSearchByFamily($term)
{
$this->db->select('*,tab_family.family_name');
$this->db->from('tab_members');
$this->db->join('tab_family', 'tab_family.family_id = tab_members.family_id');
$this->db->where('tab_members.family_id', $term);
$query = $this->db->get();
return $query->result();
}
控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require "vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class Applications extends CI_Controller {
public function generatefamilyreport()
{
$output="";
$term = trim($this->input->post('searchterm'));
echo $term;
$result = $this->Family_model->getAllMembersSearchByFamily($term);
if(!empty($result))
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$i = 1;
foreach($result as $res)
{
$sheet->setCellValue('A'.$i, $res->name);
$i++;
}
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="users.xlsx"');
header('Cache-Control: max-age=0');
header('Expires: Fri, 11 Nov 2011 11:11:11 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer->save('php://output');
}
}
}
看法
<?php
$attributes = array('class' => 'search-form', 'id' => 'generatereport');
echo form_open('Applications/generatefamilyreport',$attributes);
?>
<input type="text" name="searchterm" id="generatereporttext" <?php if(isset($term) && !empty($term)) { ?> value="<?php echo $term;?>" <?php } ?>>
<button class="btn btn-danger" style="margin-left: 15px;">Generate Report</button>
<?php echo form_close(); ?>
我是第一次使用phpspredsheet。有人能帮我修一下吗??提前谢谢!
编辑1
当我尝试使用内联数据(即不使用数据库中的数据)时,同样的代码也可以工作。excel文件被导出,我可以打开它。以下是为内联数据编辑的控制器代码:
public function generatefamilyreport()
{
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->setTitle('Simple');
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('A1', 'This')
->setCellValue('B2', 'is')
->setCellValue('C1', 'a')
->setCellValue('D2', 'test.');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="simple.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
}
暂无答案!
目前还没有任何答案,快来回答吧!