gpt4 book ai didi

PHPExcel - colspan 主题名称

转载 作者:可可西里 更新时间:2023-11-01 13:46:57 37 4
gpt4 key购买 nike

  • 表名:主题
    enter image description here

  • 我的预期输出
    enter image description here

  • 我的代码

    <?php
    //dbconnection

    $q = "SELECT name FROM subject";
    $r = mysqli_query($dbc, $q);

    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
    {
    $name[] = $row['name'];
    }

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);

    $headers = array('School Name');
    $objPHPExcel->getActiveSheet()->fromArray($headers, null, 'A1');
    $objPHPExcel->getActiveSheet()->fromArray($name, null, 'B1');

    $objPHPExcel->getActiveSheet()->mergeCells('B1:C1');
    $objPHPExcel->getActiveSheet()->mergeCells('D1:E1');
    $objPHPExcel->getActiveSheet()->mergeCells('F1:G1');
    $objPHPExcel->getActiveSheet()->mergeCells('H1:I1');
    ?>
  • 我想为每个主题设置 colspan = 2。我不想写 4 行单独的代码。我尝试使用 for 循环,如下所示:

    $objWorksheet = $objPHPExcel->getActiveSheet();

    $row = 1;
    $highestColumn = $objWorksheet -> getHighestColumn();
    $highestColumn++;

    for($column = 'A'; $column != $highestColumn; $column++)
    {
    if($column <= 'C')
    {
    //[HERE]
    }
    }
  • 在我写完 for 循环后,我卡住了。我不知道如何控制列名和设置 colspan。有人可以帮助我吗?

最佳答案

在主题列表的循环中执行此操作可能容易得多:

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'School Name');
$objPHPExcel->getActiveSheet()->mergeCells('A1:A2');

$column = 'B';
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$objPHPExcel->getActiveSheet()->setCellValue($column.'1', $row['name']);
$mergeRange = $column.'1:';
$objPHPExcel->getActiveSheet()->setCellValue($column.'2', 'Student Name');
$column++;
$mergeRange .= $column.'1';
$objPHPExcel->getActiveSheet()->setCellValue($column.'2', 'Grade');
$objPHPExcel->getActiveSheet()->mergeCells($mergeRange);
$column++;
}

关于PHPExcel - colspan 主题名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633937/

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