php读取excel转字符串数组中,将数组导出到PHPExcel产生数组到字符串转换错误

我正在使用Codeigniter和PHPexcel尝试将数据库结果中的数组写入excel工作表中.

我有以下数据组成的数组.

Array

(

[0] => stdClass Object

(

[ORDER] => 12334

[DATE] => 2015-10-05

[TEXT] => TEST

[TIME] => 06:03:03

[STATUS] => 1

)

[1] => stdClass Object

(

[ORDER] => 99999

[DATE] => 2015-10-05

[TEXT] => TEST2

[TIME] => 08:03:03

[STATUS] => 0

)

)

当我尝试使用phpexcel将数据写入excel文件时,得到的类stdClass的对象无法转换为字符串错误.

我想和excel文件一起使用,列名称为Order,Date,Text,Time Status,并用相应的值填充行.

这是我当前的代码

$this->load->library('excel');

$this->excel->setActiveSheetIndex(0);

$this->excel->getActiveSheet()->setTitle('test worksheet');

$this->excel->getActiveSheet()->fromArray($ordersArray, NULL, 'A1');

$filename='export.xls'; //save our workbook as this file name

header('Content-Type: application/vnd.ms-excel'); //mime type

header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name

header('Cache-Control: max-age=0'); //no cache

//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)

//if you want to save it as .XLSX Excel 2007 format

$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');

//force user to download the Excel file without writing it to server's HD

$objWriter->save('php://output');

我相信这与数组中的对象有关,但是我不确定如何使其工作.

解决方法:

您需要首先将对象转换为数组,以使其成为2d数组:

array_walk(

$ordersArray,

function (&$row) {

$row = (array) $row;

}

);

$this->excel->getActiveSheet()->fromArray($ordersArray, NULL, 'A1');

标签:codeigniter,phpexcel,php

来源: https://codeday.me/bug/20191027/1947000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值