PHPexcel cannot reads styles from xls

空扰寡人 提交于 2019-12-17 21:32:43

问题


I cannot read styles from xls by PHPexcel. I browsing for a solution a lot but everywhere writes solutions for write xls but I like it to read. I like especially strikethrough but it cannot reads neither this nor any other style informations. My code is the following, this reads all data correctly but nothing more. Im sure to make text styled in the xls.

require_once 'PHPExcel.php';
$filepath = "path/to/your/xls/file.xls";
$inputFileType = PHPExcel_IOFactory::identify($filepath);

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($filepath);

$total_sheets = $objPHPExcel->getSheetCount();
$allSheetName = $objPHPExcel->getSheetNames(); 
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0) ;
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

$headingsArray = $objWorksheet->rangeToArray('A1:' . $highestColumn . '1', null, true, true, true);
$headingsArray = $headingsArray[1];

print_r( $objWorksheet->toArray() );

Does anybody have an idea?

thx


回答1:


PHPExcel is perfectly capable of reading styles from xls files: have you actually tried getting the style details from a cell, because your code here doesn't show any tests to check that styles have been read?

$objWorksheet->toArray()

reads the content from cells as simple PHP scalar values.

To determin the styling of a cell, you need to read that cell's style details.

e.g.

$cellHasStrikeThrough = $objPHPExcel->getActiveSheet()->getStyle('B2')
    ->getFont()->getStrikethrough();

The API documentation shows all the details for reading style information such as colours, fills, fonts, borders, etc

EDIT

A cell's contain may also be rich text, with different parts of the content having different styles. If you retrieve a rich text value from a cell using

$objPHPExcel->getActiveSheet()->getCellValue('B2');

a richtext object will be returned. It is then possible to iterate through each block (or run) of the richtext object checking the style for that block



来源:https://stackoverflow.com/questions/12979394/phpexcel-cannot-reads-styles-from-xls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!