Làm cách nào để thêm hàng mới vào tệp .xls hiện có bằng PHPExcel?Thêm hàng mới bằng PHPExcel?
Tôi có phải tính số hàng đã tồn tại không?
Nếu có, làm cách nào tôi có thể làm điều đó cho một tệp excel?
Làm cách nào để thêm hàng mới vào tệp .xls hiện có bằng PHPExcel?Thêm hàng mới bằng PHPExcel?
Tôi có phải tính số hàng đã tồn tại không?
Nếu có, làm cách nào tôi có thể làm điều đó cho một tệp excel?
Giả thiết lập này:
$objPHPExcel = PHPExcel_IOFactory::load("foo.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
Bạn có thể nhận được số hàng như sau:
$num_rows = $objPHPExcel->getActiveSheet()->getHighestRow();
Sau này, bạn có thể nhìn vào cách chèn một hàng bằng cách sử dụng câu lệnh sau:
$objWorksheet->insertNewRowBefore($num_rows + 1, 1);
Điều này thêm 1 hàng mới trước $num_rows
.
Ví dụ ở trên chỉ thêm hàng trống. Ví dụ bên dưới thêm dữ liệu từ một biểu mẫu.
<?php
require_once '../inc/phpexcel/Classes/PHPExcel.php';
require_once '../inc/phpexcel/Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load("myExcelFile.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
//add the new row
$num_rows = $objPHPExcel->getActiveSheet()->getHighestRow();
$objWorksheet->insertNewRowBefore($num_rows + 1, 1);
$name = isset($_POST['name']) ? $_POST['name'] : '';
if($submit){
//SAVING THE NEW ROW - on the last position in the table
$objWorksheet->setCellValueByColumnAndRow(0,$num_rows+1,$name);
}
//display the table
echo '<table>'."\n";
echo '<thead>
<tr>
<th>Company Name</th>
</tr>
</thead>'."\n";
echo '<tbody>'."\n";
foreach ($objWorksheet->getRowIterator() as $row) {
echo '<tr>'."\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
echo '<td>'.$cell->getValue().'</td>'."\n";
}
echo '</tr>'."\n";
}
echo '</tbody>'."\n";
echo '</table>'."\n";
?>
Tôi đã theo dõi mã của bạn và không nhận được kết quả phù hợp. Bạn có thể vui lòng xem? http://stackoverflow.com/questions/32312743/phpexcel-how-to-use-insertnewrowbefore-function-correctly –
$ objReader được xác định như thế nào? – Novak
Tôi đã theo phần mã của bạn và tôi đã không nhận được kết quả đúng. Bạn có thể vui lòng xem: http://stackoverflow.com/questions/32312743/phpexcel-how-to-use-insertnewrowbefore-function-correctly –