|
controller:Table_export
<?php
class Table_export extends CI_Controller {
function __construct(){
parent::__construct();
// Here you should add some sort of user validation
// to prevent strangers from pulling your table data
}
function index(){
if($this->common->get_uid() && $this->common->get_permission()){
//$this->load->database();
//$query = $this->db->query("select * from `$table_name` WHERE del= 1");
$this->load->model('dataexport_model');
$query = $this->dataexport_model->getmembers();
// $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("UserData")->setDescription("none");
//创建人
$objPHPExcel->getProperties()->setCreator("xxxx");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', iconv( 'utf-8','GBK//IGNORE', '中文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="xxx_'.date('dMy').'.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
}else{
redirect('admin/index');
}
}
//活动参与下载
function activity_index(){
if($this->common->get_uid() && $this->common->get_permission()){
$this->load->model('dataexport_model');
$query = $this->dataexport_model->getactivity();
if(!$query)
return false;
// Starting the PHPExcel library
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
$objPHPExcel = new PHPExcel();
//设置题目
$objPHPExcel->getProperties()->setTitle("UserData")->setDescription("none");
//创建人
$objPHPExcel->getProperties()->setCreator("xxxx");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', iconv( 'utf-8','GBK//IGNORE', '中文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="xxxx_'.'activity'.date('dMy').'.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
}else{
redirect('admin/index');
}
}
//
}
?>
model:
<?php
class Dataexport_model extends CI_Model {
public function __construct() {
parent :: __construct();
}
public function getmembers(){
$this->db->select('uid,nickname,telphone,email,gender,year');
$query = $this->db->get('membersview');
return $query;
}
//7月14日加的
public function getactivity(){
$this->db->select('apply_actid,apply_name,apply_gender,apply_age,apply_telphone,apply_qq');
$query = $this->db->get('hk_applyactive');
return $query;
}
}
view
<div class="row">
<a href="<?php echo base_url('table_export/index');?>" class="btn btn-primary btn-lg active" role="button">下载</a>
</div>
|
|