出现重复列的信息 phpexcel,mySql

Information coming up as duplicate Columns phpexcel, mySql

我正在从数据库中检索信息并使用 PHPExcel 将其导出到 excel。信息检索似乎工作正常,但列在 excel sheet 中重复。 我使用以下方法检索信息:

public function monthlyKm($startDate, $endDate, $userid)
    {
        $query = "SELECT  a.`travelDate`,MIN(a.`openning`) AS minimum, MAX(a.`closing`) AS maximum, MAX(a.`closing`)- MIN(a.`openning`) AS diff, b.`destination`
                    FROM kilologs a
                    INNER JOIN users u ON u.`userid` = a.`userid`
                    INNER JOIN destination b ON a.`destid` = b.`destid`
                    WHERE  (a.`travelDate` BETWEEN CAST('".$startDate."' AS DATE) AND CAST('".$endDate."' AS DATE))
                    AND a.userid = $userid
                    GROUP BY a.`travelDate`, a.`destid`";
        $query_set = mysql_query($query) or die(mysql_error());
        return $query_set;

    }

信息应该按以下方式显示:

但这是我得到的:

这是生成excelsheet的代码:

$result_set = $kiloLog->monthlyKm( $startDate,$endDate,$userid);


if(isset($_POST['download']))

{

require_once 'src/PHPExcel.php';
    try{

    $sheet = new PHPExcel();
    $sheet->getActiveSheet()->getSheetView()->setZoomScale(75);

    //set Metadata
    $sheet->getProperties()->setCreator('www.bitsofttech.co.za')
        ->setLastModifiedBy('www.bitsofttech.co.za')
        ->setTitle('Kilometer Logs')
        ->setKeywords('kilos logged report');

    //set default settings
    $sheet->getDefaultStyle()->getAlignment()->setVertical(
        PHPExcel_Style_Alignment::VERTICAL_TOP);
    $sheet->getDefaultStyle()->getAlignment()->setHorizontal(
        PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $sheet->getDefaultStyle()->getFont()->setName('Calibri');
    $sheet->getDefaultStyle()->getFont()->setSize(12);

    //Get reference to active spreadsheet in workbook
    $sheet->setActiveSheetIndex(0);
    $activeSheet = $sheet->getActiveSheet();

    //Set Print Options
    $activeSheet->getPageSetup()->setOrientation(
        PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)
        ->setFitToWidth(1)
        ->setFitToHeight(0);

    $activeSheet->getHeaderFooter()->setOddHeader('&C&B&16' .
        $sheet->getProperties()->getTitle())
        ->setOddFooter('&CPage &P of &N');



    //Populate the sheet with data
    $row = mysql_fetch_assoc($result_set);

    print_r($row);

    $colHeaders = array_keys($row);
    $col = 'A';
    $rowNum = 1;

    //set the column headings
    $activeSheet->setCellValue('A1','Date');
    $activeSheet->getColumnDimension('A')->setAutoSize(true);

    $activeSheet->setCellValue('B1','Opening');
    $activeSheet->getColumnDimension('B')->setAutoSize(true);

    $activeSheet->setCellValue('C1','Closing');
    $activeSheet->getColumnDimension('C')->setAutoSize(true);

    $activeSheet->setCellValue('D1','Total');
    $activeSheet->getColumnDimension('D')->setAutoSize(true);

    $activeSheet->setCellValue('E1','Destination');
    $activeSheet->getColumnDimension('E')->setAutoSize(true);



    //Populate the individual cells
    do{
        $col = 'A';
        $rowNum++;
        foreach($row as $value){
            $activeSheet->setCellValue($col++ . $rowNum, $value);
        }

    }while($row= mysql_fetch_array($result_set));

    $activeSheet->getStyle('A2:A' .$rowNum)->getAlignment()
        ->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

    $activeSheet->getStyle('B2:B' .$rowNum)->getAlignment()
        ->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

    $activeSheet->getStyle('C2:C' .$rowNum)->getAlignment()
        ->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

    $activeSheet->getStyle('E2:E' .$rowNum)->getAlignment()
        ->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

    $activeSheet->getStyle('D2:D' .$rowNum)->getAlignment()
        ->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
        ->setWrapText(true);


    //Give the spreadsheet a title
    $activeSheet->setTitle('Monthly Logs');


    //Generate the Excel file and download
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="Monthlylogs.xlsx"');
    header('Cache-Control: max-age=0');

    $writer = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');
    ob_end_clean();
    $writer->save('php://output');
    exit();



}catch(Exception $e){

    $error = $e->getMessage();
    echo $error;
}

Errrm...您的意思是 columns(即垂直线;数据集中的值)重复,对吗?因为我找不到任何重复的行(即水平线;数据集)。

你的问题出在你的循环中:

$row = mysql_fetch_assoc($result_set);
// ...
do{
    // ...
    foreach($row as $value){
        // use $value
    }

} while ($row = mysql_fetch_array($result_set));

注意,您使用 mysql_fetch_assoc 初始化 $row,但为了迭代,您调用 mysql_fetch_array。这是 documentation on mysql_fetch_array:

的方法签名和一些解释

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )

...

By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

TL;DR

答案更新如下

其实有两个问题:

  1. 问题:第一行数据(excel第2行)与后面几行不同

    原因:不同的API调用循环初始化和继续。

    修复: 在两个地方使用相同的 API 调用。

  2. 问题:第 3 行之后的每个 excel 行(数据行 2 及之后的数据)每个值都有两次。

    原因: mysql_fetch_array() 将默认为每个值创建两个索引:一个数字索引和一个关联索引(即列名)。

    修复: 使用不同的 API 调用或指定 $result_type.

两个问题的解决方案包含在一个代码示例中:

while (($row = mysql_fetch_row($result_set)) !== FALSE) {
    // ...
    foreach ($row as $value) {
        // ...
    }
}