我在单元格L2中有一个带有“BC/B22-”的Excel2007xlsx文档。它是粗体的,红色的。使用PHPExcel (v1.8.0,2014-03-02),我想检测单元格是否粗体,但我只能得到“BC/b22-”值,而没有样式信息。
这是我的密码:
$inputFileType = PHPExcel_IOFactory::identify($sFilepath);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load($sFilepath);
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$cell = $sheet->getCellByColumnAndRow(11,2);
$v = $cell->getValue();
var_dump($v);
// Echoes: string(8) "BC/B22--"
$rte = $cell->getValue()->getRichTextElements();
// Error: "Call to a member function getRichTextElements() on a non-object"
echo ($cell->getValue() instanceof PHPExcel_RichText) ?
"instance" : "no instance";
// Echoes "no instance"
发布于 2015-03-04 00:11:07
这完全取决于单元格是否包含简单的文本内容并将粗体设置为单元格样式,或者单元格内容是否是富文本对象。
如果$cell->getValue()
返回一个字符串,则需要测试单元格的样式:
$isBold = $cell->getStyle()->getFont()->getBold();
否则,如果$cell->getValue()
返回一个Rich对象,那么您需要遍历该Rich对象,以查看它的任何部分是否为粗体:
$isBold = false;
$elements = $cell->getValue()->getRichTextElements();
foreach ($elements as $element) {
if ($element instanceof PHPExcel_RichText_Run) {
$isBold |= $element->getFont()->getBold();
}
}
https://stackoverflow.com/questions/28845117
复制相似问题