如何制作水平表phpexcel?

时间:2017-08-23 07:22:13

标签: phpexcel

$objPHPExcel->getActiveSheet()->setCellValue('A1', "No");
$objPHPExcel->getActiveSheet()->setCellValue('B1', "Name");
$objPHPExcel->getActiveSheet()->setCellValue('C1', "Age");
$objPHPExcel->getActiveSheet()->setCellValue('D1', "Job");
$styleArray = array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THICK,'color' => array('argb' => '808080'),),),);
$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->applyFromArray($styleArray);

$sql="SELECT * FROM CUBA";
$query_c = mysqli_query($conn,$sql);
$n=2;
while($row_result=mysqli_fetch_assoc($query_c)){
                      $objPHPExcel->getActiveSheet()->setCellValue('A'.$n,$row_result['id']);
                      $objPHPExcel->getActiveSheet()->setCellValue('B'.$n,$row_result['name']);
                      $objPHPExcel->getActiveSheet()->setCellValue('C'.$n,$row_result['age']);
                      $objPHPExcel->getActiveSheet()->setCellValue('D'.$n,$row_result['job']);
                $n++;
}
  $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
  $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);

这是我已经完成的表的代码,我想将其更改为水平。

the table from the coding

我想要的桌子 horizontal table

1 个答案:

答案 0 :(得分:0)

当您使用$n作为行号时;改为使其成为列,并将每个数据项存储在第1-4行中:

$n='B';
while($row_result=mysqli_fetch_assoc($query_c)){
    $objPHPExcel->getActiveSheet()->setCellValue($n.'1',$row_result['id']);
    $objPHPExcel->getActiveSheet()->setCellValue($n.'2',$row_result['name']);
    $objPHPExcel->getActiveSheet()->setCellValue($n.'3',$row_result['age']);
    $objPHPExcel->getActiveSheet()->setCellValue($n.'4',$row_result['job']);
    $n++;
}