PHPExcel从数据库获取数据并将值放入单元格

PHPExcel从数据库获取数据并将值放入单元格,phpexcel,Phpexcel,需要一些帮助。。。 所以,我试图将数据从数据库导出到excel文件。 我能够将数据提取到数据库中,但当我在while循环中将数据导出到excel时,我只得到一条记录。 请帮忙。我正在使用PHPExcel 1.8.0库 这是我的密码: <?php include('config/config_msdb.php'); /** Set default timezone (will throw a notice otherwise) */ date_default_timezone

需要一些帮助。。。 所以,我试图将数据从数据库导出到excel文件。 我能够将数据提取到数据库中,但当我在while循环中将数据导出到excel时,我只得到一条记录。 请帮忙。我正在使用PHPExcel 1.8.0库 这是我的密码:

    <?php

include('config/config_msdb.php');


 /** Set default timezone (will throw a notice otherwise) */
date_default_timezone_set('Asia/Manila');

 // include PHPExcel
require('lib/PHPExcel.php');

// create new PHPExcel object
$objPHPExcel = new PHPExcel;

// set default font
$objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri');

// set default font size
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);


// create the writer
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");


/**

 * Define currency and number format.

 */

// currency format, € with < 0 being in red color
$currencyFormat = '#,#0.## \€;[Red]-#,#0.## \€';

// number format, with thousands separator and two decimal points.
$numberFormat = '#,#0.##;[Red]-#,#0.##';



// writer already created the first sheet for us, let's get it
$objSheet = $objPHPExcel->getActiveSheet();

// rename the sheet
$objSheet->setTitle('YellowCab');



// let's bold and size the header font and write the header
// as you can see, we can specify a range of cells, like here: cells from A1 to A4
$objSheet->getStyle('A2:P2')->getFont()->setBold(true)->setSize(12);



// write header

$objSheet->getCell('A2')->setValue('Date');
$objSheet->getCell('B2')->setValue('TC Within 30 mins');
$objSheet->getCell('C2')->setValue('Total TC');
$objSheet->getCell('D2')->setValue('%');
$objSheet->getCell('E2')->setValue("Within 15 mins");
$objSheet->getCell('F2')->setValue("Total TC");
$objSheet->getCell('G2')->setValue("%");
$objSheet->getCell('H2')->setValue("Excellent TC");
$objSheet->getCell('I2')->setValue("Total TC");
$objSheet->getCell('J2')->setValue("%");
$objSheet->getCell('K2')->setValue('Good TC');
$objSheet->getCell('L2')->setValue('Total TC');
$objSheet->getCell('M2')->setValue('%');
$objSheet->getCell('N2')->setValue('Poor TC');
$objSheet->getCell('O2')->setValue('Total TC');
$objSheet->getCell('P2')->setValue('%');

 //get record
$query="SELECT pr.TransDate,pr.TC30,pr.Total_TRX1,cast(round(pr.Hitrate,0)     as nvarchar (10))+'%' AS Hitrate ,pr.PDT15,pr.Total_TRX2,cast(round(pr.ProdTime,0) as nvarchar(10))+'%' AS ProdTime,pr.Excellence,pr.Total_TRX4,
            cast(round(pr.ExcelPercent,0) as nvarchar(10))+'' AS ExcelPercent,pr.Good,pr.Total_TRX5,cast(round(pr.GoodPercent,0) as nvarchar(10))+'%' AS GoodPercent,
            pr.Poor,pr.Total_TRX6,cast(round(pr.PoorPercent,0) as nvarchar(10))+'%' AS PoorPercent,lp.area_name FROM part_view AS pr
            LEFT JOIN lp_areas AS lp ON lp.id = pr.StoreID
            WHERE lp.is_delete=0 AND lp.area_status=1 AND lp.id!='43'  ORDER BY pr.TransDate DESC";
