How to detect new line character when reading a xlsx file?

Topics: Developer Forum
Mar 29, 2012 at 12:50 PM

I am kinda new to this library and I am making a simple script that imports data from xlsx file into a db and then display them in a page. I am testing a sample script which iterates through the file and display them in a table. The issue is it doesn't detect the new line character so the result is, all words in the cell are in one line. How do I make it detect a new line charater?

Here's the code:

include 'PHPExcel.php';

$objReader = PHPExcel_IOFactory::createReader('Excel2007');

$objPHPExcel = $objReader->load("test.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
echo '<table>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
  echo '<tr>' . "\n";
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false); // This loops all cells,
                                                     // even if it is not set.
                                                     // By default, only cells
                                                     // that are set will be
                                                     // iterated.
  foreach ($cellIterator as $cell) {
    echo '<td>' . $cell->getValue() . '</td>' . "\n";
  echo '</tr>' . "\n";
echo '</table>' . "\n";

Any help would be appreciated...

Mar 30, 2012 at 1:10 PM

Try this...

echo '<td>' . nl2br( $cell->getValue() ) . '</td>' . "\n";

Mar 30, 2012 at 1:48 PM

It works but is there a way to wrap them with appropriate html tags? nl2br abuses <br> which is not semantic.  Thanks a lot!

Mar 30, 2012 at 2:46 PM

how about str_replace("\n", "<br />\n", $cell->getValue())