您好,欢迎访问一九零五行业门户网

CI中使用PHPExcel导出数据到Excel

load->database();$query = $this->db->query(select * from `$table_name` where del= 1);// $query = mb_convert_encoding(gb2312, utf-8, $query);if(!$query)return false;// starting the phpexcel library
$this->load->library('phpexcel');$this->load->library('phpexcel/iofactory');$objphpexcel = new phpexcel();
$objphpexcel->getproperties()->settitle(export)->setdescription(none);$objphpexcel->setactivesheetindex(0)
->setcellvalue('a1', iconv('gbk', 'utf-8', '中文hello'))->setcellvalue('b2', 'world!')->setcellvalue('c1', 'hello');// field names in the first row$fields = $query->list_fields();$col = 0;foreach ($fields as $field){$objphpexcel->getactivesheet()->setcellvaluebycolumnandrow($col, 1, $field);$col++;}// fetching the table data
$row = 2;foreach($query->result() as $data){$col = 0;foreach ($fields as $field){$objphpexcel->getactivesheet()->setcellvaluebycolumnandrow($col, $row, $data->$field);$col++;}$row++;
}$objphpexcel->setactivesheetindex(0);
$objwriter = iofactory::createwriter($objphpexcel, 'excel5');
//发送标题强制用户下载文件
header('content-type: application/vnd.ms-excel');header('content-disposition: attachment;filename=products_'.date('dmy').'.xls');header('cache-control: max-age=0');$objwriter->save('php://output');
}}?>
复制代码
加入数据库有表名为products,然后访问http://www.yourwebsite.com/table_export/index/products 就可以导出excel文件了。
其它类似信息

推荐信息