$myData = "Hello,World";
// Write data to cell A1, replacing comma with a new line character
$worksheet->setCellValueByColumnAndRow(1, 1, str_replace(",", "\n", $myData));
// Set the cell alignment to wrap the text
$worksheet->getStyle('A1')->getAlignment()->setWrapText(true);
// Set the row height to automatically adjust to the number of lines
// of data in the cell
$worksheet->getRowDimension(1)->setRowHeight(-1);
Inserting a new row and filling in data to vcells phpExcel
Question
I have the following code that does a great job sending data to excel sheet. In the database I have values under a column say k i have values which are comma separated e.g. alphonce, ochieng, abc, so for each value/word, i want to create a new row so that alphonce is in the new row in a given cell same as ochieng etc, like generated table rows holding the names.
$trd = $this->getRequestedTestsDisplay2($labref);
$coa_details = $this->getAssayDissSummary($labref);
$row = 26;
$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);
$worksheet->setCellValueByColumnAndRow($col++, $row, str_replace(",", "\n", $trd[$i]->specification));
$worksheet->setCellValueByColumnAndRow($col++, $row, str_replace(",", "\n",$determined));
$worksheet->setCellValueByColumnAndRow($col++, $row, str_replace(",", "\n", $trd[$i]->complies));
$worksheet->getStyle($col++ . $row)->getAlignment()->setWrapText(true);
$worksheet->getRowDimension($row)->setRowHeight(-1);
$row++;
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow