I have written the code to export the data into an excel sheet, but the PHPExcel libray is not loaded into the code.It shows some error while clicking the button"Export".
错误是: enter image description here
public function Arrear_Print(){
$data['page'] = 'export-excel';
$data['title'] = 'Export Excel data | TechArise';
$data['startdate'] =$this->input->post('SDate');
$data['enddate'] = $this->input->post('EDate');
$this->load->model('Export');
$data['PName'] = $this->Export->mobileList();
// load view file for output
//$this->load->view('header');
$this->load->view('TipUp_Loan\Arrear_Print', $data);
// $this->load->view('footer');
}
public function createXLS() {
// create file name
$fileName = 'mobile-'.time().'.xlsx';
// load excel library
$this->load->library('excel');
$mobiledata = $this->model->Export->mobileList();
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// set Header
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'AcNo');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Mode');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Party Name');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'Loan Date');
$objPHPExcel->getActiveSheet()->SetCellValue('E1', 'Loan Amt');
$objPHPExcel->getActiveSheet()->SetCellValue('F1', 'Bal.Amt');
$objPHPExcel->getActiveSheet()->SetCellValue('G1', 'Coll.Amt');
$objPHPExcel->getActiveSheet()->SetCellValue('H1', 'Rec.Amt');
$objPHPExcel->getActiveSheet()->SetCellValue('I1', 'Phone.No');
// set Row
$rowCount = 2;
foreach ($mobiledata as $element)
{
$objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $element['TAcNo']);
$objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $element['TMode']);
$objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $element['TName']);
$objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $element['TDate']);
$objPHPExcel->getActiveSheet()->SetCellValue('E' . $rowCount, $element['TAmt']);
$objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, $element['total_sum']);
$objPHPExcel->getActiveSheet()->SetCellValue('G' . $rowCount, $element['RLAmt']);
$objPHPExcel->getActiveSheet()->SetCellValue('H' . $rowCount, $element['']);
$objPHPExcel->getActiveSheet()->SetCellValue('I' . $rowCount, $element['Phone']);
$rowCount++;
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($fileName);
// download file
header("Content-Type: application/vnd.ms-excel");
redirect(site_url().$fileName);
}
这是一个控制器代码:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Export extends CI_Model {
// get mobiles list
public function mobileList() {
$data['startdate'] =$this->input->post('SDate');
$data['enddate'] = $this->input->post('EDate');
$PName = $this->input->post('TName');
$SDate = $this->input->post('SDate');
$EDate = $this->input->post('EDate');
if($PName = $this->input->post('TName')){
$query = $this->db->query("SELECT `TAcNo`,`TMode`,`TDate`,`TAmt`,`TName`, COALESCE(SUM(`RLAmt`) ,0) AS `RLAmt`,`Phone` FROM `tipup_payment` LEFT OUTER JOIN `tipup_receipt` ON `tipup_receipt`.`TRAcNo` = `tipup_payment`.`TAcNo` AND `TDate` >= '$SDate' AND `TDate` <= '$EDate' LEFT OUTER JOIN `parmaster` ON `parmaster`.`PName` = `tipup_payment`.`TName` WHERE TName = '$PName' GROUP BY `TAcNo`ORDER BY `TAcNo` ASC");
return $query->result_array();
//$data['PName']=$query->result_array();
//$this->load->view('TipUp_Loan\Arrear_Print',$data,FALSE);
}
else {
$query = $this->db->query("SELECT `TAcNo`,`TMode`,`TDate`,`TAmt`,`TName`, COALESCE(SUM(`RLAmt`) ,0) AS `RLAmt`,`Phone` FROM `tipup_payment` LEFT OUTER JOIN `tipup_receipt` ON `tipup_receipt`.`TRAcNo` = `tipup_payment`.`TAcNo` AND `TDate` >= '$SDate' AND `TDate` <= '$EDate' LEFT OUTER JOIN `parmaster` ON `parmaster`.`PName` = `tipup_payment`.`TName` GROUP BY `TAcNo` ORDER BY `TAcNo` ASC");
return $query->result_array();
//$data['PName']=$query->result_array();
//$this->load->view('TipUp_Loan\Arrear_Print',$data,FALSE);
}
}
}
?>
这是型号代码:
enter image description here 这是输入视图页面。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 enter image description here
这是获取的数据,并在单击“导出”按钮时显示在视图页面中。错误显示我从未上传图片。
我已经在codeiginter网站上下载了phpexcel库文件。
这是链接。...enter link description here
请帮助我解决这个问题。
答案 0 :(得分:1)
有很多方法可以解决您的问题。一切都取决于您如何放置(和放置)PHPExcel文件,以及从错误消息中调用该文件,因为我们可以假设PHP在与实际位置不同的位置寻找PHPExcel文件。
YMMV,但这是我的安装。
要输出XLS文件,请从我的模型中获取数据数组(在模型中始终使用return $query->result_array();
而不是return $query->result();
,除非您以后想将返回的对象转换为数组),然后执行以下操作:< / p>
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle($report);
$titles = array_keys($data[0]);
$this->excel->getActiveSheet()->fromArray($titles, NULL, 'A1');
$this->excel->getActiveSheet()->fromArray($data, NULL, 'A2');
$filename='output.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
请注意,我正在呼叫$this->excel
,该电话来自此代码所在的application/libraries/Excel.php
:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/phpexcel/PHPExcel.php";
class Excel extends PHPExcel {
public function __construct() {
parent::__construct();
}
}
?>
从require
上的application/third_party/phpexcel/PHPExcel.php
可以看到核心PHPExcel文件(由于路由区分大小写,因此请始终注意大小写)。在PHPExcel.php所在的同一级别上,我已将目录包含核心PHPExcel文件-包括autoloader.php
-命名为PHPExcel。
这非常简单明了,并且对于使用CI 3.1.x的多个项目来说,对我来说效果很好