Iterating over mysql data and posting to excel with phpexcel - php

I would like to produce something like this in an excel file but it produces this which is not what i want. I would like the output to be exactly the same. If the number of tests are four, then four tests are dynamically printed vertically in a cell in excel file as in the word document plus the additional details
here is more details of the number of tests and their details.
$trd = $this->getRequestedTestsDisplay2($labref); //An array of objects 4 in total
$coa_details = $this->getAssayDissSummary($labref); //details as shown http://nqcl.alphybay.com/word.png
phpword code
for ($i = 0; $i < count($trd); $i++) {
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
}
$table3->addRow(400);
$table3->addCell(1500)->addText($trd[$i]->name);
$table3->addCell(1500)->addText($trd[$i]->methods);
$table3->addCell(1700)->addText($trd[$i]->compedia);
$table3->addCell(1800)->addText($trd[$i]->specification);
$table3->addCell(1700)->addText('DETERMINED', $style3);
$table3->addCell(1500)->addText($trd[$i]->complies);
}
phpxcel code
$row = 19;
$col=1;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
$worksheet
->setCellValueByColumnAndRow($col, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->complies);
$col++;
}
$row++;
}

Perhaps resetting column for each row, and not overwriting each value in the same row/column:
$row = 19;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
$col=1;
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
$worksheet
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->complies);
}
$row++;
}
EDIT
You mean something like:
$row = 19;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
$col=1;
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
}
$worksheet
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->complies);
}
$row++;
}

Related

Searching in Excel with PHP

I am trying to find the last cells value of a row in an Excel table by the first cells value using PHPExcel. The user enters the value of the first cell in the row (which is the "command" parameter) and if it exists in the table my function is supposed to jump to the last cell of the row and return the value. This is my code:
public function search($command, $excelobj) {
foreach ($excelobj->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$rowiterator = $worksheet->getRowIterator();
foreach ($row->getCellIterator() as $cell) {
$celliterator = $row->getCellIterator();
$head = $celliterator->current()->getValue();
if(strcmp($head, $command)) {
for ($i = 0; $i == 8; $i++) {
$celliterator->next();
}
return $cell->getValue();
}
}
}
}
return false;
}
For some reason it always returns wrong.
I solved it myself, this code functions correctly:
public function search($command, $excelobj) {
foreach ($excelobj->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$rowiterator = $worksheet->getRowIterator();
$celliterator = $row->getCellIterator();
$celliterator->setIterateOnlyExistingCells(true);
$head = $celliterator->current()->getValue();
while (strcmp($head, $command) !== 0 && $rowiterator->valid()) {
$rowiterator->next();
$row = new PHPExcel_Worksheet_Row($worksheet, $rowiterator->key());
$celliterator = $row->getCellIterator();
$head = $celliterator->current()->getValue();
}
for ($i = 0; $i <= 7; $i++) {
$celliterator->next();
}
return $celliterator->current()->getValue();
}
}
return false;
}

How to get row id using color code in phpexcel

I have a excel file which contains certain rows with color i want to get the row id of a particular color code but unable to do it .. already searched but found nothing below is my code for PHPEXCEL
$cellColor = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();
This will give me the color code and for the value i have $cell->getValue() where $cell is some variable for $cellIterator
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell)
{
$cellColor = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();
if (!empty($cell->getCalculatedValue())) {
if ($cellColor == 'yellow') {
echo ($cellColor.'======'.$cell->getValue());
}
}
}
}
$cell->getValue() will give me the value of that particular color code But, the problem is if i have 2 rows with color yellow then $cell->getValue() will give two value like 0-> yellow1 1-> yellow2 but after deleting the 1st yellow colour data in excel then result will be 0-> yellow2 which is wrong what i need is 0->'' 1-> yellow2 Thats why i need row id for that particular color so that i can identify the row.
After hours of practicing got my expected result
$objPHPExcel = PHPExcel_IOFactory::load('someFile.xls');
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
//$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
//$nrColumns = ord($highestColumn) - 64;
$groupCount = array();
$standardSetCount = array();
$standardCount = array();
$learningTargetCount = array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col < $highestColumnIndex; ++$col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$colorCode = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();
/*
* Yellow
*/
if ($colorCode == 'FFFF00') {
$val = $cell->getValue();
//$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
$groupCount[] = $val; // $groupCount[] = $dataType;
}
/*
* gold
*/
if ($colorCode == 'CC9900') {
$val = $cell->getValue();
$standardSetCount[] = $val;
}
/*
* red
*/
if ($colorCode == 'FF3333') {
$val = $cell->getValue();
$standardCount[] = $val;
}
/*
* green
*/
if ($colorCode == '00CC33') {
$val = $cell->getValue();
$learningTargetCount[] = $val;
}
}
}
$group = (array_chunk($groupCount, $highestColumnIndex));
$standardSet = (array_chunk($standardSetCount, $highestColumnIndex));
$standard = (array_chunk($standardCount, $highestColumnIndex));
$learningTarget = (array_chunk($learningTargetCount, $highestColumnIndex));
echo '<pre>';
print_r($learningTarget);
}

