Formated cell value to HTML

Topics: Developer Forum
Dec 4, 2009 at 9:57 AM

Hi. 

I'm trying to export xlsx spreadsheet to mysql database. Every cell is a separated records in db. I need to display cell text on my web-page exactly in a same style as it was in spreadsheet, but I can't get it from cell. PHPExcel_Cell allow to get only value, but how can I gen formated and styled text ?

For example:

text in cell: This is a text in cell

needed on webpage: This is a text in cell

 

Dec 4, 2009 at 12:42 PM

Solution founded. 

 

$value = $cell->getValue();
   $str = '';
   if (is_object($value)) {
   $elements = $value->getRichTextElements();
   foreach ($elements as $item) {
   $str .= '<font style="color: #'.$item->getFont()->getColor()->getRGB().'; font-weight: '.($item->getFont()->getColor() ? 'bold' : 'normal').'">'.$item->getText().'</font>';
   }
  
   }

 

 

$value = $cell->getValue();

  	$str = '';

  	if (is_object($value)) {

  		$elements = $value->getRichTextElements();

  		foreach ($elements as $item) {

  			$str .= '<font style="color: #'.$item->getFont()->getColor()->getRGB().'; font-weight: '.($item->getFont()->getColor() ? 'bold' : 'normal').'">'.$item->getText().'</font>';

  		}	

  	}

 

 

 

 

Jan 26, 2010 at 2:33 PM

I try it .but  nothing happened. i dont know how to get object.

is_object($value)
in my case, $value always is string.

can i get  a more complete example?

Developer
Jan 27, 2010 at 9:16 AM
cicq wrote:

I try it .but  nothing happened. i dont know how to get object.

 

is_object($value)
in my case, $value always is string.

 

can i get  a more complete example?

In that case the cell value is not a Rich-Text object.

For a start, try PHPExcel's own HTML export function:

 

$excel = PHPExcel_IOFactory::load('read.xls');

$writer = PHPExcel_IOFactory::createWriter($excel, 'HTML');
$writer->writeAllSheets();
$writer->save('write.html');


 

Jan 28, 2010 at 1:40 PM

 Now  I see.  cell text   has  different  style with cell.

I  got them works.  thanks.

I wonder  how  i  can control the detail with HTML export ?

I need fetch style then show it as HTML  immediately,  I  learned , but  that is not enough.

when i export xls, I founded  the style in the HTML not as same as the style in xls file.

for example ,   cell in HTML's width narrower  than cell in the xls file.

 now i just know something like this :

 

$objWorksheet->getStyle($Columnnum.$Rownum)->getFont()->getColor()->getARGB();
$objPHPExcel->getActiveSheet()->getStyle($Columnnum.$Rownum)->getFill()->getStartColor()->getARGB()

 

is there anything more? Is there a better way out? maybe some useful links ?  i cant find more in the document .

  thanks again!

 

 

 

Apr 12, 2011 at 3:40 PM

This was tons helpful -- there is little documentation for just reading rich text from an input file -- but I had to edit the code a bit...

 

$value = $cell->getValue();

if (is_object($value)) {

	$elements = $value->getRichTextElements();
  
	foreach ($elements as $item) {
		
		if(is_object($item->getFont())) {
			
			if($item->getFont()->getItalic()) {
				
				 $value = str_replace($item->getText(), "<em>".$item->getText()."</em>", $value);
				
			}
			
		}

	}

}

In particular...

if(is_object($item->getFont()))

 

...makes sure that rich text does exist per item, and...

if($item->getFont()->getItalic()) {
				
	$value = str_replace($item->getText(), "<em>".$item->getText()."</em>", $value);
				
}

...checks if there are italics (also works for bold, etc.), then italicizes that part in the value variable.