$que = mssql_query($query);
$i = '3';
while($row=mssql_fetch_array($que)){

    $transdate=date('Y-m-d',strtotime($row["TransDate"]));
    $tc30=$row["TC30"];
    $totaltrx1=$row["Total_TRX1"];
    $hitrate=$row["Hitrate"];
    $pdt15=$row["PDT15"];
    $totaltrx2=$row["Total_TRX2"];
    $prodtime=$row["ProdTime"];
    $excellence=$row["Excellence"];
    $totaltrx4=$row["Total_TRX4"];
    $excelpercent=$row["ExcelPercent"];
    $good=$row["Good"];
    $totaltrx5=$row["Total_TRX5"];
    $goodpercent=$row["GoodPercent"];
    $poor=$row["Poor"];
    $totaltrx6=$row["Total_TRX6"];
    $poorpercent=$row["PoorPercent"];
    $storename=$row["area_name"];

// we could get this data from database, but here we are writing for simplicity

$objSheet->getCell('A'.$i.'')->setValue($transdate);
$objSheet->getCell('B'.$i.'')->setValue($tc30);
$objSheet->getCell('C'.$i.'')->setValue($totaltrx1);
$objSheet->getCell('D'.$i.'')->setValue($hitrate);
$objSheet->getCell('E'.$i.'')->setValue($pdt15);
$objSheet->getCell('F'.$i.'')->setValue($totaltrx2);
$objSheet->getCell('G'.$i.'')->setValue($prodtime);
$objSheet->getCell('H'.$i.'')->setValue($excellence);
$objSheet->getCell('I'.$i.'')->setValue($totaltrx4);
$objSheet->getCell('J'.$i.'')->setValue($excelpercent);
$objSheet->getCell('K'.$i.'')->setValue($good);
$objSheet->getCell('L'.$i.'')->setValue($totaltrx5);
$objSheet->getCell('M'.$i.'')->setValue($goodpercent);
$objSheet->getCell('N'.$i.'')->setValue($poor);
$objSheet->getCell('O'.$i.'')->setValue($totaltrx6);
$objSheet->getCell('P'.$i.'')->setValue($poorpercent);
$i++;



// autosize the columns
$objSheet->getColumnDimension('A')->setAutoSize(true);
$objSheet->getColumnDimension('B')->setAutoSize(true);
$objSheet->getColumnDimension('C')->setAutoSize(true);
$objSheet->getColumnDimension('D')->setAutoSize(true);
$objSheet->getColumnDimension('E')->setAutoSize(true);
$objSheet->getColumnDimension('F')->setAutoSize(true);
$objSheet->getColumnDimension('G')->setAutoSize(true);
$objSheet->getColumnDimension('H')->setAutoSize(true);
$objSheet->getColumnDimension('I')->setAutoSize(true);
$objSheet->getColumnDimension('J')->setAutoSize(true);
$objSheet->getColumnDimension('K')->setAutoSize(true);
$objSheet->getColumnDimension('L')->setAutoSize(true);
$objSheet->getColumnDimension('M')->setAutoSize(true);
$objSheet->getColumnDimension('N')->setAutoSize(true);
$objSheet->getColumnDimension('O')->setAutoSize(true);
$objSheet->getColumnDimension('P')->setAutoSize(true);


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

$objWriter->save('php://output');

/* If you want to save the file on the server instead of downloading, replace the last 4 lines by 
    $objWriter->save('test.xlsx');
*/

}//end get rows
?>
save('php://output');
/*如果要将文件保存在服务器上而不是下载,请将最后4行替换为
$objWriter->save('test.xlsx');
*/
}//结束获取行
?>

提前感谢

为了更新,我已成功导出从数据库中获取的所有数据。 因此,我的解决方案如下:

$i = '3';
while($row=mssql_fetch_array($que)){

    $transdate=date('Y-m-d',strtotime($row["TransDate"]));
    $tc30=$row["TC30"];
    $totaltrx1=$row["Total_TRX1"];
    $hitrate=$row["Hitrate"];
    $pdt15=$row["PDT15"];
    $totaltrx2=$row["Total_TRX2"];
    $prodtime=$row["ProdTime"];
    $excellence=$row["Excellence"];
    $totaltrx4=$row["Total_TRX4"];
    $excelpercent=$row["ExcelPercent"];
    $good=$row["Good"];
    $totaltrx5=$row["Total_TRX5"];
    $goodpercent=$row["GoodPercent"];
    $poor=$row["Poor"];
    $totaltrx6=$row["Total_TRX6"];
    $poorpercent=$row["PoorPercent"];
    $storename=$row["area_name"];

    $data[] = array(
                'TransDate'=>$transdate,
                'TC30'=>$tc30,
                'Total_TRX1'=>$totaltrx1,
                'Hitrate'=>$hitrate,
                'PDT15'=>$pdt15,
                'Total_TRX2'=>$totaltrx2,
                'ProdTime'=>$prodtime,
                'Excellent'=>$excellent,
                'Total_TRX4'=>$totaltrx4,
                'ExcelPercent'=>$excelpercent,
                'Good'=>$good,
                'Total_TRX5'=>$totaltrx5,
                'GoodPercent'=>$goodpercent,
                'Poor'=>$poor,
                'Total_TRX6'=>$totaltrx6,
                'PoorPercent'=>$poorpercent
    );
}

$array = stripslashes(json_encode($data));
$json = (object)json_decode($array);


foreach($json AS $datas){
    $objSheet->getCell('A'.$i.'')->setValue($datas->TransDate);
    $objSheet->getCell('B'.$i.'')->setValue($datas->TC30);
    $objSheet->getCell('C'.$i.'')->setValue($datas->Total_TRX1);
    $objSheet->getCell('D'.$i.'')->setValue($datas->Hitrate);
    $objSheet->getCell('E'.$i.'')->setValue($datas->PDT15);
    $objSheet->getCell('F'.$i.'')->setValue($datas->Total_TRX2);
    $objSheet->getCell('G'.$i.'')->setValue($datas->ProdTime);
    $objSheet->getCell('H'.$i.'')->setValue($datas->Excellence);
    $objSheet->getCell('I'.$i.'')->setValue($datas->Total_TRX4);
    $objSheet->getCell('J'.$i.'')->setValue($datas->ExcelPercent);
    $objSheet->getCell('K'.$i.'')->setValue($datas->Good);
    $objSheet->getCell('L'.$i.'')->setValue($datas->Total_TRX5);
    $objSheet->getCell('M'.$i.'')->setValue($datas->GoodPercent);
    $objSheet->getCell('N'.$i.'')->setValue($datas->Poor);
    $objSheet->getCell('O'.$i.'')->setValue($datas->Total_TRX6);
    $objSheet->getCell('P'.$i.'')->setValue($datas->PoorPercent);



$i++;




}

谢谢

在loopHi中增加
$i
!我已经解决了我的问题谢谢!