我正在开发一个项目,我需要将Excel(.xlsx)文件中的数据导入数据库中的表格。
我已经为控制器编写了代码,import.php:
if($this->input->post('send')=='add'):
$config['upload_path'] = 'media/result';
//Only allow this type of extensions
$config['allowed_types'] = 'xlsx|csv';
$this->load->library('upload', $config);
// if any error occurs
if ( ! $this->upload->do_upload('upload_file1'))
{
$error = array('error' => $this->upload->display_errors());
}
//if successfully uploaded the file
else
{
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
//load library phpExcel
$this->load->library("Excel");
//here i used microsoft excel 2007
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
//set to read only
$objReader->setReadDataOnly(true);
//load excel file
$objPHPExcel = $objReader->load('upload/'.$file_name);
$sheetnumber = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $sheet)
{
$s = $sheet->getTitle(); // get the sheet name
$sheet= str_replace(' ', '', $s); // remove the spaces between sheet name
$sheet= strtolower($sheet);
$objWorksheet = $objPHPExcel->getSheetByName($s);
$lastRow = $objPHPExcel->setActiveSheetIndex($sheetnumber)->getHighestRow();
$sheetnumber++;
if($sheet=='student')// if sheet name is student
{
//loop from first data until last data
for($j=2; $j<=$lastRow; $j++)
{
$classement = $objWorksheet->getCellByColumnAndRow(1,$j)->getValue();
$dossard = $objWorksheet->getCellByColumnAndRow(2,$j)->getValue();
$nom = $objWorksheet->getCellByColumnAndRow(3,$j)->getValue();
$sexe = $objWorksheet->getCellByColumnAndRow(4,$j)->getValue();
$cat = $objWorksheet->getCellByColumnAndRow(5,$j)->getValue();
$pays = $objWorksheet->getCellByColumnAndRow(6,$j)->getValue();
$temps = $objWorksheet->getCellByColumnAndRow(7,$j)->getValue();
$tempsreel = $objWorksheet->getCellByColumnAndRow(8,$j)->getValue();
$ecart = $objWorksheet->getCellByColumnAndRow(9,$j)->getValue();
$moyenne = $objWorksheet->getCellByColumnAndRow(10,$j)->getValue();
if($classement != '' || $dossard != ''|| $nom != ''|| $sexe != '' || $cat != '' || $pays != '' || $temps != '' || $tempsreel != '' || $ecart != '' || $moyenne != '')
{
$excel = array(
'Classement'=>$classement,
'dossard'=>$dossard,
'nomprenom'=>$nom,
'sexe'=>$sexe,
'cat'=>$cat,
'pays'=>$pays,
'temps'=>$temps,
'temspreel'=>$tempsreel,
'ecart'=>$ecart,
'moyenne'=>$moyenne
);
$this->db->insert('mt_result',$excel);
$result=($this->db->affected_rows()!= 1)? false:true;
if($result == true)
{
$this->session->set_flashdata('msg', 'Member Details Uploaded Successfully');
redirect('Result/');
}
else
{
$this->session->set_flashdata('msg1', 'Member Details Uploading Failed');
redirect('Result/');
}
}
else
{
$this->session->set_flashdata('msg1', 'Failed To Upload!Contents are not Matched');
redirect('Result/');
}
}// loop ends
}
}
}
endif;
每当我调用此控制器时,我都会收到以下错误:
遇到PHP错误
严重性:错误
消息:未找到类'PHPExcel_IOFactory'
文件名:controllers / Result.php
行号:229
如何解决这个问题?
答案 0 :(得分:1)
尝试使用$this->Excel->createReader('Excel2007')
代替PHPExcel_IOFactory::createReader('Excel2007')