我正在尝试通过从 MySql 获取数据在 Excel 中创建报告。我需要从数据库中获取主题标题并将它们作为列输出到 Excel 工作表,例如单元格:D5、E5、F5 ... M5 等。
到目前为止,我已经设法做到了,因此单元格下的显示包含如下标题:
D5:数学,E5:英语,F5:物理等。
| A | B | C | D | E | F | G | H |
--------------------------------------------------------------
1 | | | | | | | | |
...
5 | | | | Mathematics | English | Physics | ... | ... |
这很好。挑战是,我需要在每个sGRADE
之间插入一个标题,如下所示:Subject
| A | B | C | D | E | F | G | H | I | J | K |
--------------------------------------------------------------------------------------
1 | | | | | | | | | | | |
...
5 | | | | Mathematics | GRADE | English | GRADE | Physics | GRADE | ... | ... |
现在这是我到目前为止所拥有的:
$stringindex = "DEFGHIJKLMNOPQRSTUVWXYZ"; // For the excel column cells
$arr_index = str_split($stringindex); // create array elements
$arr_val2 = array(); // create array
$subject = array();
$subname2 = array();
while($sub_row = mysqli_fetch_array($resub, MYSQLI_ASSOC)) {
$subject[] = $sub_row['name']; // Get the subjects from the database and put them in an array
}
foreach($subject as $sub => $subname) {
$subkey[] = $sub; // array of subject keys e.g 0|1|2|3
$subname2[] = $subname; // array of subjects
}
foreach($arr_index as $arr => $arr_val) {
$arr_val2[] = $arr_val; // array of letters e.g D|E|F|G
}
$acount = count($subname2);
$bcount = count($arr_val2);
$size = ($acount > $bcount) ? $bcount : $acount;
$a = array_slice($subname2, 0, $size);
$b = array_slice($arr_val2, 0, $size);
$combine = array_combine($a, $b);
$sheet = $objPHPExcel->setActiveSheetIndex(0); // PHPExcel functions
foreach($combine as $key => $val) { // GET SUBJECTS
$objPHPExcel->getActiveSheet()->getColumnDimension($val)->setAutoSize(true); // Sets Column Width
$sheet
->setCellValue($val.'5', $key); // Lists Subjects as columns
} // END of FOREACH LOOP
上面的代码能够在 excel 中将主题显示为列标题:
问题:如何添加代码以在每个主题词之后添加 GRADE 列?
我希望你能指出我正确的方向,因为我现在卡住了。
提前致谢。