PHPExcel_shared_date::ExcelToPHP returns Current Date if value is Empty

I have used PHPExcel to import data from excel to PHP.
In that i am getting one date column using PHPExcel_shared_date::ExcelToPHP thats is getting properly if i put valid date in cell but Problem is if i make it blank while importing, it takes Current Date.
for ($row = 2; $row <= $highestRow + 1; $row++) {
$date_cell = $ci->excel->setActiveSheetIndex(0)->getCell('D' . $row)->getValue();
//echo $date_cell;
if ($row == $highestRow + 1 || in_array($row, $merge_row)) {
if (isset($detail_array) && !empty($detail_array))
$data[] = $detail_array;
}
if (in_array($row, $merge_row)) {
$i = 0;
$detail_array = array();
$detail_array['account'] = $ci->excel->setActiveSheetIndex(0)->getCell("A" . $row)->getvalue();
} else {
for ($col = 0; $col < count($field); $col++) {
$val = $workSheet->getCellByColumnAndRow($col, $row)->getValue();
if ($value_cell && $val) {
$detail_array['detail'][$i][$field[$col]] = $val;
$detail_array['detail'][$i]['date'] = date('d-m-y', PHPExcel_Shared_Date::ExcelToPHP($date_cell));
}
}
$i++;
}
}
print_r($data);
You help would be appreciated, Thanks!
There is problem in flow, May you have to do like:
for ($row = 2; $row <= $highestRow + 1; $row++) {
if ($row == $highestRow + 1 || in_array($row, $merge_row)) {
if (isset($detail_array) && !empty($detail_array))
$data[] = $detail_array;
}
if (in_array($row, $merge_row)) {
$i = 0;
$detail_array = array();
$detail_array['account'] = $ci->excel->setActiveSheetIndex(0)->getCell("A" . $row)->getvalue();
} else {
$alpha = "A";
for ($col = 0; $col < count($field); $col++) {
$val = $workSheet->getCellByColumnAndRow($col, $row)->getValue();
$detail_array['detail'][$i][$field[$col]] = "";
if ($value_cell && $val && trim($val) != "") {
if (PHPExcel_Shared_Date::isDateTime($ci->excel->setActiveSheetIndex(0)->getCell($alpha . $row)))
$detail_array['detail'][$i][$field[$col]] = date('d-m-y', PHPExcel_Shared_Date::ExcelToPHP($val));
else
$detail_array['detail'][$i][$field[$col]] = $val;
}
$alpha++;
}
$i++;
}
}
print_r($data);

how to select specific column order in PHPExcel

I want to select and write specific column names as I want how can I do that ?
the situation is :
$stockQuery = mysql_query("SELECT stokId,urunAdi,tlFiyat,stokMiktari,tlFiyat*stokMiktari,urunGrup,maliyetTipi FROM proje_stok where projeID =".$projeID);
but I do want to select lik
select * from proje_stok where ......
and my current writing code is
while($row_data = mysql_fetch_assoc($stockQuery)) {
$col = 0;
if ($col == 0) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order);
$col++;
}
foreach($row_data as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
$order++;
}
Instead of that I want to use the code like
foreach($row_data as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value['HEREWHATIWANTTOPUTCOLUMNNAME']);
$col++;
}
is there any suggestion to deal with ?
regards
$col = $row = $order = 1;
if ($row_data = mysql_fetch_assoc($stockQuery))
{
foreach ($row_data as $key => $value)
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(++$col, $row, $key);
do
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col = 1, ++$row, $order++);
foreach ($row_data as $value)
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(++$col, $row, $value);
}
while ($row_data = mysql_fetch_assoc($stockQuery));
}

Getting the headers in PHPExcel

I am using this piece of code in PHP to query a database and import the data to an excel file. Currently I am getting the data from the database, but I can't get the headers.
Can anyone tell me how to get the headers from the database?
$objPHPExcel = new PHPExcel();
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}
$objPHPExcel = new PHPExcel();
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
if ($col == 1) {
$row_headings = array_keys($row_data);
foreach($row_headings as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$row = 1;
$col++;
}
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}

Categories