在PHPExcel中重写已编辑的XLSX文件

时间:2012-05-28 10:06:16

标签: php phpexcel xlsx

在这个页面中,我发现了一些代码问题,并且在将它们粘在一起后,我正试图让它工作。页面可以读取上传的XLSX文件,打印表单文本字段中的每个单元格。但我无法找到,如何将编辑过的信息添加到以前的相同单元格中? 请帮忙!

<?php
require_once('Classes/PHPExcel.php');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("Ataskaita3.xlsx");
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
echo '<form name="forma" method="post" action="'.$_SERVER['REQUEST_URI'].'">';
echo '<table border=1>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
  echo '<tr>' . "\n";
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false); 
  foreach ($cellIterator as $cell) {
    echo '<td><input type="text" name="laukelis" value="' . $cell->getValue() . '"/></td>' . "\n";
  }
  echo '</tr>' . "\n";
}
echo '</table>' . "\n";
echo '<input type="submit" name="submit" value="Redaguoti" />';
echo '</form>';

if(isset($_POST['submit'])){
error_reporting(E_ALL);
require_once 'Classes/PHPExcel/IOFactory.php';

$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('Ataskaita3.xlsx');
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('A1', '4')
    ->setCellValue('A2', '5')
    ->setCellValue('A3', '6')       
    ->setCellValue('A4', '7');
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('Ataskaita_redaguota.xlsx');
}else{
echo 'edit';
}
?>

1 个答案:

答案 0 :(得分:1)

开始于:

echo '<td><input type="text" name="' . $cell->getCoordinate() . '" value="' . $cell->getValue() . '"/></td>' . "\n";