Quantcast
Channel: PHPExcel Forum Rss Feed
Viewing all articles
Browse latest Browse all 2707

New Post: How to skip first 3-4 rows while reading an Excel file.

$
0
0
Just wanted to add solution for PHPExcel version 1.8.0. Just pass row offset to getRowIterator method.
/**
 * Get row iterator
 *
 * @param  integer   $startRow    The row number at which to start iterating
 * @return PHPExcel_Worksheet_RowIterator
 */
 public function getRowIterator($startRow = 1) {
   return new PHPExcel_Worksheet_RowIterator($this,$startRow);
 }
Code:
try
{
  $reader = new PHPExcel_Reader_Excel2007();

  // http://stackoverflow.com/questions/13626678/phpexcel-how-to-
  // check-whether-a-xls-file-is-valid-or-not
  //
  if ($reader->canRead("file.xlsx")) !== true )
  {
    echo "Invalid xlsx file.";
    exit();
  }

  /*
         If you're only interested in the cell values in a workbook, 
         but don't need any of the cell formatting information, 
         then you can set the reader to read only the data values 
         and any formulas from each cell using the setReadDataOnly() method.

         It is important to note that Workbooks (and PHPExcel) store dates and 
         times as simple numeric values: they can only be distinguished from 
         other numeric values by the format mask that is applied to that cell. 
         When setting read data only to true, PHPExcel doesn't read the cell 
         format masks, so it is not possible to differentiate between dates/times 
         and numbers.
   */
  $reader->setReadDataOnly(true);

  $excel = $reader->load("file.xlsx");
}
catch(PHPExcel_Reader_Exception $e)
{
  echo "Error loading file" . $e->getMessage();
  exit();
}
catch(Exception $e)
{
  echo "Exception, error loading file 2:" . $e->getMessage();
  exit();
}

// set worksheet
//
$worksheet = $excel->setActiveSheetIndex(0);

// skip first "header" row by adding argument 2 to getRowIterator
//
foreach($worksheet->getRowIterator(2) as $row)
{
  $one   = $worksheet->getCellByColumnAndRow(0, $row->getRowIndex())->getValue();
  $two = $worksheet->getCellByColumnAndRow(1, $row->getRowIndex())->getValue();
  ...

Viewing all articles
Browse latest Browse all 2707

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>