How can i Insert a row in Excel File with PHP? [closed]

[亡魂溺海] 提交于 2020-01-17 16:35:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!