PHPExcel-读取单元格并将其传递到MYSQL

PHPExcel-读取单元格并将其传递到MYSQL,php,mysql,phpexcel,Php,Mysql,Phpexcel,我试图使用php脚本读取xlsx文件,并将单元格中的信息传递到MYSQL中 这是我的代码,我使用的是PHPExcel版本1.7.6和php5.3.5 require_once 'PHPExcel.php'; $inputFileType = 'Excel2007'; $inputFileName = $upload_path . $filename; /** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter

我试图使用php脚本读取xlsx文件,并将单元格中的信息传递到MYSQL中

这是我的代码,我使用的是PHPExcel版本1.7.6和php5.3.5

require_once 'PHPExcel.php';
$inputFileType = 'Excel2007';
$inputFileName = $upload_path . $filename;

/**  Define a Read Filter class implementing PHPExcel_Reader_IReadFilter  */ 
class chunkReadFilter implements PHPExcel_Reader_IReadFilter 
{ 
    private $_startRow = 0; 
    private $_endRow   = 0; 

    /**  Set the list of rows that we want to read  */ 
    public function setRows($startRow, $chunkSize) { 
        $this->_startRow = $startRow; 
        $this->_endRow   = $startRow + $chunkSize; 
    } 

    public function readCell($column, $row, $worksheetName = '') { 
        //  Only read the heading row, and the configured rows 
        if (($row == 1) ||
            ($row >= $this->_startRow && $row < $this->_endRow)) { 
            return true; 
        } 
        return false; 
    } 
} 


/**  Create a new Reader of the type defined in $inputFileType  **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 


/**  Define how many rows we want to read for each "chunk"  **/ 
$chunkSize = 2048; 
/**  Create a new Instance of our Read Filter  **/ 
$chunkFilter = new chunkReadFilter(); 

/**  Tell the Reader that we want to use the Read Filter  **/ 
$objReader->setReadFilter($chunkFilter); 

/**  Loop to read our worksheet in "chunk size" blocks  **/ 
for ($startRow = 2; $startRow <= 65536; $startRow += $chunkSize) { 
    /**  Tell the Read Filter which rows we want this iteration  **/ 
    $chunkFilter->setRows($startRow,$chunkSize); 
    /**  Load only the rows that match our filter  **/ 
    $objPHPExcel = $objReader->load($inputFileName); 

 //    Need to pass the cell values into the variables
我完全不知道如何将电子表格中的数据导入mysql

编辑:

我通过使用以下数组成功地打印了数据

foreach ($objWorksheet->getRowIterator() as $row) {
    $j = 1;

    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
         $data->sheets[0]['cells'][$i][$j] = $cell->getValue();

        $j++;
    } // end cell getter

    $i++;

} // end row getter

但我似乎无法将它插入我的表中。我也尝试过使用内爆函数,但没有任何效果。

最简单的方法是动态地将xlsx转换为csv文件,而不是使用普通的csv解析。只需实例化CSVWriter并保存到临时位置(我可以在明天提供示例代码)

示例代码:

  $objReader = PHPExcel_IOFactory::load ( $file_path );
  $writer = PHPExcel_IOFactory::createWriter ( $objReader, 'CSV' );
  $writer->save ( $csv_path );      
  if (($handle = fopen ( $csv_path, "r" )) !== false) {

    while ( ($data = fgetcsv ( $handle)) !== false ) {
        print_r($data);
    }
  }

谢谢darhazer,如果你能帮我做这件事的话,我会非常生气的。我花了一个星期的时间寻找一种方法来实现这一点,但什么都没有,哈哈。再次感谢Hanks的代码,这确实很有效。但我希望能够(在将来)使用excel2007的一些功能。把它转换成CSV会不会让我丢失公式之类的东西?我想,一旦我知道如何直接插入数据,我就应该能够在单元格中提取公式。备选方案:已经问过了,看这个:我在问问题之前已经试过了。它对我不起作用。@ImadMoqaddem关于行
$row[$col]=mysql\u real\u escape\u字符串($objWorksheet->getCellByColumnRow($col,$row)->getValue()),我一直收到“警告:不能将标量值用作数组”我猜您使用$col作为字母(“a”、“B”和…),不是吗?你必须使用它们的整数等价物:A->0,B->1,…,AA->26,AB->27。。。
foreach ($objWorksheet->getRowIterator() as $row) {
    $j = 1;

    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
         $data->sheets[0]['cells'][$i][$j] = $cell->getValue();

        $j++;
    } // end cell getter

    $i++;

} // end row getter
  $objReader = PHPExcel_IOFactory::load ( $file_path );
  $writer = PHPExcel_IOFactory::createWriter ( $objReader, 'CSV' );
  $writer->save ( $csv_path );      
  if (($handle = fopen ( $csv_path, "r" )) !== false) {

    while ( ($data = fgetcsv ( $handle)) !== false ) {
        print_r($data);
    }
  }