问题
i'm new with php, maybe this is can be stupid.. I have 5 parameters to add in Excel 2007 file (*.xlsx) name, phone, email and adress.
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$adress = $_POST['adress'];
how can i add 1 row each time the code run.
回答1:
Assuming you're using PHPExcel you would do this to write a single row:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$row = 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$row, $_POST['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$row, $_POST['email']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$row, $_POST['phone']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$row, $_POST['adress']);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('myfile.xlsx');
Reading files involves starting here and working forward:
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$data = $objReader->load('myfile.xlsx');
$objWorksheet = $data->getActiveSheet();
Obviously you'll need to do more digging into the docs to learn how to open existing files and append to them, but that hopefully gives you a kick in the right direction.
来源:https://stackoverflow.com/questions/13979226/how-can-i-insert-a-row-in-excel-file-with-php