PHPEXCEL 单元格包含richtext的读取问题

ello.

When i use some style in cell like color, font size,… then an error returned among the cell value.

I use PHPExcel in this way:

require_once ‘include/excel/PHPExcel/IOFactory.php’;
$objReader = PHPExcel_IOFactory::createReader(‘Excel5’);//use excel5
$objPHPExcel = $objReader->load($_FILES[‘excelfile’][‘tmp_name’]);
$FAValue = ($objPHPExcel->getActiveSheet()->getCell(‘A’.$i)->getvalue());

The returened value to $FAValue will be:

__PHP_Incomplete_Class::__set_state(array(
‘__PHP_Incomplete_Class_Name’ => ‘PHPExcel_RichText’,
‘_richTextElements’ =>
array (
0 =>
__PHP_Incomplete_Class::__set_state(array(
‘__PHP_Incomplete_Class_Name’ => ‘PHPExcel_RichText_TextElement’,
‘_text’ => ‘dsfsdf ‘,
)),
1 =>
__PHP_Incomplete_Class::__set_state(array(
‘__PHP_Incomplete_Class_Name’ => ‘PHPExcel_RichText_Run’,
‘_font’ =>
__PHP_Incomplete_Class::__set_state(array(
‘__PHP_Incomplete_Class_Name’ => ‘PHPExcel_Style_Font’,
‘_name’ => ‘Arial’,
‘_size’ => 10,
‘_bold’ => true,
‘_italic’ => false,
‘_superScript’ => false,
‘_subScript’ => false,
‘_underline’ => ‘none’,
‘_strikethrough’ => false,
‘_color’ =>
__PHP_Incomplete_Class::__set_state(array(
‘__PHP_Incomplete_Class_Name’ => ‘PHPExcel_Style_Color’,
‘_argb’ => ‘FF000000’,
‘_isSupervisor’ => false,
‘_parent’ => NULL,
‘_parentPropertyName’ => NULL,
)),
‘_parentPropertyName’ => NULL,
‘_isSupervisor’ => false,
‘_parent’ => NULL,
‘colorIndex’ => 32767,
)),
‘_text’ => ‘sdfsdf’,

Thanks in Advance

 

Answer:

This isn’t quite the same of styling of a cell…. it’s actually rich text styling of the cell content: test to see if $FAValue is an instance of a PHPExcel_RichText object, and if so then use the getPlainText() method (or simply cast to string, as PHPExcel_RichText has a magic __toString() method).

 

参考:
http://phpexcel.codeplex.com/discussions/353348
http://blog.csdn.net/longxuu/article/details/7480311
http://phpexcel.codeplex.com/discussions/34513 这里有很多答案:)(

 

 

我用以下方法解决了,仅限于读取。

If you need just the plain text then probably the right solution for you is to use setReadDataOnly(true) for the reader.

Example:

$reader = new PHPExcel_Reader_Excel2007();
$reader->setReadDataOnly(true);

$PHPExcel = $reader->load('test.xlsx');

$ws = $PHPExcel->getActiveSheet();

// A1 contains Rich-Text
echo $ws->getCell('A1')->getValue();

It is important that you use the latest source found under ‘Source Code’ as the behavior was changed after the release of PHPExcel 1.6.3. Until recently you would get Rich-Text even with setReadDataOnly(true).
http://www.codeplex.com/PHPExcel/Thread/View.aspx?ThreadId=34770


发表评论 0

Your email address will not be published. Required fields are marked *