且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PHP将数据添加到现有的Excel文件中?

更新时间:2022-01-30 01:27:41

从另一个方向思考!

不是将数据从php推送到excel,而是将数据连接添加到excel,它将从php端点taht publish xml检索其数据.

Instead of pushing data to excel from php, add a data connection to excel that will retrieve its data from a php endpoint taht publish xml.

不知道它是否适用,但是实现起来要容易得多.

Don't know if it applies, but it's far more easier to implement.

此外,您将完全控制excel文件,保留所有自定义设置,包括VBA.

Moreover, you will have total control on the excel file, keeping all customisations, including VBA.

最后,如果您朝着这个方向发展,您将避免管理所有混乱的COM对象.

And finally, you will avoid all messy COM object managing if you end up in this direction.

此解决方案的步骤:

  1. 构建一个发布XML数据的php端点(我实际上没有php知识.只需确保调用yourdata.php会以XML生成数据).
  2. 在excel中,使用数据连接,插入以下XML文件的内容:转到数据TAB(假定Excel 2010),来自其他来源",来自Xml数据导入".
  3. 让向导完成任务
  1. Build a php endpoint that publish the XML data (I have actually no php knowledge. Just ensure that calling yourdata.php will produce the data in XML).
  2. In excel, using a Data connection, insert the content of this XML file: Go to Data TAB (assuming Excel 2010), "From other source", "From Xml data import".
  3. let the wizard do the job

在那之后,您将在Excel中拥有一个列表,该列表将链接到您的xml源.只需右键单击列表/刷新以获取最新数据(或将数据连接配置为在打开时自动刷新).

After that, you will have a list in Excel, than is linked to your xml source. SImply right lick the list / refresh to get the latest data (or configure the data connection to auto refresh at opening).