如何使用PHP在Excel文件中插入一行? [英] How can i Insert a row in Excel File with PHP?

查看:1085
本文介绍了如何使用PHP在Excel文件中插入一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的php,也许这可以是愚蠢的..
我有5个参数添加到Excel 2007文件(* .xlsx)名称,电话,电子邮件和地址。

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'];

每次代码运行时如何添加1行。

how can i add 1 row each time the code run.

推荐答案

假设您使用的是 PHPExcel ,那么您将这样写一行:

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.

这篇关于如何使用PHP在Excel文件中插入一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