您好我发现下面提到的行
的PHPExcel_IOFactory未找到错误$objReader= PHPExcel_IOFactory::createReader('Excel2007');
//控制器
function ExcelDataAdd()
{
$configUpload['upload_path'] ='./uploads/';
$configUpload['allowed_types'] = 'xls|xlsx|csv';
$configUpload['max_size'] = '5000';
$this->load->library('upload', $configUpload);
$this->upload->do_upload('userfile');
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$extension=$upload_data['file_ext'];
$objReader= PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel=$objReader->load('./uploads/'.$file_name);
$totalrows=$objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
for($i=2;$i<=$totalrows;$i++)
{
$FirstName= $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
$LastName= $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
$this->Excel_export_model->Add_stud($data_stud);
}
unlink('./uploads/'.$file_name);
redirect("Welcome");
}
请告诉我哪里出错了
答案 0 :(得分:1)
我找到了一个解决方案,我没有在功能中加载模型。下面提到的功能正常工作
function ExcelDataAdd()
{
$configUpload['upload_path'] ='./uploads/';
$configUpload['allowed_types'] = 'xls|xlsx|csv';
$configUpload['max_size'] = '5000';
$this->load->library('upload', $configUpload);
$this->upload->do_upload('userfile');
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$extension=$upload_data['file_ext'];
$objReader= PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel=$objReader->load('./uploads/'.$file_name);
$totalrows=$objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
for($i=2;$i<=$totalrows;$i++)
{
$FirstName= $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
$LastName= $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
$data_stud=array('optra_id'=>$optra_id, 'student_fname'=>$student_fname);print_r($data_stud);
$this->load->model("Excel_export_model");
$this->Excel_export_model->Add_stud($data_stud);
}
unlink('./uploads/'.$file_name);
redirect("Welcome");
}
答案 1 :(得分:0)
试试这个
$this->load->library('PHPExcel');
$objPHPExcel = PHPExcel_IOFactory::load($_FILES['file']['tmp_name']);
$sheet1 = $objPHPExcel->getSheet(0)->toArray(null,true,true,true);
echo "<pre>";print_r($sheet1);die;
其中file
是<input type="file" name="file">
请从此链接下载PHPExcel并粘贴到application/library/
文件夹