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

时间:2011-11-07 21:14:45

标签: php mysql phpexcel

我正在尝试使用php脚本来读取xlsx文件,并将来自单元格的信息传递给MYSQL

这是我的代码,我使用的是PHPExcel 1.7.6和PHP 5.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

这是我需要使用的东西

for ($x = 2; $x < = count($data->sheets[0]["cells"]); $x++) {
    $item_number = $data->sheets[0]["cells"][$x][1];
    $qty_sold = $data->sheets[0]["cells"][$x][2];
    $cost_home = $data->sheets[0]["cells"][$x][3];

哪个适用于phpexcelreader,但我不知道哪些函数会对phpExcel执行相同的操作

//here is where I would pass those values into MYSQL

$sql = "INSERT INTO sales_report (`item_number`,`qty_sold`, `cost_home`) 
        VALUES ('$item_number',$qty_sold,'$cost_home')";
         echo $sql."\n";
    mysql_query($sql);
} 
?>

我完全失去了如何将电子表格中的数据导入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

但我似乎无法将其插入我的表格中。我也尝试过使用implode函数,但没有任何反应。

1 个答案:

答案 0 :(得分:2)

最简单的方法是动态地将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);
    }
  }