2012-05-04 49 views
-1

我试图通过从MySql中获取数据在Excel中创建报表。我需要从数据库中得到主题标题并将它们输出到excel表格中,例如单元格:D5,E5,F5 ... M5等。在每个现有数组元素之间添加数组元素

到目前为止,我已设法做到了这一点,细胞持有的标题是这样的:

D5:数学,E5:英语,F5:物理等

| A | B | C | D   | E  | F  | G | H | 
-------------------------------------------------------------- 
1 | | | |    |   |   |  |  | 
... 
5 | | | | Mathematics | English | Physics | ... | ... | 

这是罚款。目前的挑战是,我需要插入GRADE标题在每个Subject S的之间,是这样的:

| 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列?

我希望你能指点我正确的方向因为'我现在卡住了。

在此先感谢。

+0

假设每个学生都有一行,如果不是等级,你会在每个“主题”列中放入什么? – Squig

+0

@Squig,是的,它是每个学生一行,在每个科目下,我显示百分比标记,例如95,然后在GRADE I下显示A – Brian

+0

并且可能想要将它们保留在单独的列中(而不是像95% A)'在一列中),以便对百分比(如高,低,平均数,中位数等)进行一些计算? – Squig

回答

0

你可以尝试这样的事情:

$stringindex = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // For the excel column cells 
$columns = str_split($stringindex);    // create array elements 
$rowIndex = 5;  // start at row 5 in Excel 
$columnIndex = 3; // start at column D in Excel 
$subjects = 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 
} 

$sheet = $objPHPExcel->setActiveSheetIndex(0); // PHPExcel functions 
foreach($subject as $index => $subjectName) { // GET SUBJECTS 
    $columnName = getColumnName($columnIndex, $columns); 
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnName)->setAutoSize(true); // Sets Column Width 
    $sheet->setCellValue($columnName.$rowIndex, $subjectName); // Lists Subjects as columns 
    $columnIndex++; 

    $columnName = getColumnName($columnIndex, $columns); 
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnName)->setAutoSize(true); // Sets Column Width 
    $sheet->setCellValue($columnName.$rowIndex, "GRADE"); 
    $columnIndex++; 
} // END of FOREACH LOOP 

function getColumnName($index, $columns) { 
    $columnName = ""; 
    $numColumns = count($columns); 
    while($index >= $numColumns) { 
     $columnName = $columns[($index % $numColumns)] . $columnName; 
     $index = floor($index/$numColumns) - 1; 
    } 
    $columnName = $columns[($index % $numColumns)] . $columnName; 
    return $columnName; 
} 

我增加了一个功能getColumnName()所它给你 “AA”, “AB”,......如果您的科目和职等专栏超过AZ限制。

+0

你知道吗?你的代码似乎解决了我的问题...初步测试表明它有潜力在我想实现的目标中... lemme测试它更多,然后我会给这个赞许。 – Brian

+0

将代码放入锅中并添加更多成分后,它已经为我工作,所以再次感谢您指引我朝着正确的方向前进。我认为这个问题解决了。 – Brian

+0

当然,您可以用PHPExcel的内置PHPExcel_Cell :: stringFromColumnIndex()方法替换getColumnName()的所有东西;或者只是利用PHP的字符增长器($ columnName ='A'; $ columnName ++;) –

0

$ temp = true;

然后通过列而来的循环:

if($temp==true){ 
    //do stuff 
    $temp=false; 
}else{ 
    //do other stuff 
    $temp=true; 
}