将数组导出到 PHPExcel 生成数组到字符串的转换错误
Exporting array to PHPExcel producing array to string conversion error
我正在使用 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 文件时,出现 class stdClass
的对象无法转换为字符串错误。
我希望 excel 文件的列名称为 Order
、Date
、Text
、Time
状态,并在行中填充相应的值。
这是我当前的代码
$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');
我认为这是数组中对象的问题,但我不确定如何让它工作。
您需要先将对象转换为数组,使其成为二维数组:
array_walk(
$ordersArray,
function (&$row) {
$row = (array) $row;
}
);
$this->excel->getActiveSheet()->fromArray($ordersArray, NULL, 'A1');
我正在使用 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 文件时,出现 class stdClass
的对象无法转换为字符串错误。
我希望 excel 文件的列名称为 Order
、Date
、Text
、Time
状态,并在行中填充相应的值。
这是我当前的代码
$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');
我认为这是数组中对象的问题,但我不确定如何让它工作。
您需要先将对象转换为数组,使其成为二维数组:
array_walk(
$ordersArray,
function (&$row) {
$row = (array) $row;
}
);
$this->excel->getActiveSheet()->fromArray($ordersArray, NULL, 'A1');