我在使用PHPExcel添加新行时遇到问题。在我的数据中,我有一个多维数组。而且我不知道如何在新插入的行下方添加新行。这是我的数组的示例数据我删除了一些细节以缩短它:
Array
(
[0] => Array
(
[particular_name] => HELLO WORLD
[child_label] => Array
(
[items] => Array
(
[0] => Array
(
[particular] => Sample Particular Name1.1
[unit_label] => sqm
[unit_price] => 100
)
)
)
[note] => SAMLPE NOTE 12
)
[1] => Array
(
[particular_name] => TEST ME
[child_label] => Array
(
[items] => Array
(
[0] => Array
(
[particular] => Sample Particular 2.1
[unit_label] => sqm
[unit_price] => 1000
)
[1] => Array
(
[particular] => Sample Particular Name2.2
[unit_label] => lot
[unit_price] => 2000
)
)
)
[note] => SAMLPE NOTE 22
)
)
在excel中,我可以在excel中显示special_name。现在我想在它下面添加child_label。但我不知道如何。
这是我的一些代码:
$baseRow = 14;
$style_header = array(
'font' => array(
'bold' => true
)
);
foreach($content_list['items'] as $r => $dataRow) {
$row = $baseRow + $r;
$h_counter = $r + 1;
$objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $h_counter);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $dataRow['particular_name']); //OK NO ISSUE
$objPHPExcel->getActiveSheet()->getStyle('B'.$row)->applyFromArray($style_header);
/* HERE IS THE ISSUE, I CREATE A LOOP TO DISPLAY THE 2ND ARRAY BUT IT ONLY GETS THE LAST ARRAY AND INSTEAD OF DISPLAYING IT TO THE
BOTTOM OF THE PARTICULAR_NAME IT SHOWS AT THE TOP
foreach($dataRow['child_label']['items'] as $key => $value) {
$child_row = $row + $key;
$child_counter = $child_row + 1;
$objPHPExcel->getActiveSheet()->insertNewRowBefore($child_row,1);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$child_row, $row . "." . $child_row);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$child_row, $value['particular']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$child_row, $value['unit']);
$objPHPExcel->getActiveSheet()->setCellValue('G'.$child_row, $value['unit_label']);
$objPHPExcel->getActiveSheet()->setCellValue('H'.$child_row, $value['unit_price']);
$objPHPExcel->getActiveSheet()->setCellValue('I'.$child_row, "=F" . $child_row . "* H" . $child_row);
}
$objPHPExcel->getActiveSheet()->removeRow($row-1,1);
*/
}
$objPHPExcel->getActiveSheet()->removeRow($baseRow-1,1);
答案 0 :(得分:2)
您在循环中错误地处理行索引,导致代码多次写入相同的行。你需要一个通用的行计数器来正确地获取行号,就像这样(我删除了removeRow调用,因为我不太明白你试图用那些来实现的):
$rowCounter = $baseRow;
foreach($data as $r => $dataRow)
{
//$row = $baseRow + $rowCounter;
//$h_counter = $r + 1;
$objPHPExcel->getActiveSheet()->insertNewRowBefore($rowCounter,1);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$rowCounter, $rowCounter);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$rowCounter, $dataRow['particular_name']); //OK NO ISSUE
$objPHPExcel->getActiveSheet()->getStyle('B'.$rowCounter)->applyFromArray($style_header);
$rowCounter++;
foreach($dataRow['child_label']['items'] as $key => $value)
{
//$child_row = $row + $key;
//$child_counter = $child_row + 1;
$objPHPExcel->getActiveSheet()->insertNewRowBefore($rowCounter,1);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$rowCounter, $rowCounter . "." . $rowCounter);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$rowCounter, $value['particular']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$rowCounter, $value['unit']);
$objPHPExcel->getActiveSheet()->setCellValue('G'.$rowCounter, $value['unit_label']);
$objPHPExcel->getActiveSheet()->setCellValue('H'.$rowCounter, $value['unit_price']);
$objPHPExcel->getActiveSheet()->setCellValue('I'.$rowCounter, "=F" . $rowCounter . "* H" . $rowCounter);
$rowCounter++;
}
//$objPHPExcel->getActiveSheet()->removeRow($row-1,1);
}