我正在使用 PHPExcel 将 Excel (xlsx) 文件加载到我的 html 页面中。
我设法使用下面的表单添加了一个新表单,但不幸的是,我添加的每一行都会覆盖第一行。有没有办法刷新,这样我就不会覆盖以前的附加值?
这是我添加新行的表格:
<form action="" method="post" id="AddForm" name="AddForm">
<input type="text" id="company" name="company" value="Enter new name" />
<input type="submit" id="save" name="add" value="add"/>
</form>
下面是我用来加载/显示/添加新行的代码
<?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";
?>