我想为所有列表生成excel表以及搜索列表,所以,我在datatables中搜索,这是我的模型
public function getAllAdmissions($pdata,$getcount=null)
{
if(isset($pdata['search_text_1'])!="")
{
$this->db->like($search_1[$pdata['search_on_1']], $pdata['search_text_1']);
}
if($getcount)
{
return $this->db->select('p.person_id')->from('people as p')->join('people_details as pd','pd.people_id=p.person_id','LEFT')->get()->num_rows();
}
else
{
$this->db->select('p.status as status,p.person_id,p.first_name,p.last_name,p.sur_name,pd.father_name as father_name,p.email,p.mobile_number,p.created_on as enroll_date')->from('people as p')->join('people_details as pd','pd.people_id=p.person_id','LEFT');
}
}
$alladmissions = $query->get()->result_array();
//echo $this->db->last_query();die();
foreach($alladmissions as $key=>$values)
{
$alladmissions[$key]['sno'] = $generatesno++;
$alladmissions[$key]['enroll_date'] = date("d M Y",strtotime($alladmissions[$key]['enroll_date']));
$alladmissions[$key]['admission_date'] = date("d M Y",strtotime($alladmissions[$key]['admission_date']));
}
return $alladmissions;
这是我的控制器
public function generateexcel()
{
require_once("PHPExcel/Classes/PHPExcel.php");
$objPHPExcel = new PHPExcel();
$objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
$cols = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$c = 0;
$objPHPExcel->getActiveSheet()->setCellValue($cols[$c++]."1", 'S.No');
$objPHPExcel->getActiveSheet()->setCellValue($cols[$c++]."1", 'Date');
$k = $c++;
$objRichText = new PHPExcel_RichText();
$objRichText->createText("Students");
$objPHPExcel->getActiveSheet()->getCell($cols[$k]."1")->setValue($objRichText);
$objPHPExcel->getActiveSheet()->getStyle($cols[$k]."1")->getAlignment()->setWrapText(true);
$k = $c++;
$objRichText = new PHPExcel_RichText();
$objRichText->createText("Student Details");
$objPHPExcel->getActiveSheet()->getCell($cols[$k]."1")->setValue($objRichText);
$objPHPExcel->getActiveSheet()->getStyle($cols[$k]."1")->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->setCellValue($cols[$c++]."1", 'Amount');
$students = $this->student->getStudents();
$objPHPExcel->getActiveSheet()->fromArray($students);
$filename = "students.xls";
$objPHPExcel->getActiveSheet()->setTitle('Students');
$objPHPExcel->setActiveSheetIndex(0);
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"".$filename."\";");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
任何人都可以帮助我如何传递此cotroller的参数以及如何生成excel
答案 0 :(得分:0)
添加导出按钮:
<button type="button" class="btn btn-warning btn-sm" onClick="tableToExcel('testTable', 'W3C Example Table')" title="Export to Excel">Export</button></div><br/>
向表中添加id:
<table class="table table-bordered table-hover " border="1" id="testTable" >
在正文下方添加脚本:
<script type="text/javascript">
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()</script>