gpt4 book ai didi

PHPExcel 获取数据库字段名称到导出的 Excel

转载 作者:行者123 更新时间:2023-11-29 22:04:51 24 4
gpt4 key购买 nike

我得到了以下代码,它工作正常,但它没有带来数据库字段名称。如何在第一行添加字段名称,甚至添加一些标题名称?

 while($row = mysql_fetch_array($result)){ 

$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['target_id']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['form_id']);
$objPHPExcel->getActiveSheet()->SetCellValue('f'.$rowCount, $row['update_date']);
$objPHPExcel->getActiveSheet()->SetCellValue('g'.$rowCount, $row['user_id']);
$rowCount++;
echo "<br>New record created successfully";
}

最佳答案

添加单独的 SQL 查询来获取列标题并分别插入它们,例如:

$sql = 'SELECT DISTINCT column_name FROM database.table WHERE column_name = column_name;';

然后将标题插入 Excel 工作表中,例如:

    $res = mysqli_query($sql);
$heads = mysqli_fetch($res);

$headers = array();
$loop = 0;
while($row = mysqli_fetch_assoc($res)){
$headers[$loop] = $row;
$loop ++;
}

$r = 0;
$c = 1;
foreach ($headers as $k => $v) {
if(isset($v['column_name']) && '' != $v['column_name']) {
$sheet->setCellValueByColumnAndRow($c, $r, $v['column_name']);
}
$c++;
}

关于PHPExcel 获取数据库字段名称到导出的 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252174/

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