How to get the font name of the excel cell - php

I am iterating all cells of the excel sheet and printing out the values. Same time i also want to print the font name they are using. Is there any function to get the specific cell font name in phpexcel ? Thanks.
below is the code snippet
include('lib/phpexcel/Classes/PHPExcel/IOFactory.php');
//Use whatever path to an Excel file you need.
$inputFileName = 'text.xlsx';
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' .
$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
foreach ($sheet->getRowIterator() as $row) {
echo '<tr>' . "\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
// Is there any similar $cell->getFont() function ?? which will echo"time new roman"
echo '<td>' .$cell->getValue(). '</td>' . "\n";
}
echo '</tr>' . "\n";
}
?>

The font is an aspect of the style of a cell; so you need to get the style details for the cell, and read the font information from that:
$cell->getStyle()
->getFont()
->getName();
Note that you can also get font size, italic, bold, underlining, super/subscript, strikethru, and the font colour in a similar manner.... the font object holds more information than simply the font name.

Related

phpexcel not hiding column

<?php
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
$inputFileName = './source/marks.xls'; //using this in the meantime. Will replace later with an import feature
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
echo '<hr />';
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setVisible(false);
echo '<table>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
echo '<tr>' . "\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // This loops all cells,
foreach ($cellIterator as $cell) {
echo '<td>' . $cell->getValue() . '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
?>
Please am working with this code for a school work. everything works well except the
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setVisible(false);
which is suppose to hide column D.
Function of the code is to read the individual marks from an excel file and display it in html view. eg.
My aim is to hide the Index no column from displaying in the html view. "removeColumn('A')" works but i am doing a search with the values in column A. So just want to hide that column.

Can't read old file.xls excel file with PHPExcel

My code works perfect with file.xlsx but I can't make it work with old file.xls. I tried every code I found and nothing works... I tried change the version of Excel2007to 2003,2004 but nothing... Don't know what else to try.
Here is my current code (working with file.xlsx but I need to work with file.xls too).
<?php
function load_table(){
require_once('Classes/PHPExcel.php');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load("SampleData.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
echo '<table class="table">' . "\n";
for ($row = 1; $row <= $highestRow; ++$row) {
echo '<tr>' . "\n";
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
echo '<td>';
$first = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
if($first[0] == '='){
echo $objWorksheet->getCellByColumnAndRow($col, $row)->getCalculatedValue();
}
else
echo $first;
echo '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
}
?>
The Reader for xls files is the Excel5 Reader.
But you really should simply use the IOFactory's load() method, and let PHPExcel identify the filetype for itself and select the correct reader, because a lot of files (particularly those downloaded from the internet) that have an .xls extension aren't really BIFF format files.... PHPExcel can identify and load these correctly when using the load() method, because it can pick the correct Reader for itself.
But when you specify a Reader such as Excel2007 or Excel5 yourself, you're telling PHPExcel which Reader it must use, even though it may not be the correct Reader for the actual file.
Document link, which lists all of the different Readers by name

Cant read and manipulate excel data

I'm trying to read an excel.xlsx file. But I just can't, I'm reading about an API called PHPExcel but I can't make it work. This is what I tried:
<?php
// get access to the class
require_once 'Classes/PHPExcel.php';
include 'Classes/IOFactory.php';
$inputFileName = 'teste.xlsx';
// Read your Excel workbook
try{
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
}
catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL, TRUE, FALSE);
echo $rowData[$row];
}
?>
at the end of the for loop, I tried to read or echo the data, but I got errors like: Undefined offset.

PHPExcel library splits comma separated values into new columns

I'm trying to read a Excel spreadsheet with the PHPExcel library (1.7.9) and when the value of the field is comma separated the PHPExcel library puts de comma separated values into new colums instead of the same column.
Is this normal? Can I fix it?
this is the code of reader
<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$inputFileName = 'prueba.xls';
/** Identify the type of $inputFileName **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Load $inputFileName to a PHPExcel Object **/
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
echo '<table>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
echo '<tr>' . "\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // This loops all cells,
// even if it is not set.
// By default, only cells
// that are set will be
// iterated.
foreach ($cellIterator as $cell) {
echo '<td>' . $cell->getValue() . '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
?>
and this is a screenshot of excel spreadsheet
Thanks

How to read multiple worksheet from a single excel file in php?

I have an excel sheet having three worksheets, I am having trouble in fetching records from second worksheet.
All three worksheet is having different kind of records and with different fields, I try to google it but couldn't find a solution.
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
$file = '/media/sf_E_DRIVE/om/newData.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file);
$objWorksheet = $objPHPExcel->getActiveSheet();
$CurrentWorkSheetIndex = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
// echo 'WorkSheet' . $CurrentWorkSheetIndex++ . "\n";
echo 'Worksheet number - ', $objPHPExcel->getIndex($worksheet), PHP_EOL;
$highestRow = $worksheet->getHighestDataRow();
$highestColumn = $worksheet->getHighestDataColumn();
$headings = $worksheet->rangeToArray('A1:' . $highestColumn . 1,
NULL,
TRUE,
FALSE);
for ($row = 2; $row <= $highestRow; $row++) {
$rowData = $worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
$rowData[0] = array_combine($headings[0], $rowData[0]);
print_r($rowData);
}
}
You can refer,
http://phpexcel.codeplex.com/
This is good project developed for excel reader and writer. You can use it for your project. It has all necessary methods that are required for excel.

Categories