import xls with rich text, with phpExcel

1

I am looking for a way to import an xls with rich text to the server database through phpExcel. Some cells in the file contain plain text and other rich text, with several formats in the same cell.

I have already checked the option getStyle() and getCellValue() of PHPexcel can not reads styles from xls but I can not get it.

  • Mark Baker uses getStyle() to extract the style of a specific cell ('B2'). I need to extract the content of all, whether or not they contain rich text. With toArray() I go through all the xls.

  • In the second option, use getCellValue() . This instruction gives me the following message: Fatal error: Uncaught Error: Call to undefined method PHPExcel_Worksheet :: getCellValue ()

How do I import all the contents of the excel while maintaining the styles?

    
asked by Maria 19.07.2016 в 14:06
source

1 answer

1

The answer you link must be out of date since it is from 2012.

toArray () continues to exist and returns an array with the data of the active page.

In the current version of phpExcel the code of the second option would be something like:

$objPHPExcel->getActiveSheet()->getCell('B2')->getValue();

To go through the different cells you can use the different "iterator" (rowiterator and columniterator).

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(TRUE);
$objPHPExcel = $objReader->load("test.xlsx");

$objWorksheet = $objPHPExcel->getActiveSheet();

echo '<table>' . PHP_EOL;
foreach ($objWorksheet->getRowIterator() as $row) {
    echo '<tr>' . PHP_EOL;
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
                                                       //    even if a cell value is not set.
                                                       // By default, only cells that have a value 
                                                       //    set will be iterated.
    foreach ($cellIterator as $cell) {
        echo '<td>' . 
             $cell->getValue() . 
             '</td>' . PHP_EOL;
    }
    echo '</tr>' . PHP_EOL;
}
echo '</table>' . PHP_EOL;

This is the example that appears in: link

On each Cell object you can use the getValue and getStyle methods to get the data you need.

    
answered by 19.07.2016 в 15:55