我有以下代码,可以很好地将数据发送到Excel工作表。在数据库中,我有一列下的值,例如k i具有以逗号分隔的值,例如alphonce,ochieng,abc,所以对于每个值/单词,我想创建一个新行,以便alphonce在与ochieng等相同的给定单元格中的新行中,就像生成的表行包含名称一样。
$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++;
}
}
答案 0 :(得分:1)
$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);