duan1982453 2016-06-06 07:43
浏览 32

从对象数组创建PHPExcel

I have a PersonMapper that reads from a DB and creates Person objects (PHP Slim Framework 3):

<?php
class PersonMapper extends Mapper {

    public function getPersons() {
        $stmt = $this->db->query("SELECT p.id, p.firstname, p.pastname FROM persons p");
        $results = [];
        while($row = $stmt->fetch()) {
            $results[] = new Person($row);
        }
        return $results;
    }
}

class Person implements ArrayAccess {
    protected $id;
    protected $firstname;
    protected $lastname;

    public function __construct(array $data) {
        // no id if we're creating
        if(isset($data['id'])) { $this->id = $data['id']; }
        $this->firstname= $data['firstname'];
        $this->lastname= $data['lastname'];
    }

    public function getId() { return $this->id; }
    public function getFirstname() { return $this->firstname; }
    public function getLastname() { return $this->lastname; }

    public function offsetExists($offset) {
        return array_key_exists($offset, $this->asArray());
    }

    public function offsetGet($offset) {
        return $this->offsetExists($offset) ? $this->asArray()[$offset]:NULL;
    }

    public function offsetSet($offset, $value) {
        $this->asArray()[$offset] = $value; 
    }

    public function offsetUnset($offset) {
        if ($this->offsetExists($offset)) {
            $_array = $this->asArray();
            unset($_array[$offset]);
        }
    }

    public function asArray() {
        return array(
            'id' => $this->id, 
            'firstname' => $this->firstname, 
            'kurzbezeichnung' => $this->lastname);
    }
}

I am now able to get an array of person objects:

$mapper = new PersonMapper($this->db);
$persons= $mapper->getPersons();

Now I want to put the list of persons into an excel file:

    $excelDoc = new PHPExcel();
    $excelDoc->setActiveSheetIndex(0);
    $excelDoc->getActiveSheet()->fromArray($persons, null, 'A1');

    $writer = PHPExcel_IOFactory::createWriter($excelDoc, 'Excel2007');
    $writer->save("persons.xlsx");

Unfortunately, PHPExcel can't handle this kind of data structure:

Catchable fatal error: Object of class Person could not be converted to
string in /path/to/src/vendor/phpoffice/phpexcel/Classes/
PHPExcel/Cell/DefaultValueBinder.php on line 65

Any help is greatly appreciated!

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-06-06 08:03
    关注

    Please try : As per oficial documentation, you need first to save the file with the object writer

    Please let me know if this is what you wanted

        <?php
    date_default_timezone_set('America/Los_Angeles');
    
    require_once('PHPExcel.php');
    
    $sheet = array(
        array(
          'a1 data',
          'b1 data',
          'c1 data',
          'd1 data',
        )
      );
    
      $doc = new PHPExcel();
      $doc->setActiveSheetIndex(0);
    
      $doc->getActiveSheet()->fromArray($sheet, null, 'A1');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="your_name.xls"');
    header('Cache-Control: max-age=0');
    
      // Do your stuff here
      $writer = PHPExcel_IOFactory::createWriter($doc, 'Excel5');
    
    $writer->save('php://output');
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来