gpt4 book ai didi

PHPExcel:使用 PHP 数组从数组中设置列名

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:07:57 25 4
gpt4 key购买 nike

我正在使用 PHPExcel 库将数据导出到 excel。我能够按预期获得所有数据。但是如何设置 PHP 数组中的列名。这是我正在使用的代码。请帮忙

    $data=(
array(10) (
[0] => array(8) (
[#] => (string)
[Name] => (string) Student1
[ID] => (string) 123456
[Date] => (string) 2016-02-01
[Group] => (string) Physics
[Month] => (string) February
[Year] => (string) 2016
)
[1] => array(8) (
[#] => (string)
[Name] => (string) Student2
[ID] => (string) 569874
[Date] => (string) 2016-02-01
[Group] => (string) Biology
[Month] => (string) February
[Year] => (string) 2016......);

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', "#");
$objPHPExcel->getActiveSheet()->setCellValue('B1', "Name");
$objPHPExcel->getActiveSheet()->setCellValue('C1', "ID");
$objPHPExcel->getActiveSheet()->setCellValue('D1', "Date");
$objPHPExcel->getActiveSheet()->setCellValue('E1', "Group");
$objPHPExcel->getActiveSheet()->setCellValue('F1', "Month");
$objPHPExcel->getActiveSheet()->setCellValue('G1', "Year");

//如何替换/制作上面的动态行以根据数组数据作为列名设置第一行的单元格值。即姓名、ID、日期、.....

//Add Data

$objPHPExcel->getActiveSheet()->fromArray($data,NULL,'A2');

最佳答案

// Header
$objPHPExcel->getActiveSheet()->fromArray(array_keys(current($data)), null, 'A1');
// Data
$objPHPExcel->getActiveSheet()->fromArray($data, null, 'A2');

关于PHPExcel:使用 PHP 数组从数组中设置列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36057516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com