1. 首先你得有Ci框架才行啊.....codeigniter composer create-project codeigniter/framework 2. 直接撸代码..下载安装PHPoffice..... composer require phpoffice/phpspreadsheet 3. 接下来,你懂得,封装一个模型方法,哪里使
1. 首先你得有Ci框架才行啊.....codeigniter
composer create-project codeigniter/framework
2. 直接撸代码..下载安装PHPoffice.....
composer require phpoffice/phpspreadsheet
3. 接下来,你懂得,封装一个模型方法,哪里使(bu)用(hui)哪(dian)里(na)调(li).......
ExportXlsModel.php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class ExportXlsModel extends CI_Model{
/**
* 导出excel
* @param $data
* @param string $title
*/
public function export_excel($data, $title = '未命名.xlsx') {
// var_dump($data);
ini_set ('memory_limit', '1024M');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
foreach ($data as $key1=>$sub_data) { //列
foreach ($sub_data as $key2=>$item) { //行
$sheet->setCellValueExplicitByColumnAndRow($key2+1, $key1+1,$item,'s');
}
}
unset($data);
$writer = new Xlsx($spreadsheet);
unset($spreadsheet);
// $writer->save($title);
header("Pragma: public");
header("Expires: 0");
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Headers:content-type');
header('Access-Control-Allow-Credentials:true');
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header("Content-Disposition:attachment;filename=$title");
header("Content-Transfer-Encoding:binary");
$writer->save('php://output');
exit();
}
}
4. 用一下(ha)....
//把数据组装出呢个下面结构就可以啦......
$arr = [
['id','name','age','sex'], //这一行是表头
[1,'assasin',25,'male'], //第一行数据
[2,'bob',20,'male'], //第二行数据
[3,'juli',18,'female'] //第三行数据
];
$this->load->model('exportXlsModel');
$this->exportXlsModel->export_excel($export_array,'导出结果.xlsx');