dongleman4760 2018-12-23 06:01
浏览 134

phpspreadsheet:使用php mysql无效的文件扩展名错误

I am using phpspreadsheet to generate excel files from my database in CodeIgniter. While I try to open the exported excel file it shows me an error:

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

Here is my code:

model

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();
}

Controller:

<?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');

    }
}   

}

view

  <?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(); ?>

I am using phpspreadsheet for the first time. Can anyone help me to fix this?? Thanks in advance!

Edit 1

This same code works when I try with inline data, that is not using the data from database. The excel file gets exported and I am able to open it. here is the edited controller code for inline data:

    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');
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?