在 codeigniter 中读取 .xlsx 和 .xls 数据

Read .xlsx and .xls data in codeigniter

我想在 codeigniter 中读取 .xlsx 或 .xls 文件的数据。我已经阅读了与它相关的其他问题,但没有任何效果。我用过 phpexcel, reader 但没有运气。在我的项目中,我提供了上传 excel 文件的选项,然后我想读取数据并将其插入数据库。

现在我正在使用 php我写的 Excel 库:

    $this->load->library('excel');
    $reader= PHPExcel_IOFactory::createReader('Excel2007');
    $reader->setReadDataOnly(true);
    $path=(FCPATH.'uploads/productfile/'.$_FILES['upload_file']['name']);
    $excel=$reader->load($path);
    $sheet=$excel->setActiveSheetIndex(0);
    for($i=0;$i<=1000;$i++)
    {
      $col1= $sheet->getCellByColumnAndRow(0,$i)->getValue();
      $col2= $sheet->getCellByColumnAndRow(1,$i)->getValue();
      $col3= $sheet->getCellByColumnAndRow(2,$i)->getValue();
      var_dump($col1);
    }

但它显示:

Uncaught exception 'PHPExcel_Exception' with message 'You tried to set a sheet active by the out of bounds index: 0. The actual number of sheets is 0 Please give me some example code.

错误 请给我一些示例代码:

我想这个错误是由于 PHPexcel reader 功能的错误初始化引起的。 我通常会做这样的事情来从 excel:

中获取数据
require_once '../Classes/PHPExcel/IOFactory.php';
$filename = '../uploads/product/abc.xls';
$objPHPExcel = PHPExcel_IOFactory::load($filename);

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    $total_rows = $highestRow-1;

    for ($row = 2; $row <= $highestRow; ++ $row) {
    //id
        $cell = $worksheet->getCellByColumnAndRow(0,$row);
        $id = $cell->getValue();
        if(is_null($id)){$id = '#';}
    }

试试这个:

 $sheet = $excel->getActiveSheet()->toArray(null,true,true,true);

这将 return 您当前活动的数组 sheet.Hope 这有帮助。

感谢大家的建议: 我得到了解决方案:

$file_data = $this->upload->data();
$file_path =  './uploads/productfile/'.$file_data['file_name'];
include 'Classes/PHPExcel/IOFactory.php';
$inputFileName = $file_path; 
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet
for($i=2;$i<=$arrayCount;$i++)
{                   
    'product'=$allDataInSheet[$i]["C"],
    'brand'=$allDataInSheet[$i]["I"],
    'standard'=$allDataInSheet[$i]["J"],
}

PHPExcel_IOFactory 的这个扩展在 ™ 商标、版权、学位等符号方面存在问题

您无法读取包含此类特殊字符的特定块。

这段代码对我有用,

        $this->load->library('excel');
        $reader= PHPExcel_IOFactory::createReader('Excel2007');
        $reader->setReadDataOnly(true);
        $path= "./media/customer_exports/data-1558003506.xlsx";
        $excel=$reader->load($path);

        $sheet = $excel->getActiveSheet()->toArray(null,true,true,true);
        $arrayCount = count($sheet); 
        for($i=2;$i<=$arrayCount;$i++)
        {                   
            echo $sheet[$i]["A"].$sheet[$i]["B"].$sheet[$i]["C"];
        }