-1

MySql からデータを取得して、Excel でレポートを作成しようとしています。データベースから件名の見出しを取得し、それらを列として Excel シートに出力する必要があります (例: セル: D5、E5、F5 ... M5 など)。

これまでのところ、私はそうすることができたので、セルの下の表示には次のような見出しが表示されます。

D5:数学、E5:英語、F5:物理など

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

これは問題ありません。課題は、各s の間に次のようなGRADE見出しを挿入する必要があることです。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 列を追加するコードを追加するにはどうすればよいですか?

私はあなたが私を正しい方向に向けてくれることを願っています.coz '私は今立ち往生しています.

前もって感謝します。

4

2 に答える 2

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;
}

件名と成績の列がAZの制限を超えた場合に、「AA」、「AB」、...を提供する関数getColumnName()を追加しました。

于 2012-05-04T10:25:15.393 に答える
0

$一時=真;

次に、列を通過するループで:

if($temp==true){
    //do stuff
    $temp=false;
}else{
    //do other stuff
    $temp=true;
}
于 2012-05-04T10:07:47.460 に答える