擅长PHP - 数据循环? [英] PHP excel - data looping?

查看:129
本文介绍了擅长PHP - 数据循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据数组的数组。

所以基本格式为:

$sheet = array(
  array(
    'a1 data',
    'b1 data',
    'c1 data',
    'd1 data',
  ),
  array(
    'a2 data',
    'b2 data',
    'c2 data',
    'd2 data',
  ),
  array(
    'a3 data',
    'b3 data',
    'c3 data',
    'd3 data',
  )
);

当我传递的数组,我不知道有多少列或行会出现。
我想要做的使用PHP练成了阵列创建一个Excel表。

When I am passed the array I have no idea how many columns or rows there will be. What I want to do is using php excel create an excel sheet out of the array.

这是我所看到的,要设置数据的唯一方法是使用
$ objPHPExcel-> getActiveSheet() - GT; setCellValue('A1',$值);

from what I have seen, the only way to set data is to use $objPHPExcel->getActiveSheet()->setCellValue('A1', $value);

所以我的问题是

你将如何遍历所有的细胞?

How would you loop over the cells?

记住,有可能是说30列和70说行这将是 AD70 那么,你怎么循环?

remembering that there could be say 30 columns and say 70 rows which would be AD70 So, how do you loop that?

还是有一个内置的函数把数组材料了吗?

Or is there a built in function to turn an array to a sheet?

推荐答案

您可以从这样一个数组设置数据:

You can set the data from an array like so:

$objPHPExcel->getActiveSheet()->fromArray($sheet, null, 'A1');

fromArray与二维数组的作品。当第一个参数是二维数组,第二个参数是如果有一个空值,而最后一个参数就是左上角应该使用什么。

fromArray works with 2D arrays. Where the 1st argument is the 2D array, the second argument is what to use if there is a null value, and the last argument is where the top left corner should be.

否则,你将通过数据需要循环:

Otherwise you would need to loop through the data:

$worksheet = $objPHPExcel->getActiveSheet();
foreach($sheet as $row => $columns) {
    foreach($columns as $column => $data) {
        $worksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
    }
}

这篇关于擅长PHP - 数据循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